Exemple #1
0
def save():
    lid = "L" + request.form.get("LID")
    qid = "Q" + request.form.get("QID")
    lang = "Q" + request.form.get("lang")
    gloss = request.form.get("gloss")
    log.info("%s %s %s %s", lid, qid, lang, gloss)

    # Get token and auth
    access_token = flask.session["access_token"]
    auth1 = OAuth1(
        consumer_token.key,
        client_secret=consumer_token.secret,
        resource_owner_key=access_token["key"],
        resource_owner_secret=access_token["secret"],
    )
    token = get_tokens("csrf", auth1)
    username = flask.session.get("username", None)
    repo = LexData.WikidataSession(username=username, token=token, auth=auth1)

    # Edit – if possible
    L = LexData.Lexeme(repo, lid)
    for sense in L.senses:
        claims = sense.claims().get("P5137")
        if claims and claims[0].pure_value == qid:
            return "Sense already existing", 409
    if lang not in languages:
        return "Error. Language not supported yet!", 409
    glosses = {languages[lang]: gloss}
    claims = {"P5137": [qid]}
    try:
        _ = L.createSense(glosses, claims)
    except PermissionError as error:
        log.exception(error)
        return "Your not permitted to do this action!", 403

    # Update DB status
    cursor = db.connection.cursor()
    cursor.execute(
        """
        UPDATE
        matches
        SET status = 1
        WHERE QID = %s and LID = %s
        """,
        (qid[1:], lid[1:]),
    )
    cursor.close()
    db.connection.commit()
    return "Done!"
Exemple #2
0
def test_auth(credentials):
    with pytest.raises(Exception):
        assert LexData.WikidataSession("Username", "Password")
    anon = LexData.WikidataSession()
    LexData.Lexeme(anon, "L2")
Exemple #3
0
def repoTestWikidata():
    test = LexData.WikidataSession()
    test.URL = "https://test.wikidata.org/w/api.php"
    test.CSRF_TOKEN = "+\\"
    return test
Exemple #4
0
def repo(credentials):
    username, password = credentials
    return LexData.WikidataSession(username, password)
Exemple #5
0
# https://nudin.github.io/LexData/

import logging

import LexData
from LexData.language import en

logging.basicConfig(level=logging.INFO)

repo = LexData.WikidataSession("DL2204", "Humboldt1817")

# Open a Lexeme
#L2 = LexData.Lexeme(repo, "L72517")
#LexData.get_or_create_lexeme(repo, "zuri", eu, "Q1084")

# Access the claims
print(L2.claims.keys())
# and Forms
print(len(L2.forms))
F1 = L2.forms[0]
print(F1.claims.keys())
# and senses
print(len(L2.senses))
S1 = L2.senses[0]
print(S1.claims.keys())
Exemple #6
0
# Settings
language = "swedish"
wd_prefix = "http://www.wikidata.org/entity/"
debug = True
# Logging for LexData
# logging.basicConfig(level=logging.INFO)

#
# Instantiation
#
# Authenticate with WikibaseIntegrator
global login_instance
login_instance = wbi_login.Login(user=config.username, pwd=config.password)
# LexData authentication
login = LexData.WikidataSession(config.username, config.password)

#
# Functions
#


def yes_no_question(message: str):
    # https://www.quora.com/
    # I%E2%80%99m-new-to-Python-how-can-I-write-a-yes-no-question
    # this will loop forever
    while True:
        answer = input(message + ' [Y/n]: ')
        if len(answer) == 0 or answer[0].lower() in ('y', 'n'):
            if len(answer) == 0:
                return True
Exemple #7
0
#!/usr/bin/python3
import logging

import LexData
from LexData.language import lang_en

logging.basicConfig(level=logging.INFO)

repo = LexData.WikidataSession("MichaelSchoenitzer", "foobar")

# Open a Lexeme
L2 = LexData.Lexeme(repo, "L2")

# Access the claims
print(L2.claims.keys())
# and Forms
print(len(L2.forms))
F1 = L2.forms[0]
print(F1.claims.keys())
# and senses
print(len(L2.senses))
S1 = L2.senses[0]
print(S1.claims.keys())

# Find or create a Lexeme by lemma, language and grammatical form
L2 = LexData.get_or_create_lexeme(repo, "first", lang_en, "Q1084")

# You can easily create forms…
if len(L2.forms) == 0:
    L2.create_form("firsts", ["Q146786"])
Exemple #8
0
     with open(sys.argv[1], 'r') as f:
         contents = f.readlines()
 except IndexError:
     console.error("IndexError during reading of tsv file ")
     exit(1)
 if contents is not None:
     data = parse_input_file(contents)
 console.info("Commands parsed successfully")
 pprint(data)
 #exit(0)
 new_lexeme_count = len(data["new_lexemes"])
 command_count = len(data["commands"])
 if new_lexeme_count > 0 or command_count > 0:
     # see https://www.wikidata.org/w/api.php?action=help&modules=wbeditentity for documentation
     console.info("Logging in with LexData")
     repo = LexData.WikidataSession(config.user, config.botpassword)
     # first execute creation of lexemes
     if new_lexeme_count > 0:
         for lexeme in data["new_lexemes"]:
             lemma = lexeme["lemma"]
             forms = lexeme["forms"]
             claims = lexeme["claims"]
             wikimedia_language_code = lexeme["language_code"]
             language_item_id = lexeme["language_item_id"]
             lexical_category = lexeme["lexical_category"]
             console.info(f"Creating {lemma} with {len(forms)} forms and {len(claims)} claims")
             # Find or create a Lexeme by lemma, language and lexical category
             # Configure languages of LexData
             lang = LexData.language.Language(wikimedia_language_code, language_item_id)
             # Try finding it or create a new lexeme
             new_lexeme = LexData.get_or_create_lexeme(repo, lemma, lang, lexical_category)