Exemple #1
0
def test_search(repo):
    results = LexData.search_lexemes(repo, "first", LexData.language.en,
                                     "Q1084")
    assert len(results) == 1
    assert results[0].get("id") == "L2"

    result = LexData.get_or_create_lexeme(repo, "first", LexData.language.en,
                                          "Q1084")
    assert result["id"] == "L2"
Exemple #2
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 #3
0
def test_detatchedClaim(repo):
    LexData.Claim(propertyId="P369", value="Q1")
    LexData.Claim(propertyId="P856", value="http://example.com/")
    LexData.Claim(propertyId="P2534", value="\frac{1}{2}")
    quantity = LexData.Claim(propertyId="P2021", value=6)
    assert quantity.pure_value == 6
    date = LexData.Claim(propertyId="P580", value=datetime.now())
    assert type(date.pure_value) is str
    with pytest.raises(TypeError):
        LexData.Claim(propertyId="P856", value=1)
        LexData.Claim(propertyId="P2021", value="foo")
        LexData.Claim(propertyId="P580", value=1)
        LexData.Claim(propertyId="P580", value="foo")
    with pytest.raises(Exception):
        LexData.Claim(propertyId="P0", value="foo")
Exemple #4
0
def test_form(repo):
    L2 = LexData.Lexeme(repo, "L2")
    forms = L2.forms
    assert isinstance(forms, list)
    for form in forms:
        assert isinstance(repr(form), str)
        assert isinstance(form.form, str)
        assert isinstance(form.claims, dict)
Exemple #5
0
def test_writes(repoTestWikidata):
    L123 = LexData.Lexeme(repoTestWikidata, "L123")

    L123.createClaims({"P7": ["Q100"]})
    L123.addClaims({"P7": ["Q100"]})

    L123.createForm("test", ["Q100"])

    L123.createSense({"de": "testtest", "en": "testtest"})
    L123.createSense({"de": "more tests", "en": "more tests"}, claims={})
    L123.createSense({"en": "even more tests"}, claims={"P7": ["Q100"]})
Exemple #6
0
def test_sense(repo):
    L2 = LexData.Lexeme(repo, "L2")
    assert str(L2)
    assert repr(L2)

    senses = L2.senses
    assert isinstance(senses, list)
    for sense in senses:
        assert isinstance(repr(sense), str)
        assert isinstance(sense.glosse(), str)
        assert isinstance(sense.glosse("de"), str)
        assert sense.glosse("en") == sense.glosse()
        assert sense.glosse("XX") == sense.glosse()
        del sense["glosses"]["en"]
        assert isinstance(sense.glosse("XX"), str)
        assert isinstance(sense.claims, dict)
Exemple #7
0
def test_lexeme(repo):
    L2 = LexData.Lexeme(repo, "L2")

    assert L2.lemma == "first"
    assert L2.language == "en"
    claims = L2.claims
    assert isinstance(claims, dict)

    examples = claims.get("P5831", [])
    assert len(examples) >= 1
    example = examples[0]
    assert isinstance(repr(example), str)
    assert example.property == "P5831"
    assert example.rank == "normal"
    assert example.numeric_rank == 0
    assert example.type == "monolingualtext"
    assert example.pure_value == "He was first in line."
    assert example.value["text"] == "He was first in line."
    assert example.value["language"] == "en"
Exemple #8
0
def test_auth(credentials):
    with pytest.raises(Exception):
        assert LexData.WikidataSession("Username", "Password")
    anon = LexData.WikidataSession()
    LexData.Lexeme(anon, "L2")
Exemple #9
0
def repoTestWikidata():
    test = LexData.WikidataSession()
    test.URL = "https://test.wikidata.org/w/api.php"
    test.CSRF_TOKEN = "+\\"
    return test
Exemple #10
0
def repo(credentials):
    username, password = credentials
    return LexData.WikidataSession(username, password)
Exemple #11
0
def test_createLexeme(repoTestWikidata):
    LexData.create_lexeme(repoTestWikidata, "foobar", LexData.language.en,
                          "Q100")
Exemple #12
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 #13
0
def fetch_senses(lid):
    """Returns list of senses"""
    return LexData.Lexeme(login, lid).senses
Exemple #14
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 #15
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 #16
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)