Christian
Christian

Reputation: 111

SPARQL wikidata query: Obtain number of languages that associated wikipedia article is available in

Is it possible to use SPARQL on wikidata to extract the number of languages of a wikipedia article associated with a wikidata item?

I am new to SPARQL and wikidata. I have tried to find examples online, but no luck so far. I am able to extract the url for the wikipedia article. But I wonder if it is possible to count the number of languages for which the url exists. Here is my code so far:

prefix schema: <http://schema.org/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT ?museum ?museumLabel ?article WHERE {
  ?museum wdt:P17 wd:Q39;   # countries                  
          wdt:P31 ?type.
          ?type (wdt:P279*) wd:Q207694.


         # If available, get the "en" entry, use native language as fallback:
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en,de". }
  # get wikipedia article in english       
  OPTIONAL {
      ?article schema:about ?museum .
      ?article schema:inLanguage "en" .
      FILTER (SUBSTR(str(?article), 1, 25) = "https://en.wikipedia.org/")
    }
}
order by ?museum

I would like to add another column to my output that gives me the number of languages a wikipedia article exists in.

Upvotes: 1

Views: 638

Answers (1)

feinmann
feinmann

Reputation: 1122

PREFIX schema: <http://schema.org/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT ?museum ?museumLabel (COUNT(DISTINCT ?article) as ?wiki_article_count) WHERE {
       ?museum wdt:P17 wd:Q39;   # countries                  
               wdt:P31 ?type.
               ?type (wdt:P279*) wd:Q207694.
       SERVICE wikibase:label { bd:serviceParam wikibase:language "en,de". }
       OPTIONAL {
          ?article schema:about ?museum .
       }
}
GROUP BY ?museum ?museumLabel
ORDER BY DESC( ?wiki_article_count )

Further developing the query given in the question, one can obtain the number of linked wiki-pages (I am absolutely not sure about the nomenclature).

Please try the Wikidata Query Service here: https://w.wiki/334g

For example, one can see for the wikidata/wiki entry: https://www.wikidata.org/wiki/Q194626 (Kunstmuseum Basel) (nomenclature remains unclear to me for now. Is it a wiki-entity?) 22 linked wiki-pages. That is 21 wikipedia-languages and one multilingual-site. See screenshot here:

enter image description here

I would be thankful for any comments on this answer and could improve this answer accordingly.

Upvotes: 0

Related Questions