saydaysago
saydaysago

Reputation: 25

SPARQL regex filter causing 'no results' when filtered property is empty

I am building a Wikidata SPARQL to get information about films based on their IMDb ID. I want to filter the genre results using a regex filter, and I have it working for almost every instance, except when the film has no genres listed.

Here is my code (The included IMDb code 'tt8332922' is for 'A Quiet Place: Part II' which has no genres listed. If you swap it for another IMDb code of a movie that has a listed genre, the query will return results)

I tried wrapping it in an OPTIONAL, but then the filter doesn't work at all. I also tried to nest the filter in the optional {}'s for the initial ?genre definiton, but I couldn't get the syntax to work with grabbing the english language label.

SELECT ?filmLabel (group_concat(DISTINCT ?basedongroup;separator=", ") as ?basedon)  (group_concat(DISTINCT ?genrefixed;separator=", ") as ?genres) (group_concat(DISTINCT ?countryLabel;separator=", ") as ?countries) (group_concat(DISTINCT ?narrativelocaleLabel;separator=", ") as ?Locales) (group_concat(DISTINCT ?setinperiodLabel;separator=", ") as ?Period) ?ratingLabel ?wppage ?metacritic ?aspectratioLabel ?wikidataid
WHERE
{
  ?film wdt:P345 'tt8332922' .
   OPTIONAL { ?wppage schema:about ?film . 
     FILTER(contains(str(?wppage),'//en.wikipedia')) .}
    {bind (REPLACE(STR(?film),"http://www.wikidata.org/entity/","") AS ?wikidataid)}
  OPTIONAL { ?film wdt:P144 ?based. 
          ?basedwp schema:about ?based . 
          FILTER(contains(str(?basedwp),'//en.wikipedia')) .  }
  OPTIONAL { ?film wdt:P136 ?genre. }
  OPTIONAL { ?film wdt:P495 ?country. }
  OPTIONAL { ?film wdt:P840 ?narrativelocale. }
      OPTIONAL { ?film wdt:P2061 ?aspectratio.}
  OPTIONAL { ?film wdt:P1657 ?rating. }
  OPTIONAL { ?film wdt:P1712 ?metacritic.}
    OPTIONAL { ?film wdt:P2408 ?setinperiod.}
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]".
                        
                         }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en".
                            ?based rdfs:label ?basedLabel .
                            ?country rdfs:label ?countryLabel .
                              ?genre rdfs:label ?genreLabel .
                           ?narrativelocale rdfs:label ?narrativelocaleLabel .
                         ?setinperiod rdfs:label ?setinperiodLabel .
                            ?based schema:description ?basedDescription .

                         
 }
  filter(!regex(str(?genreLabel),'(based on|flashback)')) .
  {bind (REPLACE(STR(?genreLabel),"film","") AS ?genrefixed)}

{bind (concat(?basedLabel, " - ", ?basedDescription, " || ('", str(?basedwp), "')") as ?basedongroup)}
}
GROUP BY ?filmLabel ?wppage ?ratingLabel ?metacritic ?aspectratioLabel ?wikidataid

Upvotes: 1

Views: 204

Answers (1)

logi-kal
logi-kal

Reputation: 7880

Why don't you put all what concerns the genre in the same OPTIONAL block?

SELECT
  ?filmLabel
  (group_concat(DISTINCT ?basedongroup;separator=", ") as ?basedon)
  (group_concat(DISTINCT ?genrefixed;separator=", ") as ?genres)
  (group_concat(DISTINCT ?countryLabel;separator=", ") as ?countries)
  (group_concat(DISTINCT ?narrativelocaleLabel;separator=", ") as ?Locales)
  (group_concat(DISTINCT ?setinperiodLabel;separator=", ") as ?Period)
  ?ratingLabel ?wppage ?metacritic ?aspectratioLabel ?wikidataid
WHERE
{
  ?film wdt:P345 'tt8332922' .
  OPTIONAL {
    ?wppage schema:about ?film . 
    FILTER(contains(str(?wppage),'//en.wikipedia')) . }
  BIND(REPLACE(STR(?film),"http://www.wikidata.org/entity/","") AS ?wikidataid)
  OPTIONAL {
    ?film wdt:P144 ?based. 
    ?basedwp schema:about ?based . 
    FILTER(contains(str(?basedwp),'//en.wikipedia')) . }
  OPTIONAL {
    ?film wdt:P136 ?genre .
    ?genre rdfs:label ?genreLabel .
    FILTER(!REGEX(str(?genreLabel),'(based on|flashback)') && lang(?genreLabel) = "en") .
    BIND(REPLACE(STR(?genreLabel),"film","") AS ?genrefixed) }
  OPTIONAL { ?film wdt:P495 ?country . }
  OPTIONAL { ?film wdt:P840 ?narrativelocale . }
  OPTIONAL { ?film wdt:P2061 ?aspectratio . }
  OPTIONAL { ?film wdt:P1657 ?rating . }
  OPTIONAL { ?film wdt:P1712 ?metacritic . }
  OPTIONAL { ?film wdt:P2408 ?setinperiod . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]" . }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en".
    ?based rdfs:label ?basedLabel .
    ?country rdfs:label ?countryLabel .
    ?narrativelocale rdfs:label ?narrativelocaleLabel .
    ?setinperiod rdfs:label ?setinperiodLabel .
    ?based schema:description ?basedDescription . }
  BIND(CONCAT(?basedLabel, " - ", ?basedDescription, " || ('", str(?basedwp), "')") as ?basedongroup)
}
GROUP BY ?filmLabel ?wppage ?ratingLabel ?metacritic ?aspectratioLabel ?wikidataid

I also cleaned the code.

Upvotes: 2

Related Questions