Creating properties with statements using Wikidata Integrator

July 16, 2023 0 By addshore

Wikidata Integrator is a Python library that simplifies data integration from Wikidata (and other Wikibases). It is written in Python, is focused on Wikibase concepts (as opposed to some libraries which are MediaWiki focused) and has a user-friendly interface.

I’m currently working on a demo Wikibase and decided to bring all of the data into the Wikibase making use of a Jupyter notebook, and Wikidata integrator was my choice library to use for this task. (Jupyter notebooks are interactive coding environments that allow users to create and share documents containing live code, visualizations, and explanations.)

Along that journey I found the Wikidata Integrator documentation lacking slightly, but I managed to get initial property and item creation working with little effort. However, I couldn’t get properties to create with statements already on them (needed a subsequent edit instead).

After searching around and reaching out to some other Wikidata Integrator users I ended up in the small telegram channel (which you can find linked on the GitHub repo) where Andra showed me some code examples that I had already seen, but pointed out the part adding statements.

Maybe this is just for properties (I’m yet to fully experiment with items as part of Wikidata Integrator), but if you find yourself unable to create properties with statements, the key part is to add them to the data element on initial object creation, rather than appending them to the statements attribute of the object.

So instead of this…

property = wdi_core.WDItemEngine(mediawiki_api_url=wb_endpoint, data=s)
property.set_label("Wikidata: " + property_label)
property.statements.append(wdi_core.WDString("wikidata", prop_nr=source_pid))
property.statements.append(wdi_core.WDString(property_id, prop_nr=source_id_pid))Code language: JavaScript (javascript)

You want something like this…

statements = [
    wdi_core.WDString("wikidata", prop_nr=source_pid),
    wdi_core.WDString(property_id, prop_nr=source_id_pid)
property = wdi_core.WDItemEngine(mediawiki_api_url=wb_endpoint, data=statements)
property.set_label("Wikidata: " + property_label)

property.write(login=wb_login,entity_type='property', property_datatype='string')Code language: JavaScript (javascript)