PAWS demo of qwikidata

qwikidata is a python package for interacting with wikidata entities.

the important links are,

lets pip install it and have some fun

!pip install qwikidata
Collecting qwikidata
Requirement already satisfied: requests in /srv/paws/lib/python3.6/site-packages (from qwikidata)
Collecting mypy-extensions (from qwikidata)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /srv/paws/lib/python3.6/site-packages (from requests->qwikidata)
Requirement already satisfied: certifi>=2017.4.17 in /srv/paws/lib/python3.6/site-packages (from requests->qwikidata)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /srv/paws/lib/python3.6/site-packages (from requests->qwikidata)
Requirement already satisfied: idna<2.9,>=2.5 in /srv/paws/lib/python3.6/site-packages (from requests->qwikidata)
Installing collected packages: mypy-extensions, qwikidata
Successfully installed mypy-extensions-0.4.3 qwikidata-0.4.0
import qwikidata 
from qwikidata.linked_data_interface import get_entity_dict_from_api
from qwikidata.entity import WikidataItem, WikidataProperty, WikidataLexeme
from qwikidata.sparql import get_subclasses_of_item, return_sparql_query_results

Linked Data Interface


# create an item representing "Douglas Adams"
q42_dict = get_entity_dict_from_api(Q_DOUGLAS_ADAMS)
q42 = WikidataItem(q42_dict)
WikidataItem(label=Douglas Adams, id=Q42, description=English writer and humorist, aliases=['Douglas Noel Adams', 'Douglas Noël Adams', 'Douglas N. Adams'], enwiki_title=Douglas Adams)
claims = q42.get_truthy_claim_groups()
p31_claims = claims['P31']
{'entity-type': 'item', 'numeric-id': 5, 'id': 'Q5'}


# create a property representing "subclass of"
p279_dict = get_entity_dict_from_api(P_SUBCLASS_OF)
p279 = WikidataProperty(p279_dict)
WikidataProperty(label=subclass of, id=P279, description=all instances of these items are instances of those items; this item is a class (subset) of that item. Not to be confused with P31 (instance of), aliases=['rdfs:subClassOf', 'hyponym of', 'has superclass', 'is also a', 'subtype of', 'is a subtype of', 'subcategory of', 'is a category of', 'is thereby also a', 'is necessarily also a', 'whose instances are among', 'whose instances ⊆ those of', '⊆', '⊂', 'is a type of', 'is a class of', 'subset of', 'type of', 'form of'])


# create a lexeme representing the english noun "bank"
L_BANK = "L3354"
l3354_dict = get_entity_dict_from_api(L_BANK)
l3354 = WikidataLexeme(l3354_dict)
WikidataLexeme(lemma=bank, id=L3354, language=Q1860, lexical_category=Q1084, forms=[WikidataForm(form_id=L3354-F1, representation=bank, grammatical_features=['Q110786']), WikidataForm(form_id=L3354-F2, representation=banks, grammatical_features=['Q146786'])], senses=[WikidataSense(sense_id=L3354-S1, gloss=financial institution), WikidataSense(sense_id=L3354-S2, gloss=dry ground next to a river), WikidataSense(sense_id=L3354-S3, gloss=shallow ocean area)])
[WikidataSense(sense_id=L3354-S1, gloss=financial institution),
 WikidataSense(sense_id=L3354-S2, gloss=dry ground next to a river),
 WikidataSense(sense_id=L3354-S3, gloss=shallow ocean area)]

Wikidata SPARQL Query Service

# send any sparql query to the wikidata query service and get full result back
# here we use an example that counts the number of humans
# NOTE this query can take a few minutes
sparql_query = """
SELECT (COUNT(?item) AS ?count)
        ?item wdt:P31/wdt:P279* wd:Q5 .
res = return_sparql_query_results(sparql_query)
{'head': {'vars': ['count']},
 'results': {'bindings': [{'count': {'datatype': '',
     'type': 'literal',
     'value': '5507432'}}]}}
# use convenience function to get subclasses of an item as a list of item ids
Q_RIVER = "Q4022"
subclasses_of_river = get_subclasses_of_item(Q_RIVER)