Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query Wikidata items using its labels?

How can I query Wikidata to get all items that have labels contain a word? I tried this but didn't work; it retrieved nothing.

SELECT ?item ?itemLabel WHERE {
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en".
    ?item rdfs:label ?itemLabel.  
  }
FILTER(CONTAINS(LCASE(?itemLabel), "keyword"))
}
LIMIT 1000
like image 649
fattah.safa Avatar asked Jul 22 '16 13:07

fattah.safa


People also ask

How do I get data from wikidata?

You can query the data in Wikidata through our SPARQL endpoint, the Wikidata Query Service. The service can be used both as an interactive web interface, or programmatically by submitting GET or POST requests to https://query.wikidata.org/sparql .

What is label in wikidata?

The label is the most common name that the item would be known by. It does not need to be unique, in that multiple items can have the same label, however no two items may have both the same label and the same description.

How do you use wikidata query service?

In the query.wikidata.org query editor, you can press Ctrl + Space (or Alt + Enter or Ctrl + Alt + Enter ) at any point in the query and get suggestions for code that might be appropriate; select the right suggestion with the up / down arrow keys, and press Enter to select it.

How do you write queries in Sparql?

Structure of a SPARQL Query A SPARQL query comprises, in order: Prefix declarations, for abbreviating URIs. Dataset definition, stating what RDF graph(s) are being queried. A result clause, identifying what information to return from the query.


2 Answers

Following your question and the useful comments provided, I ended up with this query

SELECT ?item ?itemLabel
WHERE { 
  ?item rdfs:label ?itemLabel. 
  FILTER(CONTAINS(LCASE(?itemLabel), "city"@en)). 
} limit 10

For which I got those results

item          itemLabel
wd:Q515       city
wd:Q7930989   city
wd:Q15253706  city
wd:Q532039    The Eternal City
wd:Q1969820   The Eternal City
wd:Q3986838   The Eternal City
wd:Q7732543   The Eternal City
wd:Q7737016   The Golden City
wd:Q5119      capital city
wd:Q1555      Guatemala City

try it here

like image 60
innovimax Avatar answered Dec 12 '22 17:12

innovimax


Yes, you can search by label, for example:

SELECT distinct ?item ?itemLabel ?itemDescription WHERE{  
  ?item ?label "Something"@en.  
  ?article schema:about ?item .
  ?article schema:inLanguage "en" .
  ?article schema:isPartOf <https://en.wikipedia.org/>. 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }    
}

see it on Query page.

like image 31
Alexan Avatar answered Dec 12 '22 17:12

Alexan