dahlia / wikidata

Wikidata client library for Python
https://pypi.org/project/Wikidata/
GNU General Public License v3.0
337 stars 31 forks source link

How do you get the value of an "instance" property? #38

Closed michaelmendoza42 closed 8 months ago

michaelmendoza42 commented 3 years ago

Hi, I'm new to wikidata in general.

I can't figure out how to get the start/end dates of someone who held office. For example, president George Washington (Q23), there is a property P39 (offices held) and within that I can get that he was a president (Q11696). But that item itself has no start/end date property (P580, P582). However, on the wikidata page there is definitely those properties for each office held.

In my code I'm doing something like:

p39 = client.get('P39')
george = client.get('Q23')
george.get(p39)

which returns <wikidata.entity.Entity Q11696 'President of the United States'> which is my dead end. this is the same object you'd get if i just did client.get('Q11696') because the item has no relation to the original item Q23.

Even the SPARQL to figure out the dates for a held office is kinda crazy. You have to do something like:

?pres p:P39 ?position_held_statement .
?position_held_statement ps:P39 wd:Q11696 .
 ?position_held_statement pq:P580 ?start .

any help is appreciated. thanks.

aGGre55or commented 8 months ago

Follow the algorithm: first the entity, then the property, and not vice versa.

client = Client()
entity = client.get('Q23', load=True)
p39 = client.get('P39')
print(entity[p39].label)