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

Viewed 42

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" )
      }
  }
1 Answers

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