从DBPedia中提取英语的所有类型及其标签 [英] Extract all types and their labels in English from DBPedia
问题描述
我正在尝试使用此SPARQL查询从DBpedia中获取所有类型:
I'm trying to get all types from DBpedia using this SPARQL query:
select ?type {
?type a owl:Class .
}
现在,我还想包括由查询。我需要在查询中添加什么?
Now, I want to also include the English label of each type returned by the query. What do I need to add to my query?
推荐答案
这是一个很好的机会,可以学到更多有关如何检索任意值的知识。 DBpedia提供的信息。您的第一个查询(添加了限制)是:
This is a good opportunity to learn a bit more about how to retrieve arbitrary information from DBpedia. Your first query (with a limit added) is:
select ?type {
?type a owl:Class .
}
limit 10
结果之一是 http://dbpedia.org/ontology/Animal ,您实际上可以在网络浏览器中访问它,并且相应的页面将显示所有这些资源属性。对于动物
来说,还不是很多,但是我们感兴趣的是
One of the results is http://dbpedia.org/ontology/Animal, which you can actually visit in a web browser, and the corresponding page will display all of that resources properties. For animal
, there aren't all that many, but the ones of interest to us are
rdfs:label Tier
rdfs:label animal
rdfs:label animal
rdfs:label žival
rdfs:label 동물
我们在此处感兴趣的属性是 rdfs:label
,因此我们可以将查询扩展到
The property that we're interested in here is rdfs:label
, so we can extend the query to
select ?type ?label {
?type a owl:Class .
?type rdfs:label ?label .
}
limit 10
实际上,我们可以使用分号:
which we can actually abbreviate a little bit, using the semicolon:
select ?type ?label {
?type a owl:Class ;
rdfs:label ?label .
}
limit 10
该查询,尽管对于每个?type
;实际上,每个?标签
中有一个,因此我们得到的结果包括:
That query, though will return multiple results for each ?type
; in fact, one per ?label
, so we get results including:
http://dbpedia.org/ontology/Animal "Tier"@de
http://dbpedia.org/ontology/Animal "animal"@en
请注意,标签不仅是字符串,而且是带有语言标签的RDF文字。在SPARQL中,我们可以使用 <$获得RDF文字(如果有的话)的语言标签。 c $ c> lang 函数。可以使用 =
运算符将语言标记与 en
进行比较,但是更可靠的解决方案是使用 langMatches
,它将处理较棘手的案例,例如文档中给出的案例,其中
Notice that the labels aren't simply strings, but are RDF literals with language tags. In SPARQL, we can get the language tag of an RDF literal (if it has one) using the lang
function. It is possible to compare the language tag to "en"
with the =
operator, but a more robust solution is to use langMatches
, which will handle trickier cases like the one given in the documentation where
filter langMatches( lang(?title), "FR" )
可用于选择以下两个值,以?title
,而 filter(lang(?title)= fr)
只会找到第一个:
can be used to find select both the following values for ?title
, whereas filter( lang(?title) = "fr" )
would find only the first:
"Cette Série des Années Soixante-dix"@fr
"Cette Série des Années Septante"@fr-BE
使用langMatches,lang和filter,我们可以再次将查询更新为
Using langMatches, lang, and filter, we can update the query once more to
select ?type ?label {
?type a owl:Class ;
rdfs:label ?label .
filter(langMatches(lang(?label),"EN"))
}
limit 10
可检索DBpedia类型及其英文标签。
which retrieves DBpedia types and their English labels.
这篇关于从DBPedia中提取英语的所有类型及其标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!