WikibaseIntegrator
A Python module to manipulate data on a Wikibase instance (like Wikidata) through the MediaWiki Wikibase API and the Wikibase SPARQL endpoint.
Install / Use
/learn @LeMyst/WikibaseIntegratorREADME
Wikibase Integrator
Wikibase Integrator is a python package whose purpose is to manipulate data present on a Wikibase instance (like Wikidata).
Breaking changes in v0.12
A complete rewrite of the WikibaseIntegrator core has been done in v0.12 which has led to some important changes.
It offers a new object-oriented approach, better code readability and support for Property, Lexeme and MediaInfo entities (in addition to Item).
If you want to stay on v0.11.x, you can put this line in your requirements.txt:
wikibaseintegrator~=0.11.3
<!-- ToC generator: https://luciopaiva.com/markdown-toc/ -->
- WikibaseIntegrator / WikidataIntegrator
- Documentation
- Jupyter notebooks
- Common use cases
- Read an existing entity
- Start a new entity
- Write an entity to instance
- Add labels
- Get label value
- Add aliases
- Add descriptions
- Add a simple claim
- Get claim value
- Manipulate claim, add a qualifier
- Manipulate claim, add references
- Remove a specific claim
- Get lemma on lexeme
- Set lemma on lexeme
- Add gloss to a sense on lexeme
- Add form to a lexeme
- Common use cases
- Other projects
- Jupyter notebooks
- Installation
- Installation of the development environment
- Using a Wikibase instance
- The Core Parts
- More than Wikibase
- Helper Methods
- Examples (in "normal" mode)
- Examples (in "fast run" mode)
- Debugging
WikibaseIntegrator / WikidataIntegrator
WikibaseIntegrator (wbi) is a fork of WikidataIntegrator (wdi) whose purpose is to focus on an improved compatibility with Wikibase and adding missing functionalities. The main differences between these two libraries are :
- A complete rewrite of the library with a more object-oriented architecture allowing for easy interaction, data validation and extended functionality
- Add support for reading and writing Lexeme, MediaInfo and Property entities
- Python 3.10 to 3.14 support, validated with unit tests
- Type hints implementation for arguments and return, checked with mypy static type checker
- Add OAuth 2.0 login method
- Add logging module support
But WikibaseIntegrator lack the "fastrun" functionality implemented in WikidataIntegrator.
Documentation
A (basic) documentation generated from the python source code is available on the Read the Docs website.
Jupyter notebooks
You can find some sample code (adding an entity, a lexeme, etc.) in the Jupyter notebook directory of the repository.
Common use cases
Read an existing entity
Get the entity with the QID Q582 from the instance.
From import_entity.ipynb
entity = wbi.item.get('Q582')
Start a new entity
Start a new local entity.
entity = wbi.item.new()
Write an entity to instance
Write a local entity to the instance.
From import_entity.ipynb
entity.write()
Add labels
Add an English and a French label to the local entity.
entity.labels.set('en', 'New item')
entity.labels.set('fr', 'Nouvel élément')
Get label value
Get the english label value of the local entity.
From item_get.ipynb
entity.labels.get('en').value
Add aliases
Add an English and a French alias to the local entity.
entity.aliases.set('en', 'Item')
entity.aliases.set('fr', 'Élément')
Add descriptions
Add an English and a French description to the local entity.
entity.descriptions.set('en', 'A freshly created element')
entity.descriptions.set('fr', 'Un élément fraichement créé')
Add a simple claim
Add a Time claim with the property P74 and the current time to the local entity.
claim_time = datatypes.Time(prop_nr='P74', time='now')
entity.claims.add(claim_time)
Get claim value
Get the value of the first claim with the property P2048 of the local entity.
From item_get.ipynb
entity.claims.get('P2048')[0].mainsnak.datavalue['value']['amount']
Manipulate claim, add a qualifier
- Initialize a new Qualifiers object, add a String qualifier with the property P828 and the value 'Item qualifier' to the Qualifiers object.
- Create a String claim with the property P31533 and the value 'A String property' with the previously created Qualifiers object as qualifiers.
- Add the newly created claim to the local entity.
qualifiers = Qualifiers()
qualifiers.add(datatypes.String(prop_nr='P828', value='Item qualifier'))
claim_string = datatypes.String(prop_nr='P31533', value='A String property', qualifiers=qualifiers)
entity.claims.add(claim_string)
Manipulate claim, add references
- Initialize a new References object.
- Initialize a new Reference object, add a String reference with the property P828 and the value 'Item string reference' to the Reference object.
- Initialize a new Reference object, add a String reference with the property P828 and the value 'Another item string reference' to the Reference object.
- Add the newly created Reference objects to the References object.
- Create a String claim with the property P31533 and the value 'A String property' with the previously created References object as references.
- Add the newly created claim to the local entity.
references = References()
reference1 = Reference()
reference1.add(datatypes.String(prop_nr='P828', value='Item string reference'))
reference2 = Reference()
reference2.add(datatypes.String(prop_nr='P828', value='Another item string reference'))
references.add(reference1)
references.add(reference2)
new_claim_string = datatypes.String(prop_nr='P31533', value='A String property', references=references)
entity.claims.add(claim_string)
Remove a specific claim
Remove all claims with the property P31533 and the value Q123 from the local entity.
claims = entity.claims.get('P31533')
for claim in claims:
if claim.mainsnak.datavalue['value']['id'] == 'Q123':
claim.remove()
Get lemma on lexeme
Get all French lemmas of the lexeme.
lexeme.lemmas.get(language='fr')
Set lemma on lexeme
Add a French lemma with the value 'réponse' to the lexeme.
From lexeme_update.ipynb
lexeme.lemmas.set(language='fr', value='réponse')
Add gloss to a sense on lexeme
- Create a new Sense
