How to get Wikidata labels in more than one language? - sparql

I'm traying to get the regions of Italy in both Italian and English. I can get then in one laguage with this query...
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT DISTINCT ?RegionIT ?RegionITLabel ?ISO_code ?Geo
{
?RegionIT wdt:P31 wd:Q16110;
wdt:P300 ?ISO_code;
wdt:P625 ?Geo
SERVICE wikibase:label { bd:serviceParam wikibase:language "it" }
}
ORDER BY ?regionITLabel
... but adding another language using the standard SPARQL syntax doesn't work.

... but adding another language using the standard SPARQL syntax doesn't work.
How are you doing that? This works:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT DISTINCT ?RegionIT ?label (lang(?label) as ?label_lang) ?ISO_code ?Geo
{
?RegionIT wdt:P31 wd:Q16110;
wdt:P300 ?ISO_code;
wdt:P625 ?Geo ;
rdfs:label ?label
}
order by ?RegionIT
Link to try query
To limit to just Italian and English filter on the lang:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT DISTINCT ?RegionIT ?label ?ISO_code ?Geo
{
?RegionIT wdt:P31 wd:Q16110;
wdt:P300 ?ISO_code;
wdt:P625 ?Geo ;
rdfs:label ?label
filter(lang(?label) = 'it' || lang(?label) = 'en')
}
order by ?RegionIT
Link to try query
Obviously that multiplies the number of results, one for each language. If that's an issue you can do:
...
rdfs:label ?label_it , ?label_en
filter(lang(?label_it) = 'it' && lang(?label_en) = 'en')
...
which is effectively what the language service does.

Let's list all countries in English and Russian.
#List of countries in English and Russian
SELECT ?country ?label_en ?label_ru
WHERE
{
?country wdt:P31 wd:Q6256.
?country rdfs:label ?label_en filter (lang(?label_en) = "en").
?country rdfs:label ?label_ru filter (lang(?label_ru) = "ru").
}
SPARQL query
This example was taken from the tutorial Research in programming Wikidata, section "Countries".

Related

Query works on wikidata query service but not on Apache Jena copy

The query below is the accepted answer for my question Getting only english property value. When used on the Wikidata Query service try it! it will show shortNames for countries like Australia -> AUS and Austria -> AUT as requeted. Running the same query on my local Wikidata copy created a few weeks ago based on Apache Jena Fuseki the shortName column stays empty (see screenshot below).
What is the reason for the difference and how could the query be modified to also work with Apache Jena Fuseki?
# get a list countries with the corresponding ISO code
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
SELECT ?country ?countryLabel ?shortName (MAX(?pop) as ?population) ?coord ?isocode
WHERE
{
# instance of country
?country wdt:P31 wd:Q3624078.
OPTIONAL {
?country rdfs:label ?countryLabel filter (lang(?countryLabel) = "en").
}
OPTIONAL {
?country p:P1813 ?shortNameStmt. # get the short name statement
?shortNameStmt ps:P1813 ?shortName # the the short name value from the statement
filter (lang(?shortName) = "en") # filter for English short names only
filter not exists {?shortNameStmt pq:P31 wd:Q28840786} # ignore flags (aka emojis)
}
OPTIONAL {
# get the population
# https://www.wikidata.org/wiki/Property:P1082
?country wdt:P1082 ?pop.
}
# get the iso countryCode
{ ?country wdt:P297 ?isocode }.
# get the coordinate
OPTIONAL { ?country wdt:P625 ?coord }.
}
GROUP BY ?country ?countryLabel ?shortName ?population ?coord ?isocode
ORDER BY ?countryLabel
#UninformedUser's test query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
SELECT ?country ?shortNameStmt ?shortName WHERE
{
VALUES ?country {wd:Q40} ?country wdt:P31 wd:Q3624078.
OPTIONAL {
?country p:P1813 ?shortNameStmt.
?shortNameStmt ps:P1813 ?shortName filter (lang(?shortName) = "en")
filter not exists {?shortNameStmt pq:P31 wd:Q28840786}
}
}
Did not give a result on the truthy-based import of wikidata while it worked on the latest-all import. The same holds true for the full query. Still it would be good to know why the query does not work with the truthy dataset.

Get Wikidata entity descriptions via SPARQL, without Wikidata label service

I found this following code snippet on opendata.stackexchange.com, which returns name and description of citizens of the US from Wikidata:
prefix wdt: <http://www.wikidata.org/prop/direct/>
prefix wd: <http://www.wikidata.org/entity/>
PREFIX wikibase: <http://wikiba.se/ontology#>
SELECT ?Name ?itemDescription WHERE {
?item wdt:P27 wd:Q30 .
?item rdfs:label ?Name
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
LIMIT 3
The query can be evaluated at https://query.wikidata.org/
I am trying to get a description of a particular entity, for example Q3(life). But it in this case, the labelService does not return anything.
prefix wdt: <http://www.wikidata.org/prop/direct/>
prefix wd: <http://www.wikidata.org/entity/>
PREFIX wikibase: <http://wikiba.se/ontology#>
SELECT ?Name ?itemDescription WHERE {
wd:Q3 rdfs:label ?Name
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
LIMIT 3
EDIT: I am using Virtuoso and therefore cannot rely on Wikidata Label Service.
I am using
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX schema: <http://schema.org/>
SELECT ?o
WHERE
{
wd:Q3 schema:description ?o.
FILTER ( lang(?o) = "en" )
}
now, since I am querying a Virtuoso Server with Full-Text-Search capabilities, and it would be better to retrieve the description with other properties in one go.

List countries from DBpedia

Trying to query DBpedia for a list of all countries with the dbo:longName property and the capital of each country listed but get 0 results returned. Can't see whats wrong with the query.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?country a dbo:longName ;
dbo:capital ?capital .
}
What's missing?
You are missing that ?country has a rdf:type of dbo:Country and not dbo:longName. The right query should be:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?x a dbo:Country.
?x dbo:longName ?country.
?x dbp:capital ?capital
}
Update
Based on your comment you want the URI's of the country and their capital. Therefore, you don't need dbo:longName, because you don't want the country label name. You will select the instances:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?country a dbo:Country.
?country dbo:capital ?capital
}
Note that results will bring countries that are extinct. If you want to filter countries that have ended, you should get this result with:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?country a dbo:Country.
?country dbo:capital ?capital.
FILTER NOT EXISTS { ?country dbo:dissolutionYear ?yearEnd }
}

Finding resource label

I'm currently learning SPARQL, and I'm exploring the data from dbpedia. Why does this query work:
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT
?label
WHERE {
dbr:Leipzig rdfs:label ?label.
} LIMIT 20
But this does not (i.e. id does not return anything)
PREFIX geo: <https://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT
?label ?lat
WHERE {
dbr:Leipzig rdfs:label ?label.
dbr:Leipzig geo:lat ?lat.
} LIMIT 20
Because the protocol of the WGS 84 Geo namespace is http and not https, i.e. http://www.w3.org/2003/01/geo/wgs84_pos#

How do get city population by name with SPARQL

I need to get city population by its name. I'm sure SPARQL could provide this, but I'm not sure how to write the query. I have a query which provides COuntry's capital and coordinates by it's name, so I assume it should be something similar. HELP!
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX p: <http://dbpedia.org/property/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?country ?population ?capital ?lat ?long WHERE {
?country a o:Country ; foaf:name "Germany"#en; o:capital [ geo:lat ?lat ; geo:long ?long ; p:name ?capital ]
}
All you need is:
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX p: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?pop WHERE {
?country a o:Country ;
foaf:name ?name ;
p:populationEstimate ?pop .
}
The only tricky part was finding out the name of the property that links Countries and Populations.