我正在尝试使用SPARQL查询维基词典,以获取某些语言的名词的所有术语(例如德语) 并作为输出:
我正在使用SPARQL-Endpoint:http://wiktionary.dbpedia.org/sparql我发现了一个例子,但我没弄明白 如何调整它以获得我想要的信息。
PREFIX terms:<http://wiktionary.dbpedia.org/terms/>
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
PREFIX dc:<http://purl.org/dc/elements/1.1/>
SELECT ?sword ?slang ?spos ?ssense ?twordRes ?tword ?tlang
FROM <http://wiktionary.dbpedia.org>
WHERE {
?swordRes terms:hasTranslation ?twordRes .
?swordRes rdfs:label ?sword .
?swordRes dc:language ?slang .
?swordRes terms:hasPoS ?spos .
OPTIONAL { ?swordRes terms:hasMeaning ?ssense . }
OPTIONAL {
?twordBaseRes terms:hasLangUsage ?twordRes .
?twordBaseRes rdfs:label ?tword .
}
OPTIONAL { ?twordRes dc:language ?tlang . }
}
答案 0 :(得分:6)
首先,您要选择所有名词的术语感官。正如您在示例查询的查询结果中所看到的,此信息由terms:hasPoS
关系捕获。因此,要专门查询所有名词,我们可以这样做:
PREFIX terms: <http://wiktionary.dbpedia.org/terms/>
SELECT ?term
WHERE {
?term terms:hasPoS terms:Noun .
}
LIMIT 100
你想要的下一件事只是某种语言的名词。这似乎由dc:language
关系覆盖,因此我们在该关系上添加了一个额外的约束。假设我们想要所有的英语名词:
PREFIX terms: <http://wiktionary.dbpedia.org/terms/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?term
WHERE {
?term terms:hasPoS terms:Noun ;
dc:language terms:English .
}
LIMIT 100
所以,我们现在正在选择你想要的东西,但我们还没有你想要的格式的输出,因为上面的查询只返回了术语sense的标识符,而不是实际的字符串值术语。正如我们在示例查询的输出中看到的那样,字符串值由rdfs:label
属性捕获,因此我们添加:
PREFIX terms: <http://wiktionary.dbpedia.org/terms/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
SELECT ?term ?termLabel
WHERE {
?term terms:hasPoS terms:Noun ;
dc:language terms:English ;
rdfs:label ?termLabel .
}
LIMIT 100
如果您现在查看此查询的结果,您会发现该语言存在奇怪的事情:尽管我们认为我们选择了英语,但我们也会找回具有不同语言标签的标签(例如'@ru')。要删除这些结果,我们可以进一步限制我们的查询,并说我们只想要英文背面标签:
PREFIX terms: <http://wiktionary.dbpedia.org/terms/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
SELECT ?term ?termLabel
WHERE {
?term terms:hasPoS terms:Noun ;
dc:language terms:English ;
rdfs:label ?termLabel .
FILTER(langMatches(lang(?termLabel), "en"))
}
LIMIT 100
最后,性别/属。在这里,我不太确定。查看wiktionary数据中的一些示例资源(例如,entry for dog)我会说这些信息实际上并不存在于数据中。
答案 1 :(得分:3)
Jeen的答案很重要。这是获得性别的一种选择。
英语作为一种示例语言并不适用,因为它没有语法性别。让我们来德语:
PREFIX terms: <http://wiktionary.dbpedia.org/terms/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
SELECT ?term ?termLabel
WHERE {
?term terms:hasPoS terms:Noun ;
dc:language terms:German ;
rdfs:label ?termLabel .
FILTER(langMatches(lang(?termLabel), "de"))
}
LIMIT 100
(过滤掉许多完全重复的内容会很好。(我不知道怎么做,以及为什么他们在那里。)
采用德语术语&#34; Eierkopf&#34;而不是英语&#34;狗&#34;: 我们现在可以按照http://wiktionary.dbpedia.org/resource/Eierkopf-German-Noun这一术语链接,我们在德语http://de.wiktionary.org/wiki/Eierkopf中看到了与维基词典的链接(我们也可以猜到这个网址,而不是先从wiktionary.dbpedia.org获取)。
这里可以从文本中提取属:&#34; Substantiv,m&#34; (男性为m)
德语的选项是:
<em title="Genus: Maskulinum (grammatikalisches Geschlecht: männlich)">m</em>
<em title="Genus: Femininum (grammatikal. Geschlecht: weiblich)">f</em>
<em title="Genus: Neutrum (grammatikal. Geschlecht: sächlich)">n</em>
如果名词根据区域/方言具有不同的性别,则官方性别在HTML中如上所述,并且评论显示在下方。例如:
https://de.wiktionary.org/wiki/Butter
因此,除了查询SPARQL之外,每个单词还需要1-2个网页请求,以及一些HTML内容提取。