1

I'm trying to get the list of all properties of a specific wikidata entity, starting from its id (e.g. Q39659506). I've tried to make some requests at wikidata's API using wbsearchentities and wbgetentities, but computations are too slow, due to the amount of requests and the necessary data preprocessing to obtain a Pandas Dataframe that lists properties and values like the following:

Example of the dataframe of wikidata properties I am to get

I've also tried the wikidata library for python that iterates over properties, but it is too slow compared to SPARQL. I am therefore writing this post to ask for suggestions on how to build the query such that I get properties URL, label and values URL (when available) and values, with quantities unit attached. Till now I've only been able to get the list of properties whose values are entities themselves, but not quantities or dates. You can find the query below:

# Specifying we get our info from entity DB
PREFIX entity: <http://www.wikidata.org/entity/> 

SELECT  ?propUrl ?propLabel ?valUrl ?valLabel ?valDesc
WHERE {     
  # Select the entity
  entity:Q39659506 ?propUrl ?valUrl .       
  # Specify the property
  ?property ?ref ?propUrl;
            rdf:type wikibase:Property;
            rdfs:label ?propLabel . 
  # Gather values label and description
  ?valUrl rdfs:label ?valLabel;
          schema:description ?valDesc.
  # Only english language
  FILTER (lang(?valLabel) = 'en') .
  FILTER (lang(?propLabel) = 'en' ) 
  FILTER (lang(?valDesc) = 'en' ) 
} 

Thank you so much!

  • your losing the lierals because they don't have labels. Either you put the label part into an `OPTIONAL` clause or you make use of Wikidata label service: `# Specifying we get our info from entity DB SELECT ?propUrl ?propLabel ?valUrl ?valLabel ?valDesc ?pq ?pqv WHERE { wd:Q39659506 ?propUrl ?valUrl. ?property ?ref ?propUrl; rdf:type wikibase:Property; SERVICE wikibase:label { ?valUrl rdfs:label ?valLabel . ?valUrl schema:description ?valDesc. ?property rdfs:label ?propLabel. bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }` – UninformedUser Jun 23 '22 at 05:31
  • Note, for getting units check https://stackoverflow.com/questions/70546557/wikidata-get-all-properties-of-an-item-with-units and for getting also statement qualifiers you could look at https://stackoverflow.com/questions/46383784/wikidata-get-all-properties-with-labels-and-values-of-an-item – UninformedUser Jun 23 '22 at 05:32
  • more compact: `SELECT ?wd ?wdLabel ?ps_Label ?ps_Description WHERE { VALUES (?item) {(wd:Q39659506)} ?item ?p ?statement . ?statement ?ps ?ps_ . ?wd wikibase:claim ?p. ?wd wikibase:statementProperty ?ps. SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }` – UninformedUser Jun 23 '22 at 05:38
  • 1
    Thank you so much for your answers! The first query suggested is helpful, but I will only consider wdt properties, while in the last one the ID of entities is missing, therefore I think I'll try merging them. For gathering units, I'll check the suggested links, thank you all! – Aurora Arctic Jun 23 '22 at 09:25
  • if you have more issues or questions, feel free to extend your question here. Once you got the answer, please post it here and accept your own answer. Will help others – UninformedUser Jun 24 '22 at 06:12
  • I've tried to integrate your answers with the answer in the post about units, but it seems they refer to quantity units of the statements, not about values. For instance, if Yara Birkeland (https://www.wikidata.org/wiki/Q39659506) is 79.5 metres long, I would like to keep metres in an additional column. Till now I've tried the following query, but something's wrong because of row duplication and some missing units: – Aurora Arctic Jul 08 '22 at 15:36
  • ```SELECT ?p ?wdLabel ?ps ?ps_ ?ps_Label ?ps_Description ?pq_unitLabel WHERE { VALUES ?item { wd:Q39659506 } ?item ?p ?statement. ?statement ?ps ?ps_. ?wd wikibase:claim ?p; wikibase:statementProperty ?ps. OPTIONAL { ?statement ?pq ?pq_. ?wdpq wikibase:qualifier ?pq. ?statement ?pqv [wikibase:quantityUnit ?pq_unit] } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }``` – Aurora Arctic Jul 08 '22 at 15:38

0 Answers0