8

Is there a way to extract the labels of the statements (property and object) in Wikidata? I want to get all the information that there is here:https://www.wikidata.org/wiki/Q42, properties and objects with labels so both (wikidata.org/wiki/Property:P21, sex or gender) and (wikidata.org/wiki/Q6581097, male).

I've tried to do this using the wiki data toolkit, but I can't find a way to avoid doing multiple requests to the API (I'm now getting ids from the Q42 entities and then, for each of these, I send an API request to get the label.)

The problem is the same I've found in these two questions:

How to get the name of a Wikidata item

https://stackoverflow.com/questions/31266398/getting-readable-results-from-wikidata

There's a way to get all the information when I retrieve the data for the Q42 entity?

4

3 Answers 3

5

You could do it with SPARQL:

SELECT ?wdLabel ?ooLabel
WHERE {
  VALUES (?s) {(wd:Q42)}
  ?s ?wdt ?o .
  ?wd wikibase:directClaim ?wdt .
  ?wd rdfs:label ?wdLabel .
  OPTIONAL {
    ?o rdfs:label ?oLabel .
    FILTER (lang(?oLabel) = "en")
  }
  FILTER (lang(?wdLabel) = "en")
  BIND (COALESCE(?oLabel, ?o) AS ?ooLabel)
 } ORDER BY xsd:integer(STRAFTER(STR(?wd), "http://www.wikidata.org/entity/P"))

Try it.

Perhaps the problem was that Wikidata supplies labels for entities in the wd: namespace only.

In order to provide labels for properties in the wdt: namespace, one have to use the special predicate wikibase:directClaim, which connects the wd: namespace entity for the property to its wdt: namespace representation.

2

You could try this one:

https://www.wikidata.org/wiki/Special:EntityData/Q42.ttl?flavor=full

Which generates RDF description of Q42 containing most of what you need I think (also available as .nt or .rdf). Be careful with it though - it can produce a lot of info, for Q42 it's over 1M but can be even more.

You can make more specific query using the SPARQL interface but I don't understand the requirements enough to write an actual query.

0

This may help.

There is a python package called wikidata using which you can query.

Here is a sample : for entity 'Q5' you can check the label/description in eng(or other language) as shown below:

from wikidata.client import Client
client = Client()  # doctest: +SKIP
entity = client.get('Q5', load=True)
>>> entity.label.texts['en']
'human'

>>> entity.description.texts['en']
m'common name of Homo sapiens'

image_prop = client.get('P18')
image = entity[image_prop]
>>> image
<wikidata.commonsmedia.File 'File:KBS "The Producers" press conference, 11 May 2015 10.jpg'>
>>> image.image_resolution
(820, 1122)
>>> image.image_url
'https://upload.wikimedia.org/wikipedia/commons/6/60/KBS_%22The_Producers%22_press_conference%2C_11_May_2015_10.jpg'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.