Can't get actor names for a given film title when multiple films have the same name in DBpedia - sparql

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)

Related

How to filter date to get the film release date in sparql using regex

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX db: <http://dbpedia.org/ontology/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?releaseDate
WHERE {
?film rdf:type db:Film;
foaf:name ?title.
?film dbp:releaseDate ?releaseDate.
FILTER regex(?releaseDate, "2016", "i").
}
Getting the release date but only two i am not sure about querying . since i am new to this.

SPARQL Query for list of ALL Classes related to Person

I want to make a SPARQL query which returns a list of all Ontology classes/properties which are related to Person. For eg., like the subclasses (derived from) of Person
<rdfs:subClassOf rdf:resource="http://dbpedia.org/ontology/Person"/>
or have a domain/range of Person
<rdfs:domain rdf:resource="http://dbpedia.org/ontology/Person"/>.
For example, the results like "http://dbpedia.org/ontology/OfficeHolder" & "http://dbpedia.org/ontology/Astronaut" should be returned by the query, as the first one has rdfs:domain Person while the second one was a rdfs:subClassOf Person.
Here's the query I've written:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
select distinct ?s
where {
{
?s rdfs:domain dbo:Person .
}
union
{
?s rdfs:range dbo:Person .
}
union
{
?s rdfs:subClassOf dbo:Person .
}
}
Now, this query returns a list of all the classes that explicitly mention Person in their Properties, but miss out classes like Singer, which is a subclass of MusicalArtist, which is in the domain of Person.
I want a query that lists out all such classes/properties, which are related to Person, directly or by "inheritance". Any suggestions?
It seems, you confuse classes with properties... Read carefully RDFS 1.1, it is short.
If you want to retrieve both classes and properties "related to" dbo:Person, use property paths:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?p ?s WHERE
{
{
?s (rdfs:subPropertyOf|owl:equivalentProperty|^owl:equivalentProperty)*/
rdfs:domain/
(rdfs:subClassOf|owl:equivalentClass|^owl:equivalentClass)*
dbo:Person .
BIND (rdfs:domain AS ?p)
}
UNION
{
?s (rdfs:subPropertyOf|owl:equivalentProperty|^owl:equivalentProperty)*/
rdfs:range/
(rdfs:subClassOf|owl:equivalentClass|^owl:equivalentClass)*
dbo:Person .
BIND (rdfs:range AS ?p)
}
UNION
{
?s (rdfs:subClassOf|owl:equivalentClass|^owl:equivalentClass)*
dbo:Person .
BIND (rdfs:subClassOf AS ?p)
}
# FILTER (STRSTARTS(STR(?s), "http://dbpedia.org/"))
}

How to get `filter not exists` query working with ARC2?

I have an ARC2 based RDF store setup and filled with some data. The following query returns the expected results (all URIs of things of type vcard:Individual or foaf:Person) when I run it without the FILTER NOT EXISTS part:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX app: <http://example.com#>
SELECT ?person
WHERE {
?person rdf:type ?type .
FILTER (?type = vcard:Individual || ?type = foaf:Person)
FILTER NOT EXISTS {
?person app:id ?id
}
}
When I try to exclude those things having the app:id property by running the full query, it fails with the following message:
Incomplete FILTER in ARC2_SPARQLPlusParser
Incomplete or invalid Group Graph pattern. Could not handle " FILTER NOT EXISTS { " in ARC2_SPARQLPlusParser
When testing the query in the query validator on sparql.org, there are no issues. Is there something wrong with my query that I am missing or is this a shortcoming of ARC2?
Can you think of an alternative query that I can try with ARC2 to get the desired result?
ARC2 does not support SPARQL 1.1, thus, you have to use the common OPTIONAL-FILTER(!BOUND()) pattern:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX vcard: <http://www.w3.org/2001/vcard-rdf/3.0#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX app: <http://example.com#>
SELECT ?person
WHERE {
?person rdf:type ?type .
FILTER (?type = vcard:Individual || ?type = foaf:Person)
OPTIONAL {
?person app:id ?id
}
FILTER ( !BOUND(?id) )
}

Filtering DBpedia disambiguation page

I have a SPARQL Query, and I want to eliminate all disambigution resources. How can I do this? This is my query:
prefix rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
select distinct ?Nom ?resource ?url where {
?resource rdfs:label ?Nom.
?resource foaf:isPrimaryTopicOf ?url.
FILTER (langMatches( lang(?Nom), "EN" )).
?Nom <bif:contains> "Apple".
}
You can add the following prefix and filter to your query:
prefix dbo: <http://dbpedia.org/ontology/>
filter not exists {
?resource dbo:wikiPageRedirects*/dbo:wikiPageDisambiguates ?dis
}
This says to exclude resources and resources that redirect to a resources that disambiguate some articles. That gives you a query like this:
prefix rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix dbo: <http://dbpedia.org/ontology/>
select distinct ?Nom ?resource ?url where {
?resource rdfs:label ?Nom.
?resource foaf:isPrimaryTopicOf ?url.
FILTER (langMatches( lang(?Nom), "EN" )).
?Nom <bif:contains> "Apple".
filter not exists {
?resource dbo:wikiPageRedirects*/dbo:wikiPageDisambiguates ?dis
}
}
SPARQL results
Now, even though that removes all the disambiguation pages, you may still have results that include "disambiguation" in the title. For instance, one of the results is:
The Little Apple (disambiguation)"#en
http://dbpedia.org/resource/The_Little_Apple_(disambiguation)
Even though that has "disambiguation" in the name, it's not a disambiguation page. It doesn't have any values for dbo:wikiPageDisambiguates. it does redirect to another page, though. You may want to filter out things that redirect to something else, too. You can modify the filter though:
filter not exists {
?resource dbo:wikiPageRedirects|dbo:wikiPageDisambiguates ?dis
}
That says to filter out any resource that either redirects to something, or that disambiguates something. This is actually a simpler filter, really. This makes your query:
prefix rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix dbo: <http://dbpedia.org/ontology/>
select distinct ?Nom ?resource ?url where {
?resource rdfs:label ?Nom.
?resource foaf:isPrimaryTopicOf ?url.
FILTER (langMatches( lang(?Nom), "EN" )).
?Nom <bif:contains> "Apple".
filter not exists {
?resource dbo:wikiPageRedirects|dbo:wikiPageDisambiguates ?dis
}
}
SPARQL results

SPARQL query to print the name, birthday pairs for a list of people in dbpedia

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 }
}