Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of languages a Wikipedia page is available in? (SPARQL query)

I'm trying to have a list of Italian books from the 1980 on and the number of Wikipedia pages their original Wikipedia page has been translated into, for instance I would like to have:

Book, number
The name of the Rose, 5

Where 5 is the number of languages The Name of the Rose has been translated into in Wikipedia, for instance there is an English wiki page, a Dutch one, a Spanish one, a French one, a Greek one.

Here is the query so far:

SELECT ?item ?label 
WHERE
{
  VALUES ?type {wd:Q571 wd:Q7725634}  # book or literary work
  ?item wdt:P31 ?type .
  ?item wdt:P577 ?date FILTER (?date > "1980-01-01T00:00:00Z"^^xsd:dateTime) . #dal 1980
  ?item rdfs:label ?label filter (lang(?label) = "it")
  ?item wdt:P495 wd:Q38 .


}

I get the list of books, but I can't find the right property to look for.

like image 578
Semantic_researcher Avatar asked Sep 06 '25 03:09

Semantic_researcher


1 Answers

Did you actually get The Name of the Rose in your example? Because its date of publication is set to 1980 (which is = 1980-01-01 for our purposes), I had to change your query to a >= comparison.

Then, using COUNT() and GROUP BY as mentioned in the comment gets you what you want. But if you really just need the number of sitselinks, there is a shortcut that may be useful. It was added because that number is often used as a good proxy for an item's popularity, and using the precomputed number is vastly more efficient than getting all links, grouping, and counting.

SELECT ?book ?bookLabel ?sitelinks ?date WHERE {
  VALUES ?type { wd:Q571 wd:Q47461344 wd:Q7725634 }
  ?book wdt:P31 ?type;
        wdt:P577 ?date;
        wdt:P495 wd:Q38;
        wikibase:sitelinks ?sitelinks.
  FILTER((?date >= "1980-01-01T00:00:00Z"^^xsd:dateTime) && (?date < "1981-01-01T00:00:00Z"^^xsd:dateTime))
  SERVICE wikibase:label { bd:serviceParam wikibase:language "it,en". }
}

Query

Note that the values may slightly differ from the version with group & count because here sites such as commons or wikiquote are also included.

like image 122
Matthias Winkelmann Avatar answered Sep 09 '25 17:09

Matthias Winkelmann