Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wikidata entity value from name

Is there a way to get Wikidata page information based on the name of the entity for example if I wanted to get page data for Google. I think it has to be done using "entity" with the corresponding entity value however I am not sure of there is any easy way to determine the entity value.

like image 637
Oroku Avatar asked Dec 12 '14 21:12

Oroku


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 identifier in Wikidata?

Each Wikidata entity is identified by an entity ID, which is a number prefixed by a letter. items, also known as Q-items, are prefixed with Q (e.g. Q12345 ), properties are prefixed by P (e.g. P569 ) and. lexemes are prefixed by L (e.g. L1 ).


3 Answers

If you want to do this using the API, you would first use wbsearchentities to find out which entity do you want. For example:

https://www.wikidata.org/w/api.php?action=wbsearchentities&search=Google&language=en

The problem with this is that there are multiple entities called "Google": the company (Google Inc.), the search engine (Google Web Search), the verb (to google) and even a Wikipedia disambiguation page.

After you somehow decide which entity to access, use wbgetentities to actually get the information you want:

https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q95&languages=en

Or, if you can't decide which entity to use, you could get information for all of them at the same time:

https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q95|Q9366|Q961680|Q1156923&languages=en

like image 89
svick Avatar answered Sep 30 '22 02:09

svick


If you are familiar with Python you could do it programmatically with the Wikidata api, using Pywikibot The following python script, obtains the wikidata entities. If you want the data objects for each individual wikidata entity, you need to uncomment the last two lines

 from pywikibot.data import api
 import pywikibot
 import pprint

 def getItems(site, itemtitle):
     params = { 'action' :'wbsearchentities' , 'format' : 'json' , 'language' : 'en', 'type' : 'item', 'search': itemtitle}
     request = api.Request(site=site,**params)
     return request.submit()

 def getItem(site, wdItem, token):
    request = api.Request(site=site,
                          action='wbgetentities',
                          format='json',
                          ids=wdItem)    
    return request.submit()

def prettyPrint(variable):
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(variable)

# Login to wikidata
site = pywikibot.Site("wikidata", "wikidata")
repo = site.data_repository()
token = repo.token(pywikibot.Page(repo, 'Main Page'), 'edit')
wikidataEntries = getItems(site, "Google")
# Print the different Wikidata entries to the screen
prettyPrint(wikidataEntries)

# Print each wikidata entry as an object
#for wdEntry in wikidataEntries["search"]:
#   prettyPrint(getItem(site, wdEntry["id"], token))

which results in

{   u'search': [   {   u'aliases': [u'Google Inc.'],
                       u'description': u'American multinational Internet and technology corporation',
                       u'id': u'Q95',
                       u'label': u'Google',
                       u'url': u'//www.wikidata.org/wiki/Q95'},
                   {   u'aliases': [u'Google Search', u'Google Web Search'],
                       u'description': u'Internet search engine developed by Google, Inc.',
                       u'id': u'Q9366',
                       u'label': u'Google',
                       u'url': u'//www.wikidata.org/wiki/Q9366'},
                   {   u'description': u'Wikipedia disambiguation page',
                       u'id': u'Q961680',
                       u'label': u'Google',
                       u'url': u'//www.wikidata.org/wiki/Q961680'},
                   {   u'aliases': [u'Google'],
                       u'description': u'verb',
                       u'id': u'Q1156923',
                       u'label': u'google',
                       u'url': u'//www.wikidata.org/wiki/Q1156923'},
                   {   u'id': u'Q10846831',
                       u'label': u'google',
                       u'url': u'//www.wikidata.org/wiki/Q10846831'},
                   {   u'aliases': [u'Google Android'],
                       u'description': u'operating system for mobile devices created by Google',
                       u'id': u'Q94',
                       u'label': u'Android',
                       u'url': u'//www.wikidata.org/wiki/Q94'},
                   {   u'description': u'web browser developed by Google',
                       u'id': u'Q777',
                       u'label': u'Google Chrome',
                       u'url': u'//www.wikidata.org/wiki/Q777'}],
    u'searchinfo': {   u'search': u'Google'},
    u'success': 1}
like image 25
Andra Avatar answered Sep 30 '22 00:09

Andra


Maybe you can use sparql, to run a query:

SELECT ?item WHERE {
  ?item rdfs:label "Google"@en
}

You can use in python using pywikibot:

 import pywikibot
 from pywikibot import pagegenerators, WikidataBot

 sparql = "SELECT ?item WHERE { ?item rdfs:label 'Google'@en }"
 entities = pagegenerators.WikidataSPARQLPageGenerator(sparql, site=repo)
 entities = list(entities)
like image 31
dafnahaktana Avatar answered Sep 30 '22 01:09

dafnahaktana