-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.py
289 lines (264 loc) · 7.66 KB
/
ai.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from assets.database import elemDict, varDict
import random
import matplotlib
import matplotlib.pyplot as plt
import os
"""
NOTE:
Noms de fonctions : plus clair et plus long
> analyze -> analyze_usr_string_chatbox
Commentaires :
> Ajouter plus de commentaires sur quoi fait quoi.
> Ajouter docstrings, type d'entrée et sortie.
Noms de variables: plus clair et plus long
> Exemple : analyse : x, pas intuitif, plutôt indiquer :
> x->usr_input_string
try/except :
> Ne pas hésiter à utiliser le mot-clé raise.
lisibilité :
> Espace ton code
(PS : Je parle mal, mais c'est quand même très très cool,
les autres de ma promos
ils utilisent tous ChatGPT comme des merdes.)
Pistes d'amélioration :
- Éclaicit ton code, que ce soit côté commentaires ou docstrings.
C'est, en soit peu important si tu gardes tout en local,
mais si tu es sur un Repo Github, alors d'autres
sont suceptibles de le voir.
- Possibilité : utilise un fichier .bat pour faciliter
le lancement de l'application par un autre utilisateur.
ATTENTION : le chemin de tes fichiers reisque d'être cassé,
utilise qqch du genre :
icon_path = os.path.join(os.path.dirname(__file__),
'..', 'media', 'icons', 'app_icon.png')
Car l'emplacement d'utilisation n'est plus le même.
- Possibilité : Met à jour dynamiquement la taille de la fenêtre.
Soit screen_height,screen_wide la hauteur et largeur d'écran.
Utilise des multiplicateurs pour n%.
- Possibilité : sur un côté esthétique plus simple,
change le nom de la fenêtre et met une icône.
Aussi, ajoute éventuellement un peu de son, c'est funny.
Et, tu peux ajouter un temps de réponse avec un effet
comme si l'autre paysan écrivait.
- History :
Utilise une fonction write_to_history()
qui écrit au propre ton message dans l'historique
Éventuellement, utilise une structure pile/file; si utilisé dans le code.
Voir : https://www.ukonline.be/cours/python/apprendre-python/chapitre5-4
Bref, l'utilisation d'une structure de stockage native (ou objet)
serait bien plus propre.
SI TU VEUX AVANCER :: https://www.france-ioi.org/algo/chapters.php
"""
isTraining = False
try:
history = (open("assets/history").read()).split("#")
while "" in history:
history.remove("")
except FileNotFoundError:
open("assets/history", "x")
history = []
iterHistory = []
try:
stats = (open("assets/statistics").read()).split("#")
while "" in stats:
stats.remove("")
except FileNotFoundError:
open("assets/statistics", "x")
stats = []
iterStats = []
def analyze(x: str) -> dict:
"""
Analyse une entrée utilisateur, et renvoie le dictionnaire approprié.
"""
analysis = {
"elements": [],
"variables": [],
"values": [],
"type": "",
}
x = x.lower()
x = x.replace("é", "e")
x = x.replace("è", "e")
x = x.replace("'", " ")
for i, c in enumerate(x):
if (
c == ","
and ((x[i - 1]) in "0123456789" if i > 0 else False)
and ((x[i + 1]) in "0123456789" if i < len(x) - 1 else False)
):
x = x.replace(c, ".")
splitQues = x.split(" ")
for i, word in enumerate(splitQues):
if word in elemDict:
analysis["elements"].append(word)
try:
analysis["values"].append(int(word))
except ValueError:
try:
analysis["values"].append(float(word))
except ValueError:
pass
if word in varDict:
analysis["variables"].append(word)
else:
wordAndNext = \
"".join(list(
word + " " + splitQues[i + 1]
if i < len(splitQues) - 1 else "")
)
if wordAndNext in varDict:
analysis["variables"].append(wordAndNext)
if len(analysis["variables"]) and (
(len(analysis["elements"]) == 0)
!= (len(analysis["values"]) == 0)
):
analysis["type"] = "question"
return analysis
def treatment(entry):
global history, iterHistory, output, answer, \
average, correct, wrong, isTraining
if not isTraining:
iterHistory = []
entry = analyze(entry)
for variable in entry["variables"]:
for element in entry["elements"]:
ans = elemDict[element][varDict[variable][2]]
history.append(f"e/{variable}/{element}/{ans}")
x = open("assets/history", "a")
x.write(f"e/{variable}/{element}/{ans}#")
x.close()
# print(history)
return (
("la " if varDict[variable][3] == "f" else "le ")
+ f"{varDict[variable][0]} "
+ ("de l'" if element[0] in "aeiouyh" else "du ")
+ f"{elemDict[element][0]} est "
f"{ans} "
+ varDict[variable][1]
)
for value in entry["values"]:
diff = {}
for i, x in elemDict.items():
diff[str(i)] = abs(
x[varDict[variable][2]]
- value
)
# print(diff)
ans = "".join(list(
i for i, x in diff.items() if min(diff.values()) == x))
history.append(f"v/{variable}/{value}/{ans}")
x = open("assets/history", "a")
x.write(f"v/{variable}/{value}/{ans}#")
x.close()
return (
"l'élément avec une "
+ varDict[variable][0]
+ " de "
+ f"{str(elemDict[ans][varDict[variable][2]])} "
+ varDict[variable][1]
+ (" est l'" if ans[0] in "aeiouyh" else " est le ")
+ elemDict[ans][0]
)
elif isTraining:
output = ""
if iterHistory == []:
history = list(set(history))
random.shuffle(history)
iterHistory = iter(history)
correct = 0
wrong = 0
else:
entry = analyze(entry)
for i in entry.values():
for x in i:
try:
if answer in x:
output = "correct ! "
correct += 1
else:
output = "faux ! "
wrong += 1
except TypeError:
if answer in i:
output = "correct ! "
correct += 1
else:
output = "faux ! "
wrong += 1
x = next(iterHistory, "END")
if not x == "END":
x = x.split("/")
if x[0] == "e":
output += (
("Quelle " if varDict[x[1]][3] == "f" else "Quel ")
+ ("est l'" if x[1][0] in "aeiouyh" else (
"est la " if varDict[x[1]][3] == "f" else "est le "))
+ varDict[x[1]][0]
+ (" de l'" if x[2][0] in "aeiouyh" else " du ")
+ elemDict[x[2]][0]
+ " ?"
)
answer = float(x[3])
elif x[0] == "v":
output += (
(
"Quel élément a une " if varDict[x[1]][3] == "f"
else "Quel élément a un ")
+ varDict[x[1]][0]
+ " de "
+ x[2]
+ " ?"
)
answer = x[3]
else:
os.remove("assets/history")
return "An error has occurred while trying to fetch history data. \
deleting history file."
elif x == "END":
history = []
x = open("assets/history", "w")
x.write("")
x.close()
x = open("assets/statistics", "a")
x.write(f"{correct}/{wrong}#")
x.close()
stats.append(f"{correct}/{wrong}#")
correct100 = round(correct * 100 / (wrong + correct))
fig, ax = plt.subplots()
ax.pie([correct, wrong], labels=["correct", "faux"], colors=["g", "r"])
if len(stats) > 1:
fig, bars = plt.subplots()
stats1 = []
stats2 = []
for x in stats:
x = x.replace("#", "")
x = x.split("/")
stats1.append(float(x[0]))
stats2.append(float(x[1]))
bars.stackplot(range(len(stats)), stats1, stats2, colors=["g", "r"],)
else:
output += """
Aucune statistiques sur le long terme à afficher."""
isTraining = False
output += (f"""
bonnes réponses : {correct}
mauvaises réponses : {wrong}
taux de bonne réponses : {correct100}%""")
plt.show()
else:
return "something wrong happened. not supposed to be possible tho."
# utilise le "Except as e", et print e en fstring : 'err_msg {e}'
return output
def train(): # fix de flemmard, peut-être faire un fichier
# qui contient tes globales, chargé avant ?
# A toi de voir.
# + nom de fonction, plutôt change_training_state()->None:
global isTraining
if not isTraining:
isTraining = True
else:
isTraining = False
def pltUseQt():
matplotlib.use('Qt5Agg')
def pltUseTk():
matplotlib.use('TkAgg')