-
Notifications
You must be signed in to change notification settings - Fork 3
/
trainer.py
174 lines (159 loc) · 7.75 KB
/
trainer.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from trainertypes import TrainerTypes
from typing import Dict, List
class Trainer:
def __init__(self, trainer: dict):
self.rawType: str = trainer.get("trainer")
self.type = [t for t in TrainerTypes if t.value == trainer.get("trainer")]
self.type = self.rawType if not self.type else self.type[0]
self.atoms: dict | list = trainer.get(
"atoms"
) # type is always list for no-instruction-trainer ("content")
self.cats: list = trainer.get(
"cats"
) # a list of categories. only for DRAG_GROUP as far as i know.
self.instruction: str | None = trainer.get(
"instruction"
) # dependant, some have instructions some dont.
self.solutions = [{}]
self.isNecessary: bool = True
self.isUnknownType: bool = False
def solve(self) -> "Trainer":
if "content" in self.rawType:
self.type = self.rawType
self.isNecessary = False
pass
else:
if (
self.type == TrainerTypes.MULTIPLE_CHOICE
): # safe to assume that MULTIPLE_CHOICE will always be solved.
furtherInstruction = f"{self.atoms.get('a')} "
for solution in self.atoms.get("b"):
if solution.startswith("++"):
sol = f"@blue:{solution[2:]}@green:".replace("<b>", "").replace(
"</b>", ""
)
self.solutions.append(
{
"furtherInstruction": furtherInstruction,
"solution": sol.replace("_", " "),
}
)
elif (
self.type == TrainerTypes.MULTIPLE_CHOICE_TEXT
): # safe to assume that MULTIPLE_CHOICE_TEXT will always be solved.
for atom in self.atoms:
if atom.get("a"):
furtherInstruction = f"{atom.get('a')} "
for solution in atom.get("b"):
if solution.startswith("++"):
sol = f"@blue:{solution[2:]}@green:".replace(
"<b>", ""
).replace("</b>", "")
self.solutions.append(
{
"furtherInstruction": furtherInstruction,
"solution": sol.replace("_", " "),
}
)
elif (
self.type == TrainerTypes.GAP
): # safe to assume that GAP will always be solved.
partial = (
self.atoms.get("b")
.replace("<b>", "")
.replace("</b>", "")
.split(" ")
)
sol = " ".join(
[
f"{'@blue:' if word.startswith('((++') or '_' in word else '@green:'}{word.replace('((++', '').replace('_', ' ').replace('))', '').replace('((', '').replace('.', '@green:.')}"
for word in partial
if not word.endswith("))")
]
)
self.solutions.append(
{
"furtherInstruction": "",
"solution": sol.replace("_", " "),
}
)
elif self.type == TrainerTypes.GAP_MULTI:
if type(self.atoms) == dict:
sol = " ".join(
[
f"{'@blue:' if word.startswith('((') else '@green:'}{word.replace('((', '').replace('))', '')}"
for word in self.atoms.get("b").split(" ")
if not word.startswith("--")
]
)
self.solutions.append(
{
"furtherInstruction": "",
"solution": sol.replace("_", " "),
}
)
elif type(self.atoms) == list:
for atom in self.atoms:
sol = " ".join(
[
f"{'@blue:' if word.startswith('((') else '@green:'}{word.replace('((', '').replace('))', '')}"
for word in atom.get("b").split(" ")
if not word.startswith("--")
]
)
self.solutions.append(
{
"furtherInstruction": "",
"solution": sol.replace("_", " "),
}
)
elif self.type == TrainerTypes.BUTTONS:
if type(self.atoms) == list:
for atom in self.atoms:
if "++" in atom.get("b"):
sol = " ".join(
[
f"{' @blue:' if word.startswith('++') else '@green:'}{word.replace('++', '')}"
for word in atom.get("b").split(" ")
if word.startswith("++")
]
)
self.solutions.append(
{
"furtherInstruction": atom.get("a")
.replace("<b>", "")
.replace("</b>", ""),
"solution": sol.replace("_", " "),
}
)
else:
sol = " ".join([f" @blue:{atom.get('b')}"])
self.solutions.append(
{
"furtherInstruction": atom.get("a")
.replace("<b>", "")
.replace("</b>", ""),
"solution": sol.replace("_", " "),
}
)
elif self.type == TrainerTypes.LIST_MATCH:
... # https://content.anton.app/files/?fileId=level%2Fc-natdeu-9%2Ftopic-04-grammatik-satzlehre%2Fblock-01-satzglieder%2Flevel-03&etag=0958-7629
elif self.type == TrainerTypes.FIND_WORDS_IN_TEXT:
... # https://content.anton.app/files/?fileId=level%2Fc-natdeu-9%2Ftopic-04-grammatik-satzlehre%2Fblock-01-satzglieder%2Flevel-03&etag=0958-7629
elif self.type == TrainerTypes.GRAVITY:
... # https://anton.app/en_us/learn/deutsch-9-10-klasse/topic-04-grammatik-satzlehre/exercises-01-satzglieder/exercise-01/
elif self.type == TrainerTypes.TABLE:
... # https://anton.app/en_us/learn/deutsch-9-10-klasse/topic-04-grammatik-satzlehre/exercises-01-satzglieder/exercise-01/
elif self.type == TrainerTypes.DRAG_GROUP:
for atom in self.atoms:
sol = f" @blue:{self.cats[atom.get('cat')]} -> {atom.get('b')}"
self.solutions.append(
{
"furtherInstruction": self.instruction.get("text"),
"solution": sol.replace("_", " "),
}
)
else:
self.type = self.rawType
self.isUnknownType = True
return self