/
InfoboxExtractor.scala
338 lines (272 loc) · 14.3 KB
/
InfoboxExtractor.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package org.dbpedia.extraction.mappings
import org.dbpedia.extraction.config.provenance.DBpediaDatasets
import org.dbpedia.extraction.transform.Quad
import collection.mutable.HashSet
import org.dbpedia.extraction.ontology.datatypes.{Datatype, DimensionDatatype}
import org.dbpedia.extraction.wikiparser._
import org.dbpedia.extraction.dataparser._
import org.dbpedia.extraction.util.RichString.wrapString
import org.dbpedia.extraction.ontology.Ontology
import org.dbpedia.extraction.util._
import org.dbpedia.extraction.config.mappings.InfoboxExtractorConfig
import scala.collection.mutable.ArrayBuffer
import org.dbpedia.extraction.config.dataparser.DataParserConfig
import org.dbpedia.iri.UriUtils
import scala.language.reflectiveCalls
/**
* This extractor extracts all properties from all infoboxes.
* Extracted information is represented using properties in the http://xx.dbpedia.org/property/
* namespace (where xx is the language code).
* The names of the these properties directly reflect the name of the Wikipedia infobox property.
* Property names are not cleaned or merged.
* Property types are not part of a subsumption hierarchy and there is no consistent ontology for the infobox dataset.
* The infobox extractor performs only a minimal amount of property value clean-up, e.g., by converting a value like “June 2009” to the XML Schema format “2009–06”.
* You should therefore use the infobox dataset only if your application requires complete coverage of all Wikipeda properties and you are prepared to accept relatively noisy data.
*/
class InfoboxExtractor(
context : {
def ontology : Ontology
def language : Language
def redirects : Redirects
}
)
extends PageNodeExtractor
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Configuration
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private val ontology = context.ontology
private val language = context.language
private val wikiCode = language.wikiCode
private val minPropertyCount = InfoboxExtractorConfig.minPropertyCount
private val minRatioOfExplicitPropertyKeys = InfoboxExtractorConfig.minRatioOfExplicitPropertyKeys
private val ignoreTemplates = InfoboxExtractorConfig.ignoreTemplates
private val ignoreTemplatesRegex = InfoboxExtractorConfig.ignoreTemplatesRegex
private val ignoreProperties = InfoboxExtractorConfig.ignoreProperties
private val labelProperty = ontology.properties("rdfs:label")
private val typeProperty = ontology.properties("rdf:type")
private val propertyClass = ontology.classes("rdf:Property")
private val rdfLangStrDt = ontology.datatypes("rdf:langString")
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Regexes
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: i18n
private val RankRegex = InfoboxExtractorConfig.RankRegex
private val SplitWordsRegex = InfoboxExtractorConfig.SplitWordsRegex
private val TrailingNumberRegex = InfoboxExtractorConfig.TrailingNumberRegex
private val splitPropertyNodeRegexInfobox = if (DataParserConfig.splitPropertyNodeRegexInfobox.contains(wikiCode))
DataParserConfig.splitPropertyNodeRegexInfobox.get(wikiCode).get
else DataParserConfig.splitPropertyNodeRegexInfobox.get("en").get
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Parsers
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private val unitValueParsers = ontology.datatypes.values
.filter(_.isInstanceOf[DimensionDatatype])
.map(dimension => new UnitValueParser(context, dimension, true))
private val intParser = new IntegerParser(context, true, validRange = (i => i%1==0))
private val doubleParser = new DoubleParser(context, true)
private val dateTimeParsers = List("xsd:date", "xsd:gMonthYear", "xsd:gMonthDay", "xsd:gMonth" /*, "xsd:gYear", "xsd:gDay"*/)
.map(datatype => new DateTimeParser(context, new Datatype(datatype), true))
private val singleGeoCoordinateParser = new SingleGeoCoordinateParser(context)
private val objectParser = new ObjectParser(context, true)
private val linkParser = new LinkParser(true)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// State
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private val seenProperties = HashSet[String]()
override val datasets = Set(DBpediaDatasets.InfoboxProperties, DBpediaDatasets.InfoboxTest, DBpediaDatasets.InfoboxPropertyDefinitions)
override def extract(node : PageNode, subjectUri : String) : Seq[Quad] =
{
if(node.title.namespace != Namespace.Main && !ExtractorUtils.titleContainsCommonsMetadata(node.title)) return Seq.empty
val quads = new ArrayBuffer[Quad]()
/** Retrieve all templates on the page which are not ignored */
for { template <- InfoboxExtractor.collectTemplates(node)
resolvedTitle = context.redirects.resolve(template.title).decoded.toLowerCase
if !ignoreTemplates.contains(resolvedTitle)
if !ignoreTemplatesRegex.exists(regex => regex.unapplySeq(resolvedTitle).isDefined)
}
{
val propertyList = template.children.filterNot(property => ignoreProperties.get(wikiCode).getOrElse(ignoreProperties("en")).contains(property.key.toLowerCase))
var propertiesFound = false
// check how many property keys are explicitly defined
val countExplicitPropertyKeys = propertyList.count(property => !property.key.forall(_.isDigit))
if ((countExplicitPropertyKeys >= minPropertyCount) && (countExplicitPropertyKeys.toDouble / propertyList.size) > minRatioOfExplicitPropertyKeys)
{
for(property <- propertyList; if (!property.key.forall(_.isDigit))) {
// TODO clean HTML
val cleanedPropertyNode = NodeUtil.removeParentheses(property)
val splitPropertyNodes = NodeUtil.splitPropertyNode(cleanedPropertyNode, splitPropertyNodeRegexInfobox)
//for(splitNode <- splitPropertyNodes; pr <- extractValue(splitNode); if pr.unit.nonEmpty)
//sh: removed pr.unit.nonEmpty as it kicked out all objectproperty triples from wikilinks,
// didn't test for further side-effects seems to work
for(splitNode <- splitPropertyNodes; pr <- extractValue(splitNode))
{
val propertyUri = getPropertyUri(property.key)
try
{
//sh: pr.unit should be empty (null) for objects
quads += new Quad(language, DBpediaDatasets.InfoboxProperties, subjectUri, propertyUri, pr.value, splitNode.sourceIri, pr.unit.getOrElse(null))
if (InfoboxExtractorConfig.extractTemplateStatistics)
{
val stat_template = language.resourceUri.append(template.title.decodedWithNamespace)
val stat_property = property.key.replace("\n", " ").replace("\t", " ").trim
quads += new Quad(language, DBpediaDatasets.InfoboxTest, subjectUri, stat_template,
stat_property, node.sourceIri, ontology.datatypes("xsd:string"))
}
}
catch
{
case ex : IllegalArgumentException => println(ex)
}
propertiesFound = true
seenProperties.synchronized
{
if (!seenProperties.contains(propertyUri))
{
val propertyLabel = getPropertyLabel(property.key)
seenProperties += propertyUri
quads += new Quad(language, DBpediaDatasets.InfoboxPropertyDefinitions, propertyUri, typeProperty, propertyClass.uri, splitNode.sourceIri)
quads += new Quad(language, DBpediaDatasets.InfoboxPropertyDefinitions, propertyUri, labelProperty, propertyLabel, splitNode.sourceIri, rdfLangStrDt)
}
}
}
}
}
}
quads
}
private def extractValue(node : PropertyNode) : List[ParseResult[String]] =
{
// TODO don't convert to SI units (what happens to {{convert|25|kg}} ?)
extractUnitValue(node).foreach(result => return List(result))
extractDates(node) match
{
case dates if dates.nonEmpty => return dates
case _ =>
}
extractSingleCoordinate(node).foreach(result => return List(result))
extractNumber(node).foreach(result => return List(result))
extractRankNumber(node).foreach(result => return List(result))
extractLinks(node) match
{
case links if links.nonEmpty => { return links}
case _ =>
}
StringParser.parse(node).map(value => ParseResult(value.value, None, Some(rdfLangStrDt))).toList
}
private def extractUnitValue(node : PropertyNode) : Option[ParseResult[String]] =
{
val unitValues =
for (unitValueParser <- unitValueParsers;
pr <- unitValueParser.parse(node) )
yield pr
if (unitValues.size > 1)
{
StringParser.parse(node).map(value => ParseResult(value.value, None, Some(rdfLangStrDt)))
}
else if (unitValues.size == 1)
{
val pr = unitValues.head
Some(ParseResult(pr.value.toString, None, pr.unit))
}
else
{
None
}
}
private def extractNumber(node : PropertyNode) : Option[ParseResult[String]] =
{
intParser.parse(node).foreach(value => return Some(ParseResult(value.value.toString, None, Some(new Datatype("xsd:integer")))))
doubleParser.parse(node).foreach(value => return Some(ParseResult(value.value.toString, None, Some(new Datatype("xsd:double")))))
None
}
private def extractRankNumber(node : PropertyNode) : Option[ParseResult[String]] =
{
StringParser.parse(node) match
{
case Some(RankRegex(number)) => Some(ParseResult(number, None, Some(new Datatype("xsd:integer"))))
case _ => None
}
}
private def extractSingleCoordinate(node : PropertyNode) : Option[ParseResult[String]] =
{
singleGeoCoordinateParser.parse(node).foreach(value => return Some(ParseResult(value.value.toDouble.toString, None, Some(new Datatype("xsd:double")))))
None
}
private def extractDates(node : PropertyNode) : List[ParseResult[String]] =
{
for(date <- extractDate(node))
{
return List(date)
}
//Split the node. Note that even if some of these hyphens are looking similar, they represent different Unicode numbers.
val splitNodes = NodeUtil.splitPropertyNode(node, "(—|–|-|—|–|,|;)")
splitNodes.flatMap(extractDate(_)) match
{
case dates if dates.size == splitNodes.size => dates
case _ => List.empty
}
}
private def extractDate(node : PropertyNode) : Option[ParseResult[String]] =
{
for (dateTimeParser <- dateTimeParsers;
date <- dateTimeParser.parse(node))
{
return Some(ParseResult(date.value.toString, None, Some(date.value.datatype)))
}
None
}
private def extractLinks(node : PropertyNode) : List[ParseResult[String]] =
{
val splitNodes = NodeUtil.splitPropertyNode(node, """\s*\W+\s*""")
splitNodes.flatMap(splitNode => objectParser.parse(splitNode)) match
{
// TODO: explain why we check links.size == splitNodes.size
case links if links.size == splitNodes.size => return links
case _ => List.empty
}
splitNodes.flatMap(splitNode => linkParser.parse(splitNode)) match
{
// TODO: explain why we check links.size == splitNodes.size
case links if links.size == splitNodes.size => links.map(x => UriUtils.cleanLink(x.value)).collect{case Some(link) => ParseResult(link)}
case _ => List.empty
}
}
private def getPropertyUri(key : String) : String =
{
// convert property key to camelCase
var result = key.toLowerCase(language.locale).trim
result = result.toCamelCase(SplitWordsRegex, language.locale)
// Rename Properties like LeaderName1, LeaderName2, ... to LeaderName
result = TrailingNumberRegex.replaceFirstIn(result, "")
result = WikiUtil.cleanSpace(result)
language.propertyUri.append(result)
}
private def getPropertyLabel(key : String) : String =
{
// convert property key to camelCase
var result = key
result = result.replace("_", " ")
// Rename Properties like LeaderName1, LeaderName2, ... to LeaderName
result = TrailingNumberRegex.replaceFirstIn(result, "")
result
}
}
object InfoboxExtractor {
def collectTemplates(node : Node) : List[TemplateNode] =
{
node match
{
case templateNode : TemplateNode => List(templateNode)
case _ => node.children.flatMap(collectTemplates)
}
}
def collectProperties(node : Node) : List[PropertyNode] =
{
node match
{
case propertyNode : PropertyNode => List(propertyNode)
case _ => node.children.flatMap(collectProperties)
}
}
}