Get Wikidata entity descriptions via SPARQL, without Wikidata label service - sparql

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.

Related

Is querying the pagerank value on DBpedia no longer available?

Since the Virtuoso version is updated recently, when I try to use the query below on the public DBpedai SPARQL endpoint. The result is empty.
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbo:<http://dbpedia.org/ontology/>
PREFIX vrank:<http://purl.org/voc/vrank#>
SELECT ?s ?v
FROM <http://dbpedia.org>
FROM <http://people.aifb.kit.edu/ath/#DBpedia_PageRank>
WHERE {
?s rdf:type dbo:University.
?s vrank:hasRank/vrank:rankValue ?v.
}
ORDER BY DESC(?v) LIMIT 50
Thank you!
There is also the option to query the Wikidata endpoint via query federation and map the result to DBpedia URIs:
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX vrank: <http://purl.org/voc/vrank#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX schema: <http://schema.org/>
SELECT * WHERE {
{
SELECT DISTINCT ?uni (URI(REPLACE(str(?article), "https://en.wikipedia.org/wiki", "http://dbpedia.org/resource")) as ?dbpedia) WHERE {
SERVICE <https://query.wikidata.org/sparql> {
?uni wdt:P31/wdt:P279* wd:Q3918.
?article schema:about ?uni .
?article schema:inLanguage "en" .
?article schema:isPartOf <https://en.wikipedia.org/> .
}
}
}
OPTIONAL {
?uni vrank:pagerank ?rank.
}
} ORDER BY desc(?rank) LIMIT 50
Just run the HDT file with the Wikidata PageRank scores locally as explained on https://github.com/athalhammer/danker-hdt-docker. The result is cleaner (no resources such as dbpedia:Physics in the result set) and the query runs in < 3 seconds.

SPARQL Wikidata - Retrieve members of Wikimedia Category

I want to retrieve the members of the Wikimedia Category American rock singers. I must use Wikidata and it tells me to use P31 with Q4167836 . The query below only returns information about the Category page and not about its members.
Is there any other way to retrieve the members of the Wikimedia Category? I also tried using the term dcterms:subject, which I use successfully with DBpedia, but in Wikidata the query returns empty results.
prefix wdt: <http://www.wikidata.org/prop/direct/>
prefix wd: <http://www.wikidata.org/entity/>
prefix wikibase: <http://wikiba.se/ontology#>
prefix bd: <http://www.bigdata.com/rdf#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?subject ?predicate ?object ?objectLabel WHERE {
?subject ?predicate ?object ; wdt:P31 wd:Q4167836 ;
rdfs:label "Category:American rock singers"#en
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .}
}
Try here
Try this query on Wikidata:
SELECT ?subjectLabel WHERE {
?subject wdt:P27 wd:Q30;
wdt:P136 wd:Q11399 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .}
}
Or try this query on DBpedia (as Mark Miller linked to):
SELECT str(?label) WHERE {
?subject dct:subject dbc:American_rock_singers ;
rdfs:label ?label .
FILTER (lang(?label) = "en")
}
Or if you must use Wikidata, try this federated query on Wikidata:
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX dct: <http://purl.org/dc/terms/>
SELECT ?wsubjectLabel WHERE {
SERVICE <http://dbpedia.org/sparql> {
?subject dct:subject dbc:American_rock_singers .
?subject owl:sameAs ?wsubject .
FILTER (STRSTARTS(STR(?wsubject), "http://www.wikidata.org"))
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

How to get Wikidata labels in more than one language?

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".

Wikidata SPARQL Query

On Wikidata's SPARQL endpoint, I want to find all the universities where someone was employed as a university teacher. So far, I managed to get the triples of type (person, position helf, employer) with the following query:
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX v: <http://www.wikidata.org/prop/statement/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
SELECT DISTINCT ?s ?sp ?q ?sLabel ?spLabel ?qLabel WHERE {
?s p:P39 ?p .
?p v:P39 ?sp .
?p pq:P108 ?q .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
This query returns all the positions held by that person. In my case, I want to limit myself to the held position of university teacher (wd:Q1622272).
If possible, I would ask for a simplified query.
Replace the variable ?sp with the specific position you want (i.e. wd:Q1622272) in the triple pattern ?p v:P39 ?sp .:
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX v: <http://www.wikidata.org/prop/statement/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
SELECT DISTINCT ?s ?q ?sLabel ?qLabel WHERE {
?s p:P39 ?p .
?p v:P39 wd:Q1622272 . # Here
?p pq:P108 ?q .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}

Filtering DBpedia disambiguation page

I have a SPARQL Query, and I want to eliminate all disambigution resources. How can I do this? This is my query:
prefix rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
select distinct ?Nom ?resource ?url where {
?resource rdfs:label ?Nom.
?resource foaf:isPrimaryTopicOf ?url.
FILTER (langMatches( lang(?Nom), "EN" )).
?Nom <bif:contains> "Apple".
}
You can add the following prefix and filter to your query:
prefix dbo: <http://dbpedia.org/ontology/>
filter not exists {
?resource dbo:wikiPageRedirects*/dbo:wikiPageDisambiguates ?dis
}
This says to exclude resources and resources that redirect to a resources that disambiguate some articles. That gives you a query like this:
prefix rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix dbo: <http://dbpedia.org/ontology/>
select distinct ?Nom ?resource ?url where {
?resource rdfs:label ?Nom.
?resource foaf:isPrimaryTopicOf ?url.
FILTER (langMatches( lang(?Nom), "EN" )).
?Nom <bif:contains> "Apple".
filter not exists {
?resource dbo:wikiPageRedirects*/dbo:wikiPageDisambiguates ?dis
}
}
SPARQL results
Now, even though that removes all the disambiguation pages, you may still have results that include "disambiguation" in the title. For instance, one of the results is:
The Little Apple (disambiguation)"#en
http://dbpedia.org/resource/The_Little_Apple_(disambiguation)
Even though that has "disambiguation" in the name, it's not a disambiguation page. It doesn't have any values for dbo:wikiPageDisambiguates. it does redirect to another page, though. You may want to filter out things that redirect to something else, too. You can modify the filter though:
filter not exists {
?resource dbo:wikiPageRedirects|dbo:wikiPageDisambiguates ?dis
}
That says to filter out any resource that either redirects to something, or that disambiguates something. This is actually a simpler filter, really. This makes your query:
prefix rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix dbo: <http://dbpedia.org/ontology/>
select distinct ?Nom ?resource ?url where {
?resource rdfs:label ?Nom.
?resource foaf:isPrimaryTopicOf ?url.
FILTER (langMatches( lang(?Nom), "EN" )).
?Nom <bif:contains> "Apple".
filter not exists {
?resource dbo:wikiPageRedirects|dbo:wikiPageDisambiguates ?dis
}
}
SPARQL results