diff options
author | Eddy Pedroni <eddy@0xf7.com> | 2022-10-18 19:29:06 +0200 |
---|---|---|
committer | Eddy Pedroni <eddy@0xf7.com> | 2022-10-18 19:29:06 +0200 |
commit | 0d07220aeceae94fc05b12c4c98bec9ee28026b4 (patch) | |
tree | 1d9f8a57031c3d959faf1c48881e87a1fdfae081 /solver.py | |
parent | 31639b35e17732cf4c543194ec6d830da0178540 (diff) |
MVP done
Diffstat (limited to 'solver.py')
-rw-r--r-- | solver.py | 63 |
1 files changed, 61 insertions, 2 deletions
@@ -1,3 +1,62 @@ +import random -def solve(case, article, number, adjective, noun): - return "" +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})") |