I am very new to this semantic web and I am doing some experiments with Sparql & DBPEDIA. I wrote a query to find out all the records which has firstName(givenName) and lastName(surName).
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
select count(DISTINCT ?x) where {
?x foaf:givenName ?y;
foaf:surname ?z.
}
When I run this query I got 9,80,000 records. But when I ran the individual queries (for firstName and lastName) I got 9,92,000 records for firstName and 9,80,000 for lastName.
Here my question is how can I get the resources which has only firstName but not the lastName...???
You can use the NOT EXISTS clause with a FILTER to achieve this:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT (COUNT(DISTINCT ?x) AS ?count)
WHERE
{
?x foaf:givenName ?y .
FILTER(NOT EXISTS { ?x foaf:surname ?z. })
}
This filters out matches for ?x where the NOT EXISTS pattern is also matched.
Alternatively you can use the MINUS clause which does roughly the same bar some minor differences which won't affect your query:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT (COUNT(DISTINCT ?x) AS ?count)
WHERE
{
?x foaf:givenName ?y .
MINUS { ?x foaf:surname ?z. }
}
Related
I am trying to get actor names for a given film title (I also have the release date in hand) with my sparql query, but given the situation that multiple films have the same name, I'm trying to differentiate them with the release date. Some films don't have the release date specified, some films don't have the label specified.
I'm trying to get results when either the release date is specified and is matching, or when it is in the label of the film and is also matching.
If I can't match the date with one of these attributes, I want no results in return
Here is my current query:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?film ?aname
WHERE {
?film a dbo:Film ;
foaf:name "A Nightmare on Elm Street"#en ;
dbo:starring ?a .
?a foaf:name ?aname
OPTIONAL
{ ?film dbo:releaseDate ?rd
BIND(year(xsd:date(?rd)) AS ?rrd)
FILTER ( ( ?rd = "1984-04-30"^^xsd:date ) || ( ?rrd = 1984) )
}
OPTIONAL
{ ?film rdfs:label ?lab
FILTER regex(?lab, "1984", "i")
FILTER ( lang(?lab) = "en" )
}
}
I think you are misusing OPTIONAL here. Instead you should be looking at UNION.
What's the difference?
SELECT ?person ?child
WHERE {
?person a :Person .
OPTIONAL {?person :hasSon ?child}
OPTIONAL {?person :hasDaughter ?child}
}
Will return every person, and optionally their sons/daughters. However this will return also people without any children at all.
Instead, something like:
SELECT ?person ?child
WHERE {
?person a :Person .
{?person :hasSon ?child}
UNION
{?person :hasDaughter ?child}
}
Will only return people who have at least one son or daughter.
Now, in your example, I have a query working like this:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?film ?aname
WHERE {
?film a dbo:Film ;
foaf:name "A Nightmare on Elm Street"#en ;
dbo:starring ?a .
?a foaf:name ?aname
{ ?film dbp:released 1984}
UNION
{ ?film rdfs:label ?lab
FILTER regex(?lab, "1984", "i")
FILTER ( lang(?lab) = "en" )
}
}
Notice that I used the dbp:released property which seems to be working.
It seems that the property used for releases is inconsistent across films, i.e. some use dbp:released , others dbo:releaseDate.
If that's an issue, you can of course add another UNION statement in the query to deal with the different case.
One more thing:
there are many triplestores out there that have reasoning, and reasoning is something that can help deal with a variety of such situations (disambiguation, multiple properties for the same thing, etc)
Assuming the triples are following:
#prefix : <http://example/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix foaf: <http://xmlns.com/foaf/0.1/> .
:alice rdf:type foaf:Person .
:alice foaf:name "Alice" .
:bob rdf:type foaf:Person .
and then we perform 3 queries based on SPARQL 1.1:
Q1:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?s foaf:name ?y }
}
Q2:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?x foaf:name ?y }
}
Q3:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?x foaf:mailbox ?y }
}
These three queries return three different solutions. Could anyone help me figure out why Q2 evaluates to no query solution in contrast to Q1 and Q3? Many thanks in advance :)
Q2 returns no solution because in your data, there exists a statement that matches ?x foaf:name ?y: ?x = :alice and ?y = "Alice". You've put no further constraints on either ?x or ?y. So no matter what the other variables in your query (?s, ?p and ?o) are bound to, the NOT EXISTS condition will always fail and therefore the query returns no result.
I created this query to return all books that are notable works by George Orwell but it returns no result.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?title where {
?person foaf:name ?name .
?title dbo:author ?person .
?title dbo:notableWork dbp:George_Orwell .
}
I cannot seem to figure out why there is no result.
I am running the query in http://dbpedia.org/snorql
Don't you have the triples about notable works in the wrong order?
Try rewriting based on this working query
SELECT *
WHERE {
:George_Orwell dbo:notableWork ?title
}
.
title
:Nineteen_Eighty-Four
:Animal_Farm
You can also bind :George_Orwell to a variable and ask more about that:
SELECT *
WHERE {
values ?author { :George_Orwell } .
?author rdfs:label ?l .
?title ?p ?author .
?title rdf:type dbo:Book .
filter (lang(?l) = "en")
}
and DESCRIBE things
describe :Animal_Farm
I created this request but does not show the results that can concater with the given name familyName it just displays the givenName and then another line, attorneys in the familyName.
i used jena 3.0 .
why does not work?
PREFIX apf: <http://jena.hpl.hp.com/ARQ/property#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT *
{?Person foaf:givenName ?x .
?Person foaf:familyName ?y .
?name apf:concat (?x " " ?y) }
Given a list of Wikipedia article titles (people's names), how do I print the name, birthday pair for each person?
Here is one way to do it using the VALUES clause in SPARQL 1.1:
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?givenName ?surname ?birth
WHERE
{
?person dbpedia-owl:birthDate ?birth .
?person foaf:givenName ?givenName .
?person foaf:surname ?surname .
VALUES ?person { dbpedia:Albert_Einstein dbpedia:Max_Planck dbpedia:Marie_Curie }
}