-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRecognizer.py
More file actions
180 lines (162 loc) · 5.42 KB
/
Recognizer.py
File metadata and controls
180 lines (162 loc) · 5.42 KB
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
# -*- coding: utf-8 -*-
## Adrien Vernotte
## Python
## 22 10 2013
### MaJ 15/11/13
from DecoderAll import *
import os
class Recognizer():
"""Initialiser dictionnaire de synonymes
Recognizer.staticSynonymes=Recognizer().synonymes()"""
staticSynonymes=None
def __init__(self, ok=(False,False,False,False), dyslexie=0):
self.ACCENT = ok[0];
self.LIAISON = ok[1];#=Pro de la langue
self.PLURIEL = ok[2];
self.DESORDRE = ok[3];
self.DYSLEXIE = dyslexie;
self.accents=u"éèêëàâäôöîïûüç"
self.equival="eeeeaaaooiiuuc"
def synonymes(self):
dec=Decoder()
syn={}
a=open(os.path.join("dep","SynFr.txt"))
b=a.readlines()
a.close()
for x in b:
x=x.strip()
i,j=x.split(":")
i=dec.decode(i)
j=dec.decode(j)
syn[i]=j
Recognizer.staticSynonymes=syn
def normalise(self,mot):
"""normalise un mot"""
if(len(mot)==0):
return mot
mot=mot.lower()
mot.replace(u"œ","oe")
for x in u",;:!?./§%*¨^£¤~#|`_\\/<>":
mot = mot.replace(x,"")
if self.ACCENT:
for x in range(len(self.accents)):
mot = mot.replace(self.accents[x],self.equival[x])
if self.LIAISON:
mot = mot.replace("'"," ")
mot = mot.replace("-"," ")
if self.PLURIEL:
mot=self.singulariser(mot)
mot=mot.strip()
return mot
def traduire(self,mot,rep):
"""normalise les mots"""
mot=self.normalise(mot)
rep=self.normalise(rep)
return mot,rep
def accepter(self,mot,rep):
mot,rep = self.traduire(mot,rep)
if (mot == rep):
return True
if self.DESORDRE:
bonnes=0
if len(mot)<len(rep)+3:
for x in mot:
if x in rep:bonnes+=1
if bonnes>len(rep)-2:
return True
if self.DYSLEXIE:
bonnes=0
total=len(rep)
if self.DESORDRE:
for x in mot:
if x in rep:bonnes+=1
else :
if len(mot)<total+1:
for x in range(len(mot)):
if mot[x]==rep[x]:bonnes+=1
else:
for x in range(len(rep)):bonnes+=1
if float(bonnes)/float(total)>self.DISLEXIE:
return True
elif bonnes==total-1 and total>3:
return True
else : return False
else:
if mot==rep:return True
if rep in Recognizer.staticSynonymes.keys():
for syn in Recognizer.staticSynonymes[rep].split(","):
syn=self.normalise(syn)
if self.accepter(mot,syn):
return True
return False
def singulariser(self,mot):
if(len(mot)==0):
return mot
if mot[-1] in ["s","x"]:
if mot[-1]=="s":
mot=mot[:-1]
elif mot[-3:-1]=="ou":
mot=mot[:-1]
elif mot[-3:-1]=="au":
mot=mot[:-2]+"l"
return mot
def isInList(self,mot,corr):
"""
Savoir si le mot est correct
"""
#if len(mot)<1: print "false length1"#return False
if len(mot)<1: return False
mot=self.normalise(mot)
corr=corr.split("(")[0]#retire les commentaires
corr=self.normalise(corr)
if mot==corr: return True
lMots=mot.split()
solutions=corr.split(",")
for corr in solutions:
good=0
if len(lMots)==len(corr.split()):
#cas normal
for x in lMots:
mot1,corr1=self.traduire(mot,corr)
if mot1==corr1:good+=1
elif self.accepter(mot1,corr1):good+=1
if good==len(lMots):return True
elif len(lMots)>len(corr.split()):
pass
#print "taille incorrecte"
# return False
else :
#compter le nombre de mots justes
for x in lMots:
mot1,corr1=self.traduire(mot,corr)
if mot1==corr1:good+=1
elif self.accepter(mot1,corr1):good+=1
if good==len(lMots):return True
elif self.DYSLEXIE and good:return True
return False
##if corr.find("(")!=-1:
## corr=corr.split("(")[0]
## if mot==corr:
## return True
## elif mot in corr.split()[1:-1]:
## return True
## ## elif mot in corr.split("-"):
## ## return True
## elif len(mot)>3 and mot in corr.split() and len(corr)>15:
## return True
## elif mot in corr.split(";"):
## return True
## elif mot in corr.split(","):
## return True
## else :
## i=0.0
## for x in range(len(mot)):
## if x < len(corr)-2 and x >0:
## if mot[x] in corr[x-1:x+1]:
## i+=1.0
## elif x<len(corr)-1 :
## if mot[x]==corr[x]:
## i+=1.0
## x=i/float(len(corr))*100.0
## if x>79.9:
## return True