1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import logging, os, os.path, string, sys
20 import numpy.oldnumeric as Numeric
21 import numpy.numarray as numarray
22 import numpy.numarray.ma as ma
23
24
26 """ Lit un fichier dans le dossier courant et renvoie une chaine """
27 path=os.path.join(os.getcwd(),"MediteAppli","test",name)
28 f = open(path)
29 res = f.read()
30 f.close()
31 return res
32
33
35 comite = _readFile('comite.txt')
36 papers = _readFile('papers.txt')
37 lcom = comite.splitlines()
38 logging.debug('# comite='+str(len(lcom)))
39 sepTable = string.maketrans("""\r\n\t"""," ")
40 i = 0
41 for c in lcom:
42 liste_nom = c.split()
43 nom = liste_nom[-1]
44 pos = papers.find(nom)
45 if pos != -1:
46 p = papers[pos-30:pos+70]
47 p = p.translate(sepTable)
48 logging.debug(c+' / '+ p +'\n')
49 i += 1
50 logging.debug('# comite member with paper='+str(i))
51
53 nb =1.0/(11)
54 i=0
55 while i < 10000 and nb > 0:
56 nb1 = 60.0*nb
57 ar = int(nb1)
58 nb = nb1-ar
59 assert 0 <= ar <60
60 logging.debug('nb1=%f / ar=%f / nb=%f',nb1,ar,nb)
61 i+=1
62 logging.debug('i='+str(i))
63
65 import numarray
66 a = numarray.zeros(5,numarray.Int8)
67 for i in range(len(a)):
68 a[i] = i
69 b = numarray.ones(5,numarray.Int8)
70 c = numarray.zeros(5,numarray.Int8)
71 print a
72 print b
73 print a-b
74 print b-a
75 print numarray.minimum(a,b)
76 taille = len(a)
77 numarray.logical_and(b-a,c, a)
78 print a
79
80
81 a = numarray.array([1,2,3,4])
82 print numarray.sum(a)
83
85 import numarray
86 pos = numarray.array([1,5,9,1])
87 x = numarray.array([0,0,0,0,0,0,0,0,0,0])
88 numarray.put(x, pos, [1,1,1,1])
89 print x
90 def d(test,i):
91 return where(test==i,1,0)
92 def d2(test):
93 d(test)
94
95
96 b = numarray.zeros(10)
97 a = numarray.zeros((4,10))
98 print a
99 for i in xrange(4-1,-1,-1):
100 pos2 = pos[i]
101 c = numarray.where(numarray.arange(10)==pos2,1,0)
102 numarray.add(b,c,b)
103 a[i] = b
104 print a
105
107 ma = Numeric.array([[1,2,3],[4,5,6]])
108 for x in ma: print x
109
111 """ Lit un fichier dans le dossier courant et renvoie une chaine """
112 path=os.path.join(os.getcwd(),"MediteAppli","test", "textes",name)
113 f = open(path)
114 res = f.read()
115 f.close()
116 return res
117
119 a1 = [-1,1,2,3,10,5]
120 b1 = [-1,10,3,2,1,6]
121
122 a = numarray.array(a1, type = 'Float32')
123 b = numarray.array(b1)
124
125
126 c = numarray.where((a / b > 0.5) , a, 0)
127 c2 = numarray.where((a / b < 2) , b, 0)
128 c3 = numarray.logical_and(c,c2)
129 print c,c2, c3
130
131 c4 = numarray.where(c3,a,0)
132 c5 = numarray.where(c3,b,0)
133 c6 = numarray.logical_and(c4,c5)
134 print c4, c5, c6
135
136
137
138
139
140 import alignement
141
142
143
144 align = alignement.AlignAstarRecur2(_readFile('CB_experience.txt')+_readFile("CB_Texte_de_1857.txt"))
145 c7,c8 = align.extractRemplacements(a1,b1)
146
147 c9 = numarray.array([0,0,0])
148
149 print c7,c8
150 print numarray.argmax(a1)
151 print numarray.array()
152 print len([])
153
155 a = ['aa','bb']
156 b = ['aa','bb']
157 print a == b
158
160 a = [ 100,80,75,74,70,60,50,30,22,20,15,10,2]
161 b = [2,5,10,12,13,19,20,30,50]
162 import bisect
163 print bisect.bisect_left(a,11)
164 print bisect.bisect_right(b,2)
165
166
168 import math
169 print math.log(0L)
170
172 if x == 0:
173 return 1
174 else:
175 return x * fact(x-1)
176
178 return reduce(lambda x,y:x*y,range(1,n+1))
179
180 F ={}
182 n1 = n
183 try:
184 return F[n]
185 except KeyError:
186 pass
187 f = 1
188 while (n > 0):
189 f = f * n
190 n = n - 1
191 F[n1] = f
192 return f
193
195 import math
196
197 n = 7 ; m = 7
198 t1 = math.pow(2,n) * math.pow(2,m)
199 t2 = 0
200 for i in xrange(1,m+1):
201
202 for j in xrange(1,i+1):
203
204 new_t2 = (fact3(i)/fact3(i-j)) * math.pow(3,j)
205 t2 += new_t2
206 print 'i',i,new_t2
207 print t1,t2,t1+t2
208
210 import math
211 n = 7 ; m = 7
212 f_n = fact3(n) ; f_m = fact3(m)
213 t = 0
214 for i in xrange(1,m):
215 t2 = 0
216 for j in xrange(1,n):
217 c2 = f_n / (fact3(j) * fact3(n-j))
218
219 new_t2 = 1
220 t2 += c2 * new_t2
221 print 'i:',i,' / c2:',c2,' / new_t2:',new_t2,' / c2*new_t2:',c2*new_t2,' / t2:',t2
222 c1 = f_m / (fact3(i) * fact3(m-i))
223 t += c1 * t2
224 print 'i:',i,' / c1:',c1,' / t2:',t2,' / c1*t2:',c1*t2
225 print 't:',t
226
228 assert n >= k
229 if k < 0: k = 0
230 return fact3(n) / (fact3(n-k) * fact3(k))
231
233 assert n >= k
234 return fact3(n) / fact3(n-k)
235
237 import math
238 n = 7 ; m = 7
239 f_n = fact3(n-1) ; f_m = fact3(m-1)
240 t = 0
241 for i in xrange(1,m):
242 t2 = 0
243 for j in xrange(1,n):
244 c2 = comb(m-1,j-1)
245
246
247 new_t2 = (fact3(i)/fact3(i-j))
248
249 c1 = comb(n-1,i-1)
250 t2 += c2 * new_t2 *c1
251 print 'i:',i,' / c2:',c2,' / new_t2:',new_t2,' / c2*new_t2:',c2*new_t2,' / t2:',t2
252
253 t += t2
254 print 'i:',i,' / c1:',c1,' / t2:',t2,' / c1*t2:',c1*t2
255 print 't:',t
256
258 import math
259 n = 3 ; m = 3
260 f_n = fact3(n-1) ; f_m = fact3(m-1)
261 t = 0
262 for i in xrange(1,m):
263
264 for j in xrange(1,n):
265 c2 = comb(m-1,j-1)
266
267
268
269
270 c1 = comb(n-1,i-1)
271 new_t2 = 0 ; x = min(i,j)
272 for k in xrange(x+1):
273 new_t2 += math.pow(comb(x,k),2)
274 t += c1 * c2 * new_t2
275 print 'i:',i,' / c2:',c2,' / new_t2:',new_t2
276
277
278 print 'i:',i,' / c1:',c1
279 print 't:',t
280
281
282 l = [2, [2, 5, [3, 3, 1], [0, 0], 0], [5, 5, [28, 28,1], [0, 0], 1]]
283
285
286 try:
287 reduce(aplatir,x,s)
288 except:
289
290 s.append(x)
291
292 return s
293 import math
294
295
296 import cPickle
297 fichier_cp = os.path.join(os.getcwd(),'cp.pkl')
298 if False and os.path.isfile(fichier_cp):
299 pkl_file = open(fichier_cp, 'rb')
300 CP = cPickle.load(pkl_file)
301 pkl_file.close()
302 else:
303 CP = {}
304
306 assert nb_choix <= taille_seq
307 try:
308 return CP[(nb_choix,taille_seq)]
309 except KeyError:
310 pass
311 liste_nuplet = []
312 for i in xrange(taille_seq,0,-1):
313 if nb_choix == 1:
314 liste_nuplet.append([i])
315 else:
316 l2 = choose_pos(nb_choix-1,taille_seq-1)
317
318
319 l3 = map(lambda elt: aplatir([],[i,elt]), l2)
320
321
322
323
324
325
326
327
328 for x in l3:
329 assert len(x) == nb_choix, (x,nb_choix)
330 liste_nuplet.extend(l3)
331
332 l4 = filtre2(liste_nuplet, taille_seq, nb_choix)
333
334 if not CP.has_key((nb_choix,taille_seq)):
335 logging.debug( 'BU CP[' +str((nb_choix,taille_seq))+ ']')
336 CP[(nb_choix,taille_seq)] = l4
337 output = open(fichier_cp, 'wb')
338 cPickle.dump(CP, output)
339 output.close()
340 return l4
341
342 -def filtre2(liste_nuplet, taille_seq, nb_choix):
343 l4=[]
344 for x in liste_nuplet:
345 previous = taille_seq+1 ; OK = True ; totalEcart = maxEcart = 0
346 for pos in x:
347
348 ecart = abs(previous-pos)
349 totalEcart += ecart-1 ; maxEcart = max(maxEcart,ecart-1)
350 if (ecart > 2) or (previous <= pos):
351 OK = False
352
353 previous = pos
354
355 ecart = abs(previous-0)
356 totalEcart += ecart-1 ; maxEcart = max(maxEcart,ecart-1)
357 if (ecart > 2) or (previous <= 0):
358 OK = False
359 if OK:
360 l4.append(x)
361 assert totalEcart <= 2*nb_choix+1,(totalEcart, 2*nb_choix+1, taille_seq, nb_choix,x)
362
363
364
365 return l4
366
367 -def filtre1(liste_nuplet, taille_seq, nb_choix):
368 l4 = []
369 for x in liste_nuplet:
370
371 i = 0 ; totalEcarts = 0
372 sorted = True ; following = True ; ecartOK = True ; ecartExtremes = True
373 for i in xrange(0,len(x)-1):
374 if x[i] <= x[i+1]:
375 sorted = False
376 break
377 ecart = abs(x[i]-x[i+1])
378 totalEcarts += ecart
379 if ecart >= 3:
380 following = False
381 break
382 if sorted and following:
383 totalEcarts += abs(taille_seq-x[0]) + abs(1-x[-1])
384 if totalEcarts > nb_choix+1:
385 ecartOK = False
386
387 if (abs(taille_seq-x[0])>=2) or (abs(1-x[-1])>=2):
388 ecartExtremes = False
389
390
391 if ecartOK and ecartExtremes:
392 l4.append(x)
393
394
395 return l4
396
398 t = 0
399 for k in xrange(1,m):
400 t += comb(n,k) * math.pow(2,k)
401 print 't140:',math.pow(2,n-1)*math.pow(2,m-1)*t
402 print 't141:',math.pow(2,n-1)*math.pow(2,m-1)*comb(n-1,m-1) * math.pow(2,min(n-1,m-1))
403 print 't142:',math.pow(2,n-1)*math.pow(2,m-1)*comb(n-1,m-1) * math.pow(2,min(n-1,m-1))
404 print 't143:',math.pow(2,n)*math.pow(2,m)*comb(n,m) * math.pow(2,min(n,m))
405 t2 = t3 = 0
406 for i in xrange(1,m+1):
407
408
409 for j in xrange(1,n+1):
410
411
412
413 c1 = comb(m-1,i-1)
414 c2 = comb(n-1,j-1)
415 new_t2 = 0
416 x = min(i,j) ; y = max(i,j)
417
418 new_t2 = 0 ; new_t3 = 0
419 for k in xrange(1,x+1):
420 new_t2 += comb(y,k) * arang(x,k) * math.pow(2,k)
421 new_t3 += arang(y,k) * comb(x,k) * math.pow(3,k)
422
423 new_tt2 = c1 * c2 * new_t2
424 new_tt3 = c1 * c2 * new_t3
425 t2 += new_tt2
426 t3 += new_tt3
427
428
429 logging.debug('t14 t2:'+str(t2))
430 logging.debug('t14 t3:'+str(t3))
431 print 't14 t2:',t2
432 print 't14 t3:',t3
433 sys.stdout.flush()
434
436 t2 = t3 = t4 = 0
437 for i in xrange(1,m+1):
438 z = min(n+1,2*i+2) ; z2 = z-1
439
440 for j in xrange(1,z):
441 assert j <= 2*i+1
442
443
444
445 c1 = comb(m-1,i-1)
446 c2 = comb(z2-1,j-1)
447 new_t2 = 0
448 x = min(i,j) ; y = max(i,j)
449
450 new_t2 = 0 ; new_t3 = 0 ; new_t4 = 0
451 b_k = int(math.floor(y / 2.0))
452 for k in xrange(b_k,x+1):
453 new_t2 += comb(y,k) * arang(x,k) * math.pow(2,k)
454 new_t3 += arang(y,k) * comb(x,k) * math.pow(2,k)
455
456
457 new_tt2 = c1 * c2 * new_t2
458 new_tt3 = c1 * c2 * new_t3
459 new_tt4 = c1 * c2 * comb(y,x) * arang(x,x) * math.pow(2,k)
460 t2 += new_tt2
461 t3 += new_tt3
462 t4 += new_tt4
463
464
465 logging.debug('t15 t2:'+str(t2))
466 logging.debug('t15 t3:'+str(t3))
467 print 't15 t2:',t2
468 print 't15 t3:',t3
469 print 't15 t4:',t4
470
471 import bisect
473
474
475 A = "Ce matin le chat observa de petits oiseaux dans les arbres."
476 B = "Le chat était en train d'observer des oiseaux dans les petits arbres ce matin. Il observa les oiseaux pendant deux heures."
477
478
479 dicoRepet = {}
480 dicoRepet = _extractRepet(A, B, dicoRepet)
481 dicoRepet = _extractRepet(B, A, dicoRepet)
482
483 dicoRepet = _filterDico(dicoRepet, A, B)
484
485
486 i = 1 ; cumul = 0
487 while i <= len(dicoRepet):
488 cumul += _afficheDic(dicoRepet,i)
489 print '---------------------------------------'
490 i += 1
491 print 'total = ',cumul
492
494 nouveauDicoRet = {}
495 for longueur, dico_chaine in dicoRepet.iteritems():
496 nouveauDicoRet[longueur] = {}
497 for chaine,(liste_pos1,liste_pos2) in dico_chaine.iteritems():
498 l1 = [] ; l2 = []
499 for i in liste_pos1:
500 if chaine == A[i:i+longueur]:
501 l1.append(i)
502 for j in liste_pos2:
503 if chaine == B[j:j+longueur]:
504 l2.append(j)
505 assert len(l1) > 0 and len(l2) > 0
506 nouveauDicoRet[longueur][chaine] = l1, l2
507 return nouveauDicoRet
508
510 cumul = 0
511 liste_alpha = []
512 for cle,item in dicoRepet[longueur].iteritems():
513
514 bisect.insort_right(liste_alpha, cle)
515 a = liste_alpha
516 liste_alpha.sort()
517 assert a == liste_alpha
518 for chaine in liste_alpha:
519 liste_pos1, liste_pos2 = dicoRepet[longueur][chaine]
520 print longueur,' / ',chaine,' / ',liste_pos1,len(liste_pos1),' / ',liste_pos2,len(liste_pos2)
521 cumul += len(liste_pos1) + len(liste_pos2)
522 print longueur,' / ',cumul
523 return cumul
524
526
527 for longueur in xrange(1,len(sequence1)):
528 i = 0
529 while i < len(sequence1) - longueur + 1:
530 chaine1 = sequence1[i:i+longueur]
531
532 if longueur <= len(sequence2):
533 j = 0
534 while j < len(sequence2) - longueur +1:
535 chaine2 = sequence2[j:j+longueur]
536 if chaine1 == chaine2:
537 if not dicoRepet.has_key(longueur):
538 dicoRepet[longueur] = {}
539 if not dicoRepet[longueur].has_key(chaine1):
540 dicoRepet[longueur][chaine1] = [],[]
541 liste_pos1, liste_pos2 = dicoRepet[longueur][chaine1]
542 if i not in liste_pos1:
543 bisect.insort_right(liste_pos1, i)
544 if j not in liste_pos2:
545 bisect.insort_right(liste_pos2, j)
546 dicoRepet[longueur][chaine1] = liste_pos1, liste_pos2
547 j += 1
548 i += 1
549 return dicoRepet
550
551
552 if __name__ == '__main__':
553 logging.basicConfig(level=logging.DEBUG,
554 format='%(asctime)s %(levelname)s %(message)s',
555
556 filename=os.path.join(os.getcwd(),'log_temp.txt'),
557 filemode='w')
558 console = logging.StreamHandler()
559 console.setLevel(logging.INFO)
560
561
562
563
564
565
566
567 m = 10; n = 100
568
569
570
571 test16()
572