how to programmatically get all available information from a Wikidata entity?

怎甘沉沦 提交于 2021-02-08 06:52:22

问题


I'm really new to wikidata. I just figured that wikidata uses a lot of reification.

Suppose we want to get all information available for Obama. If we are going to do it from DBpedia, we would just use a simple query: select * where {<http://dbpedia.org/resource/Barack_Obama> ?p ?o .} This would return all the properties and values with Obama being the subject. Essentially the result is the same as this page: http://dbpedia.org/page/Barack_Obama while the query result is in a format I needed.

I'm wondering how to do the same thing with Wikidata. This is the Wikidata page for Obama: https://www.wikidata.org/wiki/Q76. Let's say I want all the statements on this page. But almost all the statements on this page are reified in that they have ranks and qualifiers, etc. For example, for the "educated at" part, it not only has the school, but also the "start time" and "end time" and all schools are ranked as normal since Obama is not in these schools anymore.

I could just get all the schools by getting the truthy statements (using https://query.wikidata.org):

SELECT ?school ?schoolLabel WHERE {
wd:Q76 wdt:P69 ?school .
   SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
   }
 }

The above query will simple return all the schools.

If I want to get the start time and end time of the school, I need to do this:

SELECT ?school ?schoolLabel ?start ?end WHERE {
wd:Q76 p:P69 ?school_statement .
?school_statement ps:P69 ?school .
?school_statement pq:P580 ?start .
?school_statement pq:P582 ?end .
   SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
   }
 }

But the thing is, without looking at the actual page, how would I know that the ?school_statement has pq:P580 and pq:P582, namely the "start time" and "end time"? And it all comes down to a question that how do I get all the information (including reification) from https://www.wikidata.org/wiki/Q76?

Ultimately, I would expect a table like this: ||predicate||object||objectLabel||qualifier1||qualifier1Value||qualifier2||qualifier2Value||...


回答1:


you should probably go for the Wikidata data API (more specifically the wbgetentities module) instead of the SPARQL endpoint:

In your case: https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&ids=Q76

You should find all the qualifiers data you where looking for: example with entities.Q76.claims.P69.1

{ mainsnak: 
   { snaktype: 'value',
     property: 'P69',
     datavalue: 
      { value: { 'entity-type': 'item', 'numeric-id': 3273124, id: 'Q3273124' },
        type: 'wikibase-entityid' },
     datatype: 'wikibase-item' },
  type: 'statement',
  qualifiers: 
   { P580: 
      [ { snaktype: 'value',
          property: 'P580',
          hash: 'a1db249baf916bb22da7fa5666d426954435256c',
          datavalue: 
           { value: 
              { time: '+1971-01-01T00:00:00Z',
                timezone: 0,
                before: 0,
                after: 0,
                precision: 9,
                calendarmodel: 'http://www.wikidata.org/entity/Q1985727' },
             type: 'time' },
          datatype: 'time' } ],
     P582: 
      [ { snaktype: 'value',
          property: 'P582',
          hash: 'a065bff95f5cb3026ebad306b3df7587c8daa2e9',
          datavalue: 
           { value: 
              { time: '+1979-01-01T00:00:00Z',
                timezone: 0,
                before: 0,
                after: 0,
                precision: 9,
                calendarmodel: 'http://www.wikidata.org/entity/Q1985727' },
             type: 'time' },
          datatype: 'time' } ] },
  'qualifiers-order': [ 'P580', 'P582' ],
  id: 'q76$464382F6-E090-409E-B7B9-CB913F1C2166',
  rank: 'normal' }

Then you might be interesting in ways to extract readable results from those results



来源:https://stackoverflow.com/questions/38906932/how-to-programmatically-get-all-available-information-from-a-wikidata-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!