summaryrefslogtreecommitdiffstats
path: root/solver.py
blob: 4c302b5b801af4942e87e53b1bbd104f7f021aca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import random

import tables

class Query:
    def __init__(self, case, article, cardinality, adjective, noun):
        self.case = case
        self.article = article
        self.cardinality = cardinality
        self.adjective = adjective
        self.noun = noun

class Solution:
    def __init__(self, full, noun):
        self.full = full
        self.noun = noun

    def evaluate(self, candidate):
        return self.full.lower().strip() == candidate.lower().strip()

def _get():
    case = random.choice(tables.CASES)
    article = random.choice(tables.ARTICLE_TYPES)
    cardinality = random.choice(tables.CARDINALITIES)
    adjective = random.choice(tables.ADJECTIVES)
    noun = random.choice(tables.NOUNS)

    return Query(case, article, cardinality, adjective, noun)

def get():
    attempts = 0
    while attempts < 10:
        attempts += 1

        query = _get()
        try:
            solution = solve(query)
            return (query, solution)
        except Exception as e:
            print(e)
            continue

    raise Exception("Could not find query after 10 attempts!")

def solve(query):
    gender = "p" if query.cardinality == "Plural" else query.noun["gender"]
    nounKey = f"{query.case.lower()[0:3]}-{query.cardinality.lower()[0:3]}"

    # full solution
    decArt = tables.ARTICLES[query.article][query.case][gender]
    adjEnding = tables.ADJ_ENDINGS[query.article][query.case][gender]
    decNoun = query.noun[nounKey]
    
    # noun details
    basicArt = tables.ARTICLES['bestimmter Artikel']['Nominativ'][gender]
    basicNoun = query.noun[f"nom-{'plu' if query.cardinality == 'Plural' else 'sin'}"]

    if decNoun == "_":
        raise Exception("Invalid query")
    
    return Solution(f"{decArt} {query.adjective}{adjEnding} {decNoun}".strip(),
                    f"{basicArt} {basicNoun} ({query.cardinality})")