1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import psyco,bisect,string,logging,os,os.path, time,unittest
23 import contract
24 import numpy.numarray as numarray
25 import MediteAppli.suffix_tree, test_data
26 contract.checkmod(MediteAppli.suffix_tree)
27
29 """Teste les SuffixTree"""
31 td = test_data.Test_Data()
32 self.texte = "cacaoccaocaococaica"
33
34 sequences = self.texte
35
36 a = time.time()
37 self.st = MediteAppli.suffix_tree.SuffixTree(sequences)
38 b = time.time()
39 logging.debug('construction st done')
40 print 'temps construction = ',b-a
41
42 c = d = e = 0
43 for n in self.st.generate_leaves(): c += 1
44 for n in self.st.generate_inner_nodes(): d += 1
45 for n in self.st.generate_post_order_nodes(): e += 1
46 print 'size=',len(self.texte),'/ leaves=',c,'/ inner=',d,'/ all=',e
47 a = time.time()
48 self.LL = self.st.get_MEM()
49
50 b = time.time()
51 print 'temps MEM = ',b-a
52 for size in self.LL:
53 x = ''
54 for pos in self.LL[size]:
55 x += self.texte[pos:pos+size] +' '
56
57
61
62
63
65 """Teste les GeneralisedSuffixTree"""
75
76
77
78
79
80
81
83 texte1 = self.texte1.lower() ; texte2 = self.texte2.lower()
84 sepTable = string.maketrans("çéèàùâêîôûäëïöüÿÇÉÈÀÙÂÊÎÔÛÄËÏÖÜ","ceeauaeiouaeiouyCEEAUAEIOUAEIOU")
85 texte1 = texte1.translate(sepTable)
86 texte2 = texte2.translate(sepTable)
87 sepTable2 = string.maketrans(""" !\r,\n:\t;-?"'`()""","................")
88 texte1 = texte1.translate(sepTable2)
89 texte2 = texte2.translate(sepTable2)
90 self.dictPosSuppression={}
91 self.tabNbSuppression={}
92 texte1,texte2=self.preTraitSuppSep(texte1,texte2)
93 lt1 = len(texte1) ; lt2 = len(texte2)
94
95 sequences = [texte1,texte2]
96 logging.debug('debut')
97 a = time.time()
98
99
100 psyco.bind(MediteAppli.suffix_tree.GeneralisedSuffixTree)
101 self.st = MediteAppli.suffix_tree.GeneralisedSuffixTree(sequences)
102 b = time.time()
103 logging.debug('construction st done')
104
105
106
107
108
109
110
111
112
113 self.LL2 = self.st.get_MEM_index_chaine3(just_keep_words=True)
114
115
116
117
118
119 c = time.time()
120
121
122
123 logging.debug('temps construction = %f',b-a)
124 logging.debug('temps MEM = %f',c-b)
125
126
127
128
129
130
131
132 top = []
133 for (cle,longueur),liste_pos in self.LL2.iteritems():
134 nb = len(liste_pos) ; chaine = texte1[liste_pos[0]:liste_pos[0]+longueur]
135 assert cle == hash(chaine)
136 bisect.insort_left(top,(nb,chaine,liste_pos))
137 cumul_len = cumul_2 = cumul_3 = 0.0
138 top.reverse()
139 for x in top:
140 t = x[0]*len(x[1])
141 cumul_len += t
142 if x[0] <= 2: cumul_2 += t
143 else: cumul_3 += t
144
145 logging.debug('cumul_2 % texte total='+str(100*cumul_2/(lt1+lt2)))
146 logging.debug('cumul_2 % texte répété='+str(100*cumul_2/(cumul_2+cumul_3)))
147 logging.debug('cumul_3 % texte total='+str(100*cumul_3/(lt1+lt2)))
148 logging.debug('cumul_3 % texte répété='+str(100*cumul_3/(cumul_2+cumul_3)))
149 logging.debug('cumul_2+cumul_3 % texte total='+str(100*(cumul_2+cumul_3)/(lt1+lt2)))
150
151
152 texte = texte1+texte2
153 liste = texte.split('.')
154 dic = {}
155 for i in liste:
156 try:
157 dic[i] += 1
158 except KeyError:
159 dic[i] = 1
160 n=len(liste) ; liste = []
161 for mot,nb in dic.iteritems():
162 bisect.insort_right(liste,(nb,mot))
163 liste.reverse()
164 cumul = cumul_2 = cumul_3 = 0.0 ; nb_mot = 0
165 for (nb,mot) in liste:
166 nb_mot += nb
167 t = len(mot)*nb
168 if nb > 2: cumul_3 += t
169 elif nb == 2: cumul_2 += t
170 cumul += t
171
172 assert n == nb_mot
173 logging.debug('cumul_2 % texte total='+str(100*cumul_2/(lt1+lt2)))
174 logging.debug('cumul_2 % texte répété='+str(100*cumul_2/(cumul_2+cumul_3)))
175 logging.debug('cumul_3 % texte total='+str(100*cumul_3/(lt1+lt2)))
176 logging.debug('cumul_3 % texte répété='+str(100*cumul_3/(cumul_2+cumul_3)))
177 logging.debug('cumul_2+cumul_3 % texte total='+str(100*(cumul_2+cumul_3)/(lt1+lt2)))
178
179
180
181 cumul = cumul_2 = cumul_3 = cumul_nb = 0.0
182 ch_excel = [] ; ch_nb = []
183 stopWords = ['avec','dans','pour','sous','mais','encore','quand','cette','notre', 'votre', 'leur', 'leurs','donc','puis',
184 'comme', 'lorsque', 'puisque', 'quand', 'quoique', 'afin', 'ainsi', 'avant', 'comme',
185 'aucun', 'aucune', 'autre', 'certain', 'certaine', 'certaines', 'certains', 'chaque', 'plusieurs', "quelqu'",' quelque', 'quelques' ,
186 'telle', 'telles', 'tous', 'tout', 'toute', 'toutes', 'lequel', 'laquelle', 'lesquels', 'lesquelles',
187 'duquel', 'dequels', 'dequelles', 'auquel', 'auxquels', 'auxquelles',
188 'quel', 'quelle', 'quelles', 'quels',
189 'elle', 'nous', 'vous','elles',
190 'celui', 'celle', 'ceux', 'celles', 'ceci', 'cela',
191 'aucun', 'aucune', 'autre', 'autrui', 'certains', 'chacun', 'quiconque', 'rien']
192
193
194
195 ch4 = [] ; i = 0
196 global NOMB,CUM
197 for (nb,mot) in liste:
198 if len(mot)>3 and mot not in stopWords:
199 t = len(mot)*nb
200 if nb > 2: cumul_3 += t
201 elif nb == 2: cumul_2 += t
202 cumul += t
203 logging.debug(str((nb,mot))+' / cumul % texte total='+str(100*cumul/(lt1+lt2)))
204 ch_excel.append(str(nb)+';')
205 ch_nb.append(nb)
206 cumul_nb += nb
207 ch4.append(str(round(100*cumul/(lt1+lt2),2))+';')
208 if i< 200:
209 CUM[i] += round(100*cumul/(lt1+lt2),2) ; i += 1
210 NOMB += 1
211 ch_nb2 = [(0.0+nb)/(cumul_2+cumul_3) for nb in ch_nb]
212 ch_nb3 = [str(nb)+';' for nb in ch_nb2]
213 logging.debug('cumul_2 % texte total='+str(100*cumul_2/(lt1+lt2)))
214 logging.debug('cumul_2 % texte répété='+str(100*cumul_2/(cumul_2+cumul_3)))
215 logging.debug('cumul_3 % texte total='+str(100*cumul_3/(lt1+lt2)))
216 logging.debug('cumul_3 % texte répété='+str(100*cumul_3/(cumul_2+cumul_3)))
217 logging.debug('cumul_2+cumul_3 % texte total='+str(100*(cumul_2+cumul_3)/(lt1+lt2)))
218 logging.debug('chaine excel = '+''.join(ch_excel))
219 logging.debug('chaine excel2 = '+''.join(ch_nb3))
220 logging.debug('nb total = %d / nb moyen = %f / nb médian = %d',cumul_nb,cumul_nb/len(liste),ch_nb[len(ch_nb)/2])
221
224
225
226
228 """ Prétraitement supprimant plusieurs séparateurs se suivant et n'en laissant qu'un
229 Prend en entrée texte1 et texte2 et les renvoie traités """
230 debut=0
231 j=0
232 comptSuppression=0
233 res=[]
234 for texte in [texte1,texte2]:
235 i=0
236 lTexte=[]
237 while i<len(texte)-1:
238 if texte[i]=='.' and texte[i+1]=='.':
239 comptSuppression+=1
240
241 self.dictPosSuppression[i+debut]='.'
242 else:
243 lTexte.append(texte[i])
244
245
246 self.tabNbSuppression[j]=comptSuppression
247 j+=1
248 i+=1
249 lTexte.append(texte[i])
250 self.tabNbSuppression[j]=comptSuppression
251 j+=1
252 debut=i+1
253 res.append(''.join(lTexte))
254 self.tabNbSuppression[j]=comptSuppression
255 return res[0],res[1]
256
257 - def postTraitSuppSep2(self,listeOcc):
258 res=[]
259 for (occ1,occ2) in listeOcc:
260 Nocc1 = occ1+self.tabNbSuppression[occ1]
261 Nocc2 = occ2+self.tabNbSuppression[occ2]
262 res.append((Nocc1,Nocc2))
263 return res
264
266 """Teste les GeneralisedSuffixTree"""
268 self.td = test_data.Test_Data()
269 self.texte1 = self.td._readFile('chedidtapuscrit.txt') ; self.texte2 = self.td._readFile('chedidcor.txt')
270 self.texte1 = self.texte1[:8000] ; self.texte2 = self.texte2[:8000]
271
273 """Teste les GeneralisedSuffixTree"""
275 self.td = test_data.Test_Data()
276 self.texte1 = self.td._readFile('IP_823.txt') ; self.texte2 = self.td._readFile('Macadam.txt')
277 self.texte1 = self.texte1[:8000] ; self.texte2 = self.texte2[:8000]
278
280 """Teste les GeneralisedSuffixTree"""
282 self.td = test_data.Test_Data()
283 self.texte1 = self.td._readFile('CONDORCET_IMPOT_1.txt') ; self.texte2 = self.td._readFile('CONDORCET_IMPOT_10.txt')
284 self.texte1 = self.texte1[:8000] ; self.texte2 = self.texte2[:8000]
285
287 """Teste les GeneralisedSuffixTree"""
289 self.td = test_data.Test_Data()
290 self.texte1 = self.td._readFile('Condorcet_Esimpr1.txt') ; self.texte2 = self.td._readFile('Condorcet_Esimpr2.txt')
291 self.texte1 = self.texte1[:8000] ; self.texte2 = self.texte2[:8000]
292
294 """Teste les GeneralisedSuffixTree"""
296 self.td = test_data.Test_Data()
297 self.texte1 = self.td._readFile('Alth1v1.txt') ; self.texte2 = self.td._readFile('Alth1v2.txt')
298 self.texte1 = self.texte1[:8000] ; self.texte2 = self.texte2[:8000]
299
301 """Teste les GeneralisedSuffixTree"""
303 self.td = test_data.Test_Data()
304 self.texte1 = self.td._readFile('bernon1.txt') ; self.texte2 = self.td._readFile('bernon5-18_5.txt')
305 self.texte1 = self.texte1[:8000] ; self.texte2 = self.texte2[:8000]
306
308 """Teste les GeneralisedSuffixTree"""
310 self.td = test_data.Test_Data()
311 self.texte1 = self.td._readFile('dactylo-1.txt') ; self.texte2 = self.td._readFile('dactylo-11-rouge.txt')
312 self.texte1 = self.texte1[:8000] ; self.texte2 = self.texte2[:8000]
313
315 """Teste les GeneralisedSuffixTree"""
317 self.td = test_data.Test_Data()
318 self.texte1 = self.td._readFile('Tout-vieux orig.txt') ; self.texte2 = self.td._readFile('tout-vieux1909.txt')
319 self.texte1 = self.texte1[:8000] ; self.texte2 = self.texte2[:8000]
320
339
340 -def trace(commande,dic_locals):
341 import profile,pstats,sys
342
343 profile.runctx(commande,globals(), dic_locals,'c:\workspace\medite\statFile')
344 s = pstats.Stats('c:\workspace\medite\statFile')
345 s.sort_stats('time')
346 s.print_stats()
347 sys.stderr.flush()
348 sys.stdout.flush()
349
350 CUM = numarray.zeros(200,numarray.Float)
351 NOMB = 0
352 if __name__ == '__main__':
353 logging.basicConfig(level=5,
354 format='%(asctime)s %(levelname)s %(message)s',
355
356 filename=os.path.join(os.getcwd(),'log.txt'),
357 filemode='w')
358 console = logging.StreamHandler()
359 console.setLevel(logging.INFO)
360 log = logging.FileHandler(filename=os.path.join(os.getcwd(),'log_MA.txt'),mode='w')
361 log.setLevel(logging.CRITICAL)
362 log.setFormatter(logging.Formatter(''))
363 logging.getLogger('').addHandler(log)
364 _test()
365 print CUM
366 a = numarray.around(CUM / NOMB,2) ; ch = []
367 for nb in a:
368 ch.append(str(nb)+';')
369
370 logging.critical(''.join(ch))
371