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
63
64
65
|
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:
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]
if adjEnding[0] == query.adjective[-1]:
adjEnding = adjEnding[1:]
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})")
|