Hackety hack hack

Atypisches Nutzerverhalten mit Rat und Tat.

Gotta catch 'em all if they are prime

Pokemon collection

Pokémon, as Wikipedia tells us, is a multi-billion dollar media franchise controlled by video game giant Nintendo, and created by Satoshi Tajiri around 1995. Interestingly enough, Pokémon has had quite an impact on the Wikipedia project, with the completeness of Pokémon characters resulting in the Pokémon test, so named after editors used similar arguments to try to “keep” other articles—if [named Pokemon character] has an article, why shouldn’t this topic? For example, in arguing against the deletion of an article in 2005, one editor wrote “Wikipedia is not paper; if we can have articles for every minor character in Star Wars, Star Trek, and each of those pesky Pokémon, we can have an article about Professor Hopper.” (See the article on the Wikimedia blog for the full story).

I played around with the Wikidata Query Service and SPARQL queries recently. As one of the first projects on Wikidata was WikiProject Pokémon (initially known as the Pokémon Task Force), information on the fictional Pokémon species and their numbers (the Pokédex numbers) is rather complete. A query with SPARQL for all Pokémon is easy:

 
 
 
 
 
 
 
 
# Gotta catch 'em all
SELECT ?pokemon ?pokemonLabel ?pokedexNumber
WHERE
{
  ?pokemon wdt:P31 wd:Q3966183.
     ?pokemon wdt:P1112 ?pokedexNumber
SERVICE wikibase:label { bd:SERVICEParam wikibase:language "en" }
} ORDER BY (?pokedexNumber)

You can try out this query.

But I wanted a query that was a little more complex: Find all Pokémon with a number in the Pokédex that is prime. My first try didn’t work. The mistake I made: I wanted to compare the Pokédex number with the label of a prime number. Unfortunately, the label service in the Wikidata Query Service fills in labels too late to use them in a subquery. But the community was helpful as ever. User:Nikki pointed me in the correct direction: Use rdfs:label instead. Here’s the solution:

 
 
 
 
 
 
 
 
 
 
 
 
 
# Gotta catch 'em all if they are prime
SELECT ?pokemon ?pokemonLabel ?pokedex WHERE {
{ 
  SELECT ?prime WHERE {
    ?p wdt:P31 wd:Q49008;
   rdfs:label ?prime FILTER (lang(?prime) = "en") .
         }
  }
  ?pokemon wdt:P31 wd:Q3966183;
   wdt:P1112 ?pokedex FILTER (xsd:integer(?pokedex) = xsd:integer(?prime)).
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }.
}
ORDER BY ?pokedex

Try it out

But there’s an even simpler solution without a subquery that is obvious once you wrape your head around the fact that query doesn’t work like map in functional languages but is rather closer to declarative programming. It’s basically: “Find a solution that satisfies all the triples given above”.

See it here:

 
 
 
 
 
 
 
 
 
 
 
# Gotta catch 'em all if they are prime
SELECT ?pokemon ?pokemonLabel ?pokedex WHERE {
?prime wdt:P31 wd:Q49008;
   wdt:P1181 ?pokedex .
      
?pokemon wdt:P31 wd:Q3966183;
 wdt:P1112 ?pokedex .

SERVICE wikidatabase:label { bd:serviceParam wikibase:language "en". }.
}
ORDER BY ?pokedex

Try out this

Thanks again, User:Nikki!