Getting DBPedia Infobox categories

岁酱吖の 提交于 2019-11-30 13:53:39

OK, so I've actually figured out more or less exactly how to do this, so I'm submitting this as an answer rather than just an edit. What seems to give me exactly what I'm looking for is to start by iterating through the class heirarchy using this query:

SELECT ?class ?label WHERE {
     ?class rdfs:subClassOf owl:Thing.
     ?class rdfs:label ?label. 
     FILTER(lang(?label) = "en")
}

Feeding the selected result into the query in place of owl:Thing each time.

Once the user has selected the lowest-level class that they'd like, to display a list of properties, in descending order by the number of entries in which they appear, I use this query:

SELECT ?prop ?title WHERE {
     ?country ?prop [].
     ?country a <http://dbpedia.org/ontology/Country>.
     ?prop rdf:type rdf:Property.
     ?prop rdfs:label ?title
} ORDER BY DESC(COUNT(DISTINCT ?country))

Of course, if you actually look at those results, there are some funky properties there that don't have very descriptive labels ("s"? What?), but this is at least what I was looking for in the first place.

(1) Query for all existing classes:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?class
WHERE {
  ?s rdf:type ?class .
}

(2) Query for all properties used in any instance of class C:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?property
WHERE {
  ?s rdf:type <C> .
  ?s ?property ?o
}

This will get you all of the properties whose rdfs:domain is SpaceMissions:

select ?property where {
    ?property rdfs:domain <http://dbpedia.org/ontology/SpaceMission>
}

These properties all accept SpaceMission as the subject.

Note that in RDF(S), it's not required to have an explicit rdfs:domain statement for each property, because rdfs:domains can be implied by the usage of the property. You may find therefore that this query will give you a list of all properties that have been defined with a domain of SpaceMission but won't give you a list of all properties that are actually used with all of the instances of SpaceMission.

It's quite possible that some of the properties aren't actually defined as such with:

?p a rdf:Property .

but any term in the middle position is by definition a property. So you might get more results with:

SELECT ?prop ?title WHERE {
     ?country a <http://dbpedia.org/ontology/Country>.
     ?country ?prop [] .
     ?prop rdfs:label ?title .
} 
ORDER BY DESC(COUNT(DISTINCT ?country))

(reordered slightly, being selective at the start may improve performance)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!