使用 SPARQL 查询 DBPedia 以查找具有相同对象的主题

Querying DBPedia using SPARQL to find subjects with same objects

我正在尝试从 dbpedia 中查找所有这些资源,例如 rdf:type 具有相同对象(例如出生日期)的人。? 我想用子查询来做,但它绝对不是解决方案。 谁能提供一些有用的指导?

SPARQL 查询基本上是一组三重模式,即

形式的查询的连接(逻辑与)
?subject ?predicate ?object.

您需要的是相同的?object。考虑到您只关心 ?subject?predicate 并不重要),您可以通过根据 ?object 对结果进行排序来执行这样的查询。因此,您将看到共享 ?object 的结果。

select ?s ?p ?o where {

    ?s ?p ?o.        
}
order by ?o

如果你也关心 ?predicate,你应该使用它来排序结果。

select ?s ?p ?o where {

    ?s ?p ?o.        
}
order by ?o ?p

因为这对查询可能涉及太多结果,因为它们将检索所有可能的结果。我建议根据某些特定条件过滤 ?object。例如,要 select 所有 ?subject 共享 Person 的实例作为他们的 ?object,请使用:

select ?s where {

?s ?p ?o.
{select  ?o where{
    ?o a <http://dbpedia.org/ontology/Person>}
}
} 

根据你的描述我认为你的意思是:

prefix dbp: <http://dbpedia.org/property/>
prefix foaf: <http://xmlns.com/foaf/0.1/>

select ?s1 ?s2 ?dob
where {
    ?s1 a foaf:Person ; dbp:birthDate ?dob . # Find a person, get their dob
    ?s2 a foaf:Person ; dbp:birthDate ?dob . # Find a person with the same dob
}

调整类型和谓词以适应。

这将包括一些冗余:您会在主题相同的地方找到答案 ('Napoleon' 'Napoleon') 并得到两次答案 ('Daniel Dennett' 'Neil Kinnock', 'Neil Kinnock' 'Daniel Dennett')。您可以使用过滤器将其删除:

filter (?s1 < ?s2)

这只是确保一个先于另一个(但是查询引擎想要这样做)。

prefix dbp: <http://dbpedia.org/property/>
prefix foaf: <http://xmlns.com/foaf/0.1/>

select ?s1 ?s2 ?dob
where {
    ?s1 a foaf:Person ; dbp:birthDate ?dob .
    ?s2 a foaf:Person ; dbp:birthDate ?dob .
    filter (?s1 < ?s2)
}

See the result

另一种解决方案是使用聚合函数,就像在这个查询模板中一样

select ?o (count(distinct ?s) as ?cnt) (group_concat(distinct ?s; separator=";") as ?subjects) {
?s a <CLASS> ;
   <PREDICATE> ?o .
}
group by ?o
order by desc(count(distinct ?s))

每个对象的 returns 属于给定谓词的 class CLASS 的主题数量和主题列表 PREDICATE

例如,询问可以使用的足球运动员的日期

prefix dbo: <http://dbpedia.org/ontology/>
select ?date (count(distinct ?s) as ?cnt) (group_concat(distinct ?s; separator=";") as ?subjects) {
?s a dbo:SoccerPlayer ;
   dbo:birthDate ?date .
}
group by ?date
order by desc(count(distinct ?s))
select * where {
?person1 a <http://dbpedia.org/ontology/Person>.
?person1 dbo:birthYear ?date.
?person2 a <http://dbpedia.org/ontology/Person>.
?person2 dbo:birthYear ?date


FILTER (?person1 != ?person2)
}
limit 10

Dbpedia 将不允许您在其 public 端点上执行该查询,因为它会消耗比允许的更多的时间,并且 you cannot change that time. Nevertheless, there are ways to execute it