-
Notifications
You must be signed in to change notification settings - Fork 3
/
analyste.py
224 lines (175 loc) · 7.73 KB
/
analyste.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
####
# Copyright (C) 2006-2015 Kim Gerdes
# kim AT gerdes.fr
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
# See the GNU General Public License (www.gnu.org) for more details.
#
# You can retrieve a copy of the GNU General Public License
# from http://www.gnu.org/. For a copy via US Mail, write to the
# Free Software Foundation, Inc.
# 59 Temple Place - Suite 330,
# Boston, MA 02111-1307
# USA
####
import time, re, cgitb, cgi,sha, Cookie, os,sys
cgitb.enable()
import session
from random import choice
from frenchLinguist import FrenchLinguist
from lookup import lookup
from rogers import Rogers
class Elizia:
listeInterro=["pourquoi","quand","comment"]
fini = False
dicoDejaDitElizia = {}
cas = None # contient le mot clé trouvé
def __init__(self,session):
self.sess = session
self.memory = session.data.get('memory',None)
#if not self.memory: self.memory=[([],[],[],[])]
if self.memory:self.memoProblems,self.memoAnswers,self.memoCases,self.memoTimes = zip(*self.memory)
else:self.memoProblems,self.memoAnswers,self.memoCases,self.memoTimes=[],[],[],[]
#session.data.get('memoireinputsPatient',[])
#self.avantCas = session.data.get('memoireCas',[])
self.dicoDejaDitElizia = session.data.get('dejaDitelizia',{})
self.sex = session.data.get('sex',"unknown")
self.rogers = Rogers()
self.goodCases=[c for c in self.memoCases if c not in self.rogers.noRecall]
self.linguist = FrenchLinguist()
self.problem = "???" # devrait être écrasé par le vrai input
def analyse(self,problem):
""" ici se fait tout le boulot. """
self.problemBase=problem.strip()
#first letter of sentence lower key:
if len(self.problemBase)>1 : self.problemBase = self.problemBase[0].lower()+self.problemBase[1:]
self.special=self.problemBase.split(":")[0]
#print "***",self.problemBase.split(":"),self.special
if self.special in ["words","lemmas","all"]:self.problemBase=" ".join(self.problemBase.split(":")[1:])
else:self.special=None
#self.problem = self.problemBase
self.problem = self.linguist.decontracte(self.problemBase)
if self.special=="words":return "words: "+self.problem
self.cas = [] # contains (quality,case) couples
inputPropre = self.linguist.nettoyerTexte(self.problem)
inputSplit = inputPropre.split()
#print inputSplit
if self.special=="lemmas": return "lemmas: "+" / ".join(lookup(inputPropre))
lemmas = lookup(inputPropre) # list of lemmas (that are different from the form)
sex = self.linguist.guesssex(self.problem)
if sex != "unknown":
self.sess.data['sex'] = sex
if self.sex != "unknown" and sex != self.sex:
c="votre identité sexuelle"
self.cas += [(self.rogers.quality[c],c)]
#return self.repo()
self.sex = sex
#for lemma in lemmas:
#print lemma,"-"
allKeys = list(set(inputSplit + lemmas))
allKeys.sort()
if self.special=="all": return "all: "+" / ".join(allKeys)
# special cases:
if len(self.problem) > 0 and self.cleTrouvee(self.rogers.keywords['vos SMS'],self.problem,self.problem.split()):
c="vos SMS"
self.cas += [(self.rogers.quality.get(c,0),c)]
#voir si le patient a dit qqch
if len(inputSplit)==0:
#if len(self.patientInput) > 0:
c="Vide"
self.cas += [(self.rogers.quality.get(c,0),c)]
return self.repo()
# voir si le patient a pê posé une question :
# donc si ça commence avec un mot interrogatif ou termine sur un point d'interrogation
if inputSplit[0] in self.listeInterro or self.problem[-1]=="?":
c="Question"
self.cas += [(self.rogers.quality.get(c,0),c)]
elif self.problemBase in self.memoProblems :
c="Répétition"
self.cas += [(self.rogers.quality.get(c,0),c)]
# regular cases:
for nom in self.rogers.keywords.keys(): # pour chaque clé
if self.cleTrouvee(self.rogers.keywords[nom],inputPropre,allKeys):
self.cas += [(self.rogers.quality[nom],nom)]
#voir si le patient est très bref
if len(inputSplit)==1:
c="Bref"
self.cas += [(self.rogers.quality.get(c,0),c)]
c="Retour"
self.cas += [(self.rogers.quality.get(c,0),c)]
return self.repo()
def cleTrouvee(self,cles,texte,liste):
"""
cherche une liste cles dans un self.texte (string) et dans la liste de mots correspondante
deux cas : clé est un mot : il doit apparaître entouré de blancs
ou clé contient des espaces : peu importe, tant qu'il apparaît
"""
for c in cles :
if " " in c and c in texte:
return True
elif c in liste :
return True
return False
def repo(self):
""" fonction importante qui rend la réponse et gêre les mémoires """
ordered,reponse,cases,maxiq = False,"",[],0
for q,c in self.cas:
if q>maxiq:
cases=[c]
maxiq=q
elif q==maxiq:cases+=[c]
#print cases
nomcas=choice(cases) # take one of the maxi cases by chance
#print nomcas
memoire = self.dicoDejaDitElizia.get(nomcas,[])
#if nomcas in self.dicoDejaDitElizia.keys() : memoire = self.dicoDejaDitElizia[nomcas]
#else : memoire = []
ordered = nomcas in self.rogers.ordered #== "Insulte" ,...
# if (ordered, no more answers and no cycle) or end case: end it all
self.fini = (nomcas in self.rogers.ordered and len(memoire) >= len(self.rogers.answers[nomcas])-2 and nomcas in self.rogers.noCycle ) or nomcas == "Fin"
#if case is worse than 1, we do have good cases, come back to old cases from time to time (nomcas=="Retour" )
if maxiq<1 and self.goodCases and 0 == choice([0,1]) :
nomcas = "Reprise"
casRepris = self.goodCases[-1]
#print self.goodCases,self.memoCases
if 0 == choice([0,1,2]): # parfois (1/3) on ajoute une réponse de l'ancien cas
if casRepris in self.dicoDejaDitElizia.keys() : memoReprise = self.dicoDejaDitElizia[casRepris]
else : memoReprise = []
reponse = " \n "+self.choisirReponse(casRepris,memoReprise,False)
reponse = self.choisirReponse(nomcas,memoire,ordered) + reponse
if nomcas == "Reprise" : reponse = reponse.replace("xxx",casRepris)
if nomcas == "Retour" and " xxx" in reponse:
reponse = reponse.replace("xxx","")
reponse = reponse + self.linguist.phraseEnchassee(self.problem,self.sex)
# se souvenir de ce qu'on a dit
try:
self.sess.data['memory'] = (self.sess.data.get('memory') or [])+[( self.problemBase,reponse,nomcas,repr(time.time()) )]
except:
print "Putain, j'ai un problème de mémoire !"#.encode("utf-8")
return self.linguist.beautifier(reponse,self.sex)
def choisirReponse(self,casActuel,memoireActuel,ordered):
# le [:] permet de faire une copie
# - pour qu'on ne touche pas à la liste originale
listeReponseLocale=self.rogers.answers[casActuel][:]
for rep in memoireActuel:
if rep in listeReponseLocale: # normalement, ça devrait tjrs être dedans
listeReponseLocale.remove(rep) # on enlève ce qu'on a déjà dit
if ordered : reponse = listeReponseLocale[0]
else : reponse = choice(listeReponseLocale) # choix par hasard parmi le reste
memoireCas=memoireActuel+[reponse] # on retient la réponse (sans phrase du patient en cas Retour)
# vider le mémoire sauf la dernière réponse au cas où on a déjà utilisé toutes les réponses proposées
if len(memoireCas) >= len(self.rogers.answers[casActuel]):
while len(memoireCas)>1:
memoireCas.pop(0)
self.dicoDejaDitElizia[casActuel]=memoireCas
self.sess.data['dejaDitelizia'] = self.dicoDejaDitElizia
return reponse