diff options
-rwxr-xr-x | data/noun-query.sh | 7 | ||||
-rw-r--r-- | kasus.py | 13 | ||||
-rw-r--r-- | query.py | 21 | ||||
-rw-r--r-- | solver.py | 63 | ||||
-rw-r--r-- | tables.py | 77 |
5 files changed, 149 insertions, 32 deletions
diff --git a/data/noun-query.sh b/data/noun-query.sh index 948bf02..c9d3224 100755 --- a/data/noun-query.sh +++ b/data/noun-query.sh @@ -12,6 +12,8 @@ sed -r -z "s/\n(\|[A-Z])/\1/g" noun-stage2 > noun-stage3 grep -e "Deutsch Substantiv Übersicht" noun-stage3 > noun-stage4 python -c """ +import re + with open('noun-stage4', 'r', encoding='utf8') as clean: cleanLines = clean.readlines() @@ -26,11 +28,12 @@ with open('nouns.csv', 'w', encoding='utf-8') as nouns: for d in dicts: try: + if not re.match(r'[A-Za-z_]', d['Nominativ Singular'][0]): + continue line = ','.join([d['Genus'], d['Nominativ Singular'], d['Nominativ Plural'], d['Akkusativ Singular'], d['Akkusativ Plural'], d['Dativ Singular'], d['Dativ Plural'], d['Genitiv Singular'], d['Genitiv Plural']]) nouns.write(line + '\n') except: pass """ - -rm noun-stage* +#rm noun-stage* @@ -1,16 +1,16 @@ from colorama import Fore, Back, Style +import readline -import query import solver total = 0 correct = 0 while True: - case, article, number, adjective, noun = query.get() + query, solution = solver.get() - parameters = f"{Fore.MAGENTA}{case}, {Fore.BLUE}{article}, {Fore.YELLOW}{number}{Style.RESET_ALL}" - queryString = f"{noun}, {adjective}" + parameters = f"{Fore.MAGENTA}{query.case}, {Fore.BLUE}{query.article}, {Fore.YELLOW}{query.cardinality}{Style.RESET_ALL}" + queryString = f"{query.noun['nom-sin']}, {query.adjective}" total += 1 print(f"{Style.BRIGHT}--- Übung {total} ---{Style.NORMAL}") @@ -20,14 +20,13 @@ while True: response = input("> ") - solution = solver.solve(case, article, number, adjective, noun) print(f"{Style.BRIGHT}", end="") - if response == solution: + if solution.evaluate(response): correct += 1 print(f"{Fore.GREEN}Richtig ({correct}/{total})") else: - print(f"{Fore.RED}Falsch ({correct}/{total}):{Style.RESET_ALL} {solution}") + print(f"{Fore.RED}Falsch ({correct}/{total}):{Style.RESET_ALL} {solution.full}, {solution.noun}") print(f"{Style.RESET_ALL}") diff --git a/query.py b/query.py deleted file mode 100644 index c89b3a7..0000000 --- a/query.py +++ /dev/null @@ -1,21 +0,0 @@ -import json -import random - -CASES = ["Nominativ", "Akkusativ", "Dativ", "Genitiv"] -ARTICLES = ["bestimmter", "unbestimmter", "kein"] -CARDINALITIES = ["Singular"] * 3 + ["Plural"] - -with open("adjectives.json", "r") as f: - ADJECTIVES = json.load(f) - -with open("nouns.json", "r") as f: - NOUNS = json.load(f) - -def get(): - case = random.choice(CASES) - article = random.choice(ARTICLES) - cardinality = random.choice(CARDINALITIES) - adjective = random.choice(ADJECTIVES) - noun = random.choice(NOUNS) - - return case, article, cardinality, adjective, noun @@ -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})") diff --git a/tables.py b/tables.py new file mode 100644 index 0000000..54847e1 --- /dev/null +++ b/tables.py @@ -0,0 +1,77 @@ +import json +import csv + +ARTICLES = { + "bestimmter Artikel" : { + "Nominativ" : { "m" : "der", "f" : "die", "n" : "das", "p" : "die" }, + "Akkusativ" : { "m" : "den", "f" : "die", "n" : "das", "p" : "die" }, + "Dativ" : { "m" : "dem", "f" : "der", "n" : "dem", "p" : "den" }, + "Genitiv" : { "m" : "des", "f" : "der", "n" : "des", "p" : "der" }, + }, + "unbestimmter Artikel" : { + "Nominativ" : { "m" : "ein", "f" : "eine", "n" : "ein", "p" : "meine" }, + "Akkusativ" : { "m" : "einen", "f" : "eine", "n" : "ein", "p" : "meine" }, + "Dativ" : { "m" : "einem", "f" : "einer", "n" : "einem", "p" : "meinen" }, + "Genitiv" : { "m" : "eines", "f" : "einer", "n" : "eines", "p" : "meiner" }, + }, + "kein Artikel" : { + "Nominativ" : { "m" : "", "f" : "", "n" : "", "p" : "" }, + "Akkusativ" : { "m" : "", "f" : "", "n" : "", "p" : "" }, + "Dativ" : { "m" : "", "f" : "", "n" : "", "p" : "" }, + "Genitiv" : { "m" : "", "f" : "", "n" : "", "p" : "" }, + } +} + +ADJ_ENDINGS = { + "bestimmter Artikel" : { + "Nominativ" : { "m" : "e", "f" : "e", "n" : "e", "p" : "en" }, + "Akkusativ" : { "m" : "en", "f" : "e", "n" : "e", "p" : "en" }, + "Dativ" : { "m" : "en", "f" : "en", "n" : "en", "p" : "en" }, + "Genitiv" : { "m" : "en", "f" : "en", "n" : "en", "p" : "en" }, + }, + "unbestimmter Artikel" : { + "Nominativ" : { "m" : "er", "f" : "e", "n" : "es", "p" : "en" }, + "Akkusativ" : { "m" : "en", "f" : "e", "n" : "es", "p" : "en" }, + "Dativ" : { "m" : "en", "f" : "en", "n" : "en", "p" : "en" }, + "Genitiv" : { "m" : "en", "f" : "en", "n" : "en", "p" : "en" }, + }, + "kein Artikel" : { + "Nominativ" : { "m" : "er", "f" : "e", "n" : "es", "p" : "e" }, + "Akkusativ" : { "m" : "en", "f" : "e", "n" : "es", "p" : "e" }, + "Dativ" : { "m" : "em", "f" : "er", "n" : "em", "p" : "en" }, + "Genitiv" : { "m" : "en", "f" : "er", "n" : "en", "p" : "er" }, + }, +} + +NOUN_ENDINGS = { + "bestimmter Artikel" : { + "Nominativ" : { "m" : "", "f" : "", "n" : "", "p" : "" }, + "Akkusativ" : { "m" : "", "f" : "", "n" : "", "p" : "" }, + "Dativ" : { "m" : "", "f" : "", "n" : "", "p" : "n" }, + "Genitiv" : { "m" : "s", "f" : "", "n" : "s", "p" : "" }, + }, + "unbestimmter Artikel" : { + "Nominativ" : { "m" : "", "f" : "", "n" : "", "p" : "" }, + "Akkusativ" : { "m" : "", "f" : "", "n" : "", "p" : "" }, + "Dativ" : { "m" : "", "f" : "", "n" : "", "p" : "n" }, + "Genitiv" : { "m" : "s", "f" : "", "n" : "s", "p" : "" }, + }, + "kein Artikel" : { + "Nominativ" : { "m" : "", "f" : "", "n" : "", "p" : "" }, + "Akkusativ" : { "m" : "", "f" : "", "n" : "", "p" : "" }, + "Dativ" : { "m" : "", "f" : "", "n" : "", "p" : "n" }, + "Genitiv" : { "m" : "s", "f" : "", "n" : "s", "p" : "" }, + }, +} + +CASES = ["Nominativ"] + ["Akkusativ"] * 2 + ["Dativ"] * 2 + ["Genitiv"] + +ARTICLE_TYPES = ["bestimmter Artikel", "unbestimmter Artikel", "kein Artikel"] + +CARDINALITIES = ["Singular"] * 3 + ["Plural"] + +with open("data/adjectives.json", "r") as f: + ADJECTIVES = json.load(f) + +with open("data/nouns.csv", "r", newline="") as f: + NOUNS = [noun for noun in csv.DictReader(f)] |