0

My problem: I'm writing an NLP program in python and I need to get the entity ID for properties and lexemes. So what I basically want is, e.g. if the input is the word/property "father" I want the return value to be "P22" (property number for father). I already know some methods for getting the Q-number (see below).

from requests import get
def get_qnumber(wikiarticle, wikisite):
    resp = get('https://www.wikidata.org/w/api.php', {
        'action': 'wbgetentities',
        'titles': wikiarticle,
        'sites': wikisite,
        'props': '',
        'format': 'json'
    }).json()
    return list(resp['entities'])[0]

print(get_qnumber(wikiarticle="Andromeda Galaxy", wikisite="enwiki"))

And I thought getting the P and L-numbers would look something similar, but finding the lexeme and property number seems to be much trickier.

What I've tried: The closest thing I've found is manually searching for ID numbers with https://www.wikidata.org/wiki/Special:Search And put a "P:" and "L:" in the search string.

I also found some code for SPARQL but it was slow and I don't know how to refine the search to exclude unrelated search results.

query = """
SELECT ?item
WHERE
{
  ?item rdfs:label "father"@en
}
"""

I'm a total noob about this and haven't found any info google. So am I approaching this thing completely wrong or am I missing something really obvious?

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58

1 Answers1

3

Use action=wbsearchentities with type=property or type=lexeme:

import requests
params = dict (
        action='wbsearchentities',
        format='json',
        language='en',
        uselang='en',
        type='property',
        search='father'
        )

response = requests.get('https://www.wikidata.org/w/api.php?', params).json() 
print(response.get('search')[0]['id'])

repl.it

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58