Wikidata query for items with one Wikipedia page not in English

Viewed 86

I want to find Wikidata items, with each referring to exactly one Wikipedia page which is not an en. Wikipedia page.

I came up with this query:

SELECT ?item WHERE {
  ?article schema:about ?item .
  FILTER (SUBSTR(str(?article), 9, 2) != "en") .
  {
  SELECT ?item (COUNT(DISTINCT ?lang) AS ?count) WHERE {
    ?item wdt:P1367 ?yp_id .     # BBC 'Your paintings' artist identifier
    ?article schema:about ?item .
    FILTER (SUBSTR(str(?article), 11, 15) = ".wikipedia.org/") .
    ?article schema:inLanguage ?lang .
  } GROUP BY ?item
  HAVING (?count=1)
  ORDER BY DESC (?count)
  }
}

It executes. However, I always get a timeout.

Is there a better query to achieve what I am looking for?

1 Answers

Here's some tip:

  1. Since you take only ?count=1, there is no reason to order by ?count.
  2. Since for each article you can have only one ?lang, you can count by ?article without considering a redundant variable.
  3. Instead of working on (sub)strings, just use the schema:isPartOf property for selecting the specific domain that you want to exclude.
  4. Use FILTER NOT EXISTS instead of FILTER (... != ...)

The fourth optimiziation is the most important and it is sufficient per se.

SELECT ?item WHERE {
  FILTER NOT EXISTS { 
    ?article schema:about ?item ;
             schema:isPartOf <https://en.wikipedia.org/> .
  }
  {
  SELECT ?item (COUNT(DISTINCT ?article) AS ?count) WHERE {
    ?item wdt:P1367 ?yp_id .     # BBC 'Your paintings' artist identifier
    ?article schema:about ?item .
    FILTER (SUBSTR(str(?article), 11, 15) = ".wikipedia.org/") .
  }
  GROUP BY ?item
  HAVING (?count=1)
  }
}
Related