Find people linked with Person X using Sparql and DBpedia - sparql

I am new to querying DBPedia using Sparql. I would like to find people related to a person X using DBPedia.
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpr: <http://dbpedia.org/resource/>
PREFIX dbpo: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT ?P
WHERE {dbpr:Chuck_Norris}

In case you want the people directly related to a person, then you need two triple patterns, one having the person as a subject and another as an object. Here is one way to construct such a query:
SELECT DISTINCT ?has ?of ?person
WHERE {
{?person a foaf:Person;
?of dbr:Chuck_Norris}
UNION
{?person a foaf:Person.
dbr:Chuck_Norris ?has ?person}
}
I'm using dbr: as this is a predefined prefix in DBpedia.
In some cases, you'll get different results if you query on http://live.dbpedia.org/sparql
Now, this query has a limitation. It will get you only relations to other DBpedia resources. Quite often there might be related persons asserted as literals. I don't know of an elegant way to get them, but one way would be to ask for a list of properties that are known to be used for person relations. Here is an example with a list with one value for dbp:spouse:
SELECT DISTINCT ?has ?of ?person
WHERE {
{?person a foaf:Person;
?of dbr:Chuck_Norris}
UNION
{?person a foaf:Person.
dbr:Chuck_Norris ?has ?person}
UNION
{values ?has {dbp:spouse}
dbr:Chuck_Norris ?has ?person .}
}
From http://live.dbpedia.org/sparql this will bring you additionally:
has | person
---------------------------------------------------------
http://dbpedia.org/property/spouse | "Dianne Holechek"#en
http://dbpedia.org/property/spouse | "Gena O'Kelley"#en
---------------------------------------------------------
In general asking for literals like that will bring you a lot of garbage results, which you can reduce using string filters.

Related

Sparql get "is dbo:parent of" records from Dbpedia

I am trying to get children of persons. Some persons have children listed in "dbo:child", others have them listed as "is dbo:parent of"
Here are examples of the two types
https://dbpedia.org/page/Elizabeth_II
https://dbpedia.org/page/George_V
For the first one I can pull child records off as follows:
SELECT * WHERE {
<http://dbpedia.org/resource/Elizabeth_II>
dbo:child?child
}
For the second it's a bit harder as I think I have to find other records which point to George_V. I'm struggling to find anything that works, this is the best I can come up with
SELECT *
WHERE
{
?person a dbo:Person ;
dbo:parent [dbo:Person dbr:George_V]
}
limit 10
What's the best way to do this? Is there a way I can combine the two approaches?
Here's an example that solves the problem. Note, you have to look at the data for familial properties (relationship types) and then explore regarding data quality.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT DISTINCT ?royal ?ancestor
WHERE {
{
<http://dbpedia.org/resource/Charles,_Prince_of_Wales>
<http://dbpedia.org/property/father>+ ?ancestor .
}
UNION
{
<http://dbpedia.org/resource/Charles,_Prince_of_Wales>
<http://dbpedia.org/property/mother>+ ?ancestor .
}
BIND ( <http://dbpedia.org/resource/Charles,_Prince_of_Wales> as ?royal )
}
Live DBpedia Query Solution Link.
Alternatively, which is closer to your original property preference (i.e., dbo:parent), here is another property-paths based example.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT DISTINCT ?royal ?ancestor ?ancestorName
WHERE {
<http://dbpedia.org/resource/Elizabeth_II>
dbo:parent* ?ancestor .
?ancestor
foaf:name ?ancestorName .
FILTER (LANG(?ancestorName) = "en")
BIND ( <http://dbpedia.org/resource/Elizabeth_II> as ?royal )
}
Live Query Results Link.
We are working on a new DBpedia release that should have increased quality regarding this kind of information.
Finally, a tweak to some earlier suggestions (posted as comments).
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT DISTINCT ?royal ?child
WHERE {
dbr:George_V dbo:child+|^dbo:parent+ ?child .
BIND ( dbr:George_V AS ?royal )
}
Live DBpedia Query Link.

SPARQL query to find "notable" people

I am using live Dbpedia (http://dbpedia-live.openlinksw.com/sparql/) to get basic details of notable people. My query is:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?x0 ?name ?dob WHERE {
?x0 rdf:type foaf:Person.
?x0 rdfs:label ?name.
?x0 dbpedia-owl:birthDate ?dob.
FILTER REGEX(?name,"^[A-Z]","i").
} LIMIT 200
This works and I use LIMIT 200 to limit the output to a small number of people. My problem is the 200 people are random, and I want some way of measuring 'notability' such that I return 200 notable people, rather than 200 random people. There are over 500,000 people in Dbpedia.
My question is, how can I measure 'notability' and limit the query to return notable people only? I realize there is no 'notability' property and it is very subjective. I am happy to use any indirect or approximate measure such as number of links or number of references. But I don't know how to do this.
Edit : As a result of the helpful comments I improved the query to include page ranks:
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo:<http://dbpedia.org/ontology/>
PREFIX vrank:<http://purl.org/voc/vrank#>
SELECT DISTINCT ?s ?name2 ?dob ?v
FROM <http://dbpedia.org>
FROM <http://people.aifb.kit.edu/ath/#DBpedia_PageRank>
WHERE {
?s rdf:type foaf:Person.
?s rdfs:label ?name.
?s dbo:birthDate ?dob.
?s vrank:hasRank/vrank:rankValue ?v.
FILTER REGEX(?name,"^[A-Z].*").
BIND (str(?name) AS ?name2)
} ORDER BY DESC(?v) LIMIT 100
The problem now is there are lots of duplicates, even though I am using DISTINCT.

Get all Cities of Country from DBpedia using SPARQL

I would like to get all Cities of India from DBpedia database.
I tried many stackoverflow solutions but till now i have not got my required output.
How to fetch all Cities of Country using SPARQL.
Might be (for sure) incomplete since data in DBpedia is based on mappings from Wikipedia (here only the infoboxes):
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?s WHERE {
?s a dbo:City ;
dbo:country dbr:India
}
Or you can try to use the Wikipedia categories:
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX dct: <http://purl.org/dc/terms/>
SELECT DISTINCT ?s WHERE {
?s dct:subject/skos:broader* dbc:Cities_and_towns_in_India
}
I got solution to get Cities of Country. For Example if we want to fetch cities of India then use dbr:India in your Query
SELECT DISTINCT ?placeName WHERE {
?placeName a yago:City108524735 ; dbo:country dbr:India
}
Thanks #AKSW for your help.

Protege Equivalent to query

Please help to list all Mealcourse from the wine Ontology or happy to receive useful links like this one :Sparql query on restriction list (Equivalent To) in protégé
MealCourse
and (hasFood value Pizza)
and(hasDrink value Wine)
Thank you
This is a bit complicated, but if you look into the ontology everything that is defined as a :MealCourse is an owl:equivalentClass, so you need to first find all owl:equivalentClass and then if you look into the result you will see that they are made of owl:intersectionOf parts. Then you need to break this intersection and filter so that you will only get objects that have :MealCourse as part of the intersection.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX : <http://www.w3.org/TR/2003/PR-owl-guide-20031209/food#>
SELECT distinct *
WHERE { ?subject owl:equivalentClass ?object.
?object (owl:intersectionOf | owl:unionOf) ?node.
?node rdf:rest*/rdf:first ?eq.
Filter ( ?eq in (:MealCourse ) )
}
orderBy ?subject

How to combine the results using SPARQL (dbpedia.org with linkedmdb.org or freebase.com)

I need to get all awarded movies on 80th Award Ceremony
I tried to write SPARQL query:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix dbpedia-owl: <http://dbpedia.org/ontology/>
prefix movie: <http://data.linkedmdb.org/resource/movie/>
prefix award: <http://data.linkedmdb.org/page/film_awards_ceremony/180/>
select distinct ?film ?award where {
{ ?film a movie:film.
?award a movie:film_awards_ceremony.
} union
{ ?film a dbpedia-owl:Film }
?film rdfs:label ?label .
}
But the result is full movies list.
I found the data I need also here: https://www.freebase.com/m/02pgky2
How to combine (union) these entities in a right way ?
If is not possible - How to get the result from freebase using SPARQL and dbpedia.org?