Wikidata:Pywikibot - Python 3 Tutorial/Lexeme

From Wikidata
Jump to navigation Jump to search

This page or section is in the process of an expansion or major restructuring. You are welcome to assist in its construction by editing it as well.

If an Item can not be found the bot can also create it. It is important that the bot makes sure that the item is really necessary. Otherwise it will create many duplicates.

Example of how a Lexeme could look: first (L2) (API)

Create a Lexeme[edit]

A new item can be created like this with the API:

# -*- coding: utf-8  -*-
import pywikibot
import json
"""
Make lexeme
""" 

site = pywikibot.Site(u'wikidata', u'wikidata')
site.login()

def run(data):
    return site._simple_request(**data).submit()

labels = {}
labels["nb"] = {"language":"nb","value":"fjell"} #not supporting utf-8 æøå

summary = "Lexeme in Norwegian"

PARAMS = {
"action": "wbeditentity",
"format": "json",
"new": "lexeme",
"summary": "+Lexeme in Norwegian",
"token": site.tokens['edit'],
"data": json.dumps({"type":"lexeme","lemmas":labels,"lexicalCategory":"Q1084","language":"Q25167"}),
 #“Type” is Lexeme. “lemmas” is used as a human readable representation of the lexeme, e.g. "run" (dictionary). “lexicalCategory” is to which the lexeme belongs. This is given as a reference to a concrete Item, e.g. Q34698 for adjective. "language" is language.
}

R = run(PARAMS)

Find Lexemes[edit]

Finds a specific Lexeme with the use of language and label:

# -*- coding: utf-8  -*-
import pywikibot
import json
"""
Find Lexeme
"""

site = pywikibot.Site(u'wikidata', u'wikidata')
site.login()

def search(value, lang):
    PARAMS2 = {
        "action": "wbsearchentities",
        "format": "json",
        "search": value,
        "language": lang,
        "type": "lexeme",
    }
    data = get(PARAMS2)
    
    for lem in data['search']: #looks for match on language and label.
        if lem['match']['text'].lower() == value.lower() and lem['match']['language'].lower() == lang.lower():
            return lem #if found return simple info about the Lexeme.
        
    return ''

def getLexeme(lemme):
    PARAMS3 = {
        "action": "wbgetentities",
        "format": "json",
        "ids": lemme,
    }
    data = get(PARAMS3)
    
    if data['success'] == 1:
        return data
    else:
        return ''

test = search('vann', 'nb') #search for the lexeme "vann" in the the Norwegian language (nb).
lex = getLexeme(test['id']) #Finds all the information about this Lexeme with its ID (L8361)

"lex" is a json object of the lexeme.