0

I'm trying to make a system where user can for example write name of the city, like Berlin and maybe he wants to get its population. So, i could not know what certain city he wants so here is a problem that i can't use certain code that (for example) Berlin has.

Like in a simple situation i think i could use this code, where code Q64 is Berlin

SELECT ?population
 WHERE { 
  wd:Q64 wdt:P1082 ?population
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
      }

But what if i dont know the code of Berlin, how can i find the correct city just by name? I know that sparql can filter years, another numbers but what about strings?

Andrew
  • 145
  • 1
  • 1
  • 6
  • you can also filter on strings. `contains`, `regex` etc. are functions on strings. But, what do you do with multiple matching entities? It's likely that you'll get more than one entity. Moreover, without some string indexing, those operations are pretty slow as the whole data has to be scanned – UninformedUser Nov 09 '19 at 16:28
  • For Wikidata, "at most 20 entities and their type(s) matching the token 'Berlin'": `SELECT * WHERE { SERVICE wikibase:mwapi { bd:serviceParam wikibase:api "EntitySearch" . bd:serviceParam wikibase:endpoint "www.wikidata.org" . bd:serviceParam mwapi:search "Berlin" . bd:serviceParam mwapi:language "en" . ?item wikibase:apiOutputItem mwapi:item . ?num wikibase:apiOrdinal true . } ?item (wdt:P279|wdt:P31) ?type } ORDER BY ASC(?num) LIMIT 20` – UninformedUser Nov 09 '19 at 16:34
  • You can see, there can be entities matching a term not being a city. So, you would also have to restrict the result to cities maybe. But even then, there are multiple cities called Berlin, not just the capital of Germany. The same holds for other cities as well as entities in general. – UninformedUser Nov 09 '19 at 16:35
  • at most 20 cities matching "Berlin": `SELECT * WHERE { SERVICE wikibase:mwapi { bd:serviceParam wikibase:api "EntitySearch" . bd:serviceParam wikibase:endpoint "www.wikidata.org" . bd:serviceParam mwapi:search "Berlin" . bd:serviceParam mwapi:language "en" . ?item wikibase:apiOutputItem mwapi:item . ?num wikibase:apiOrdinal true . } ?item (wdt:P279|wdt:P31)/wdt:P279* wd:Q515 } ORDER BY ASC(?num) LIMIT 20` – UninformedUser Nov 09 '19 at 16:38

0 Answers0