Why does this SPARQL query give wrong results? - sparql

I tried doing some sparql requests on http://dbpedia.org/sparql.
My sparql-request is this:
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?Name ?Todestag ?person
WHERE {
?person dbo:deathPlace :Hamburg .
?person foaf:name ?Name .
?person dbo:deathDate ?Todestag .
FILTER ( ?Todestag > "2016-01-01"^^xsd:date ) .
} ORDER BY ?Todestag
The problem:
Somehow this FILTER doesn’t work. The SPARQL request gives me all people who died on every day since the start of time in DBpedia. However, I just want people who died after 2016. Can anyone spot the mistake in the query or the syntax?

Here is the correct solution to the query, note the casting applied to the FILTER CLAUSE ( i.e. xsd:date(?Todestag ) :
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?Name ?Todestag ?person
WHERE {
?person dbo:deathPlace :Hamburg .
?person foaf:name ?Name .
?person dbo:deathDate ?Todestag .
FILTER ( xsd:date(?Todestag) > "2016-01-01"^^xsd:date ) .
} ORDER BY ?Todestag
Live Query Solution Page (format HTML Table).
Query Definition Page

Okay, I figured it out myself. The problem is definitely the filter. I have now changed the filter and the desired result appears. The filter must be: FILTER (str(?Todestag) >= "2016") .
Completely, it would look like this:
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?Name ?Todestag ?person
WHERE {
?person dbo:deathPlace :Hamburg .
?person foaf:name ?Name .
?person dbo:deathDate ?Todestag .
FILTER (str(?Todestag) >= "2016") .
} ORDER BY ?Todestag

Related

how can i get the result in sparql query of the following?

2.5) List the people and their names who are born in Jönköping before 1900.
2.6) Are there musical artists who were born in Jönköping? (use an ASK query).
2.7) Find 10 people (URI and place of death) who were born in Jönköping, but died elsewhere.
I am using dbpedia to run queries and fetch data
SELECT DISTINCT * WHERE {?x ?y "Jönköping"#en }
This is the URI Which is retrived
DESCRIBE http://dbpedia.org/resource/Jönköping
You can solve as following;
2.5) List the people and their names who are born in Jönköping before 1900
PREFIX db: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?name ?birth
WHERE
{
?person dbo:birthPlace db:Jönköping .
?person dbo:birthDate ?birth .
?person foaf:name ?name .
FILTER (?birth < "1900-01-01"^^xsd:date) .
}
ORDER BY ?birth
2.6) Are there musical artists who were born in Jönköping? (use an ASK query)
PREFIX db: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
ASK WHERE{SELECT ?person WHERE{
?person a dbo:MusicalArtist .
?person dbo:birthPlace db:Jönköping .
}}
2.7)Find 10 people (URI and place of death) who were born in Jönköping, but died elsewhere
PREFIX db: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?name ?person
WHERE
{
?person dbo:birthPlace db:Jönköping .
?person dbo:deathPlace ?deathPlace.
?person foaf:name ?name .
FILTER (?deathPlace != db:Jönköping) .
} ORDER BY ?name LIMIT 10

Get birthplace from DBpedia people in SPARQL

I have the following code to retrieve all people that was born in Barcelona
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?person ?birthPlace
WHERE {
?person rdfs:label ?label.
?person rdf:type dbo:Person.
?person <http://dbpedia.org/property/birthPlace>
<http://dbpedia.org/resource/Barcelona>.
}
However, I do not know how to get the birthPlace. I want a variable that says next to each name that Barcelona is the place of birth. Any ideas?
How about this:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?person ?birthPlace
WHERE {
?person rdfs:label ?label.
?person rdf:type dbo:Person.
?person <http://dbpedia.org/property/birthPlace> ?birthPlace.
FILTER (?birthPlace = <http://dbpedia.org/resource/Barcelona>)
}
Note that your query has a pattern to match labels, but the labels are not returned. That leads to duplicate results because some people have multiple labels (in different languages). Remove the pattern, or add ?label to the SELECT clause.
You can abbreviate <http://dbpedia.org/property/birthPlace> to dbp:birthPlace.

SPARQL: Federated query gives no result when using local file, while same query on dbpedia does

the local file uploaded on stardog:
#prefix dbo: <http://dbpedia.org/ontology/> .
#prefix dbr: <http://dbpedia.org/resource/> .
dbr:United_States dbo:leader dbr:John_Roberts ,
dbr:Joe_Biden ,
dbr:Barack_Obama ,
dbr:Paul_Ryan .
1.query using the local file:
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX db: <http://dbpedia.org/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?person ?o
FROM <http://example.com/leaders.ttl>
WHERE{
dbr:United_States dbo:leader ?person .
SERVICE <http://dbpedia.org/sparql> { ?person dbo:abstract ?o .}
}
2.Same query using only dbpedia will give results:
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX db: <http://dbpedia.org/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?person ?o
FROM <http://example.com/leaders.ttl>
WHERE{
#dbr:United_States dbo:leader ?person .
SERVICE <http://dbpedia.org/sparql> { dbr:United_States dbo:leader ?person. ?person dbo:abstract ?o.}
}
Using the second query will result in a colum with the leaders and a column of abstract of the leaders in all languages available from dbpedia. Why does the first query where I use the local rdf file not work? The select query on the local file with dbr:United_States dbo:leader ?person . returns exactly the same column with the same leaders as running it directly on the dbpedia endpoint: dbpedia:John_Roberts, dbpedia:Joe_Biden, dbpedia:Barack_Obama, dbpedia:Paul_Ryan.
Why does the first query give no results?

dbpedia fetch entitites in language other than english

I'm trying to extract entity dictionary contains person name etc. from dbpedia using sparql.
PREFIX owl: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT ?name
WHERE {
?person a owl:Person .
?person dbpprop:name ?name . FILTER(lang(?name) = "en")
}
The query above did succeed, but when I change the language name to fr, there is nothing to fetch.
How can I fetch names in other languages?
Moreover, why can't I filter language using query below?
SELECT ?name
WHERE {
?person a owl:Person .
?person dbpprop:language "English"
?person dbpprop:name ?name .
}
// this query returns nothing
I tried to fetch all languages using
SELECT DISTINCT ?lanName
WHERE {
?person a owl:Person .
?person dbpprop:language ?lanName .
}
and the result set contains English.
You need to filter based on the language of the value of the property. Not every property will have values in different languages, but some properties will. It seems, from your example, that dbpprop:name doesn't have values in every language. You may find more values in other languages if you look on the other language specific DBpediae.
However, for something like a name, you'll probably get multi-language results if you use the rdfs:label property. For instance, to get the names of Barack Obama, Daniel Webster, and Johnny Cash in Russian, you could do:
select ?label {
values ?person { dbpedia:Johnny_Cash dbpedia:Barack_Obama dbpedia:Daniel_Webster }
?person rdfs:label ?label .
filter langMatches(lang(?label),"ru")
}
SPARQL results
As an aside, note the use of langMatches rather than equality for matching language tags. This is usually a better approach, because it will correctly handle the different language tags within a language For example (from the SPARQL specification), you can find both of the French literals:
"Cette Série des Années Soixante-dix"#fr .
"Cette Série des Années Septante"#fr-BE .
with langMatches(lang(?title),"fr"), but only the first one with lang(?title) = "fr".
You are looking for rdfs:label for a name, of course all the names are English, you are looking at the English dbpedia.
PREFIX owl: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT distinct *
WHERE {
?person a owl:Person .
?person rdfs:label ?name .
FILTER(lang(?name) = "fr")
}
Again, for the second one, if you replace the name with the rdfs: label you can have:
PREFIX owl: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT distinct *
WHERE {
?person a owl:Person .
?person rdfs:label ?name .
?person dbpprop:language <http://dbpedia.org/resource/English_language>.
}

GROUP BY in SPARQL?

Assume that I uses the FOAF ontology. I want to return the name and the mbox for each person. I use the following query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?name ?email
WHERE {
?person foaf:name ?name.
?person foaf:mbox ?email.
}
The result of the join is a set of rows: ?person, ?name, ?email. This query is returning the ?name and ?email. Note that in some of the ?person may have multiple mailboxes, so in the returned set, a ?name row may appear multiple times, once for each mailbox.
Is there a solution to make a GROUP BY person ?name?
You can group by person but then you need an aggregation for ?name and ?email
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT (sample(?name) AS ?name2) (sample(?email) as ?email2)
WHERE {
?person foaf:name ?name.
?person foaf:mbox ?email.
} GROUP BY ?person
SAMPLE picks one possible from the group for each ?person.
or maybe
SELECT (group_concat(?name) AS ?names)
(except that's a string).
It may be easier work with
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?email
WHERE {
?person foaf:name ?name.
?person foaf:mbox ?email.
}
ORDER BY ?person ?name ?email
and process the results in your application where you know the incoming results have all the entries for one person is a single section of the results.