-
Notifications
You must be signed in to change notification settings - Fork 1
/
compte.py
924 lines (739 loc) · 25.1 KB
/
compte.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
# Copyright 2004 Laurent GUEGUEN
#
# This file is part of Sarment.
#
# Sarment 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.
#
# Sarment 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with Sarment; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import string
import math
import random
import sequence
########################################################################
########################################################################
############# classe _Arbre dont heritent Compte et Proportions
class _Arbre:
"Tree of words, structured as a catalog of _Arbre"
def __init__(self, **kw):
"""Keyword argument:
fic -- Build from filename f.
"""
self.__cat={}
self.__nbe=0
if kw.has_key('fic'):
self.read_nf(kw['fic'])
def read_nf(self, nf):
"Build from filename nf."
self.__init__()
try:
f = open(nf,'rU')
except IOError, e:
print "Unknown file: ", nf
return
s=f.readline()
while s:
t=s.split()
if t!=[]:
if len(t)<2:
print "Problem at line: " + s
self.__init__()
f.close()
return
self.__add(t[0],float(t[1]))
s=f.readline()
f.close()
def __somme(self):
"Return the sum of all nodes."
b=self.__nbe
for f in self.__cat.values():
b+=f.__somme()
return b
def __idiv__(self, v):
"""Divide each node by value v (x.__idiv(v) <==> x/= v)."""
if v!=0:
self.__nbe/=v
for f in self.__cat.values():
f/=v
###########################################
#### sorties
def lg_max(self):
"Return the length of the longest word."
if self.__cat=={}:
return 0
m=0
for c in self.__cat.values():
n=c.lg_max()
if m<n:
m=n
return m+1
def alph(self,t=[]):
"Return the list of the letters."
s=t[:]
for c in self.__cat:
if c!="^" and c not in s:
s.append(c)
s=self.__cat[c].alph(s)
return s
def __add(self,a,b):
if isinstance(self,Compte):
self._Compte__add(a,b)
elif isinstance(self,Proportion):
self._Proportion__add(a,b)
def is_empty(self):
"Return True if dictionary is empty."
return self.__cat=={}
##################################################################
##################################################################
##################################################################
##### classe Compte
class Compte(_Arbre):
def add_seq(self, s, lg, **kw):
"""Add the counts of words at most lg-length from sequence s.
Needs operator s[].
Character '^' is for beginnings and ends of sequences.
Keyword argument:
deb -- Count only after position deb (>=0) included;
fin -- Count only before position fin (<len()) included;
alpha -- Count only words which letters are in string alpha. If
alpha='', do not consider alpha;
fact -- Count each word fact (default: 1).
"""
if lg<=0:
return
f=kw.get('fact',1)
fin=kw.get('fin',len(s)-1)
deb=kw.get('deb',0)
if deb<0 or deb>fin:
return
alpha=kw.get('alpha','')
if alpha=='':
self.__add_ds_seq(s,deb,fin,lg,f)
else:
dalpha={}
for x in alpha:
dalpha[x]=1
self.__add_ds_seq_sur_alph(s,deb,fin,lg,dalpha,f)
def add_pseudo(self, x, val=1):
"""Add the counts of the word x with optionnal value val=1.
Only the count of the given word are added, and not its prefixes.
"""
self.__add(x,val)
self.__add(x[:-1],-val)
def __iadd__(self, c):
"Add counts of Compte c (x.__iadd__(c) <==> x+=c)."
self._Arbre__nbe+=c._Arbre__nbe
for i,f in c._Arbre__cat.items():
self._Arbre__cat.setdefault(i,Compte()).__iadd__(f)
return self
##########################################
#### donnees/calculs
def __getitem__(self,s):
"""Return count of word s. '.' is the wildcard, "^" excepted.
(x.__getitem__(s) <==> x[s]).
"""
if len(s)==0:
return self._Arbre__nbe
if s[0]=='.':
a=0
for i in self._Arbre__cat:
if i!="^":
a+=self._Arbre__cat[i][s[1:]]
return a
if self._Arbre__cat.has_key(s[0]):
return self._Arbre__cat[s[0]][s[1:]]
else:
return 0
def prop(self, **kw):
"""Return the corresponding Proportion:
Keyword argument:
lpost -- length of the posterior words (ie on the right) (default: max);
lprior -- length of the prior words (ie on the left) (default: 0).
"""
p=Proportion()
p.read_Compte(self, **kw)
return p
def restrict_to(self, alph):
"""Return the Compte restricted to letters of string alph.
Keep the ends ^.
"""
nc=Compte()
nc._Arbre__nbe=self._Arbre__nbe
for i,f in self._Arbre__cat.items():
nc._Arbre__nbe-=f._Arbre__nbe
if i=="^" or i in alph:
c=f.restrict_to(alph)
nc._Arbre__cat[i]=c
nc._Arbre__nbe+=c._Arbre__nbe
return nc
def strip(self,d=1):
"""Return the Compte without characters ^."""
nc=Compte()
nc._Arbre__nbe=self._Arbre__nbe
for i,f in self._Arbre__cat.items():
if i!="^":
nc._Arbre__cat[i]=f.strip(0)
elif d:
nc._Arbre__nbe-=f._Arbre__nbe
return nc
def rstrip(self,d=1):
"""Return the Compte without ending characters ^."""
nc=Compte()
a=0
for i,f in self._Arbre__cat.items():
if i!="^":
nc._Arbre__cat[i]=f.rstrip(0)
a+=f._Arbre__nbe
elif d:
c=f.rstrip(True)
if c:
nc._Arbre__cat["^"]=c
a+=c._Arbre__nbe
if not d:
nc._Arbre__nbe=self._Arbre__nbe
else:
nc._Arbre__nbe=a
if nc._Arbre__nbe==0:
return
else:
return nc
def min(self, a):
"""Return the Compte of the minimum between a and self.
"""
c=Compte()
for i,f in self._Arbre__cat.items():
if a._Arbre__cat.has_key(i):
c._Arbre__cat[i]=f.__min(a._Arbre__cat[i])
for f in c._Arbre__cat.values():
c._Arbre__nbe+=f._Arbre__nbe
return c
def max(self, a):
"""Return the Compte of the maximum between a and self.
"""
c=Compte()
c._Arbre__nbe=max(self._Arbre__nbe,a._Arbre__nbe)
lk=self._Arbre__cat.keys()
for k in a._Arbre__cat:
if not self._Arbre__cat.has_key(k):
lk.append(k)
for i in lk:
if self._Arbre__cat.has_key(i):
if a._Arbre__cat.has_key(i):
c._Arbre__cat[i]=self._Arbre__cat[i].max(a._Arbre__cat[i])
else:
c._Arbre__cat[i]=self._Arbre__cat[i].copy()
else:
c._Arbre__cat[i]=a._Arbre__cat[i].copy()
return c
def intersects(self, a):
"""Return the Compte of the counts of self restricted to the
words of a.
"""
c=Compte()
for i in a._Arbre__cat:
if self._Arbre__cat.has_key(i):
b=self._Arbre__cat[i].__intersects(a._Arbre__cat[i])
if b._Arbre__nbe!=0:
c._Arbre__cat[i]=b
for f in c._Arbre__cat.values():
c._Arbre__nbe+=f._Arbre__nbe
return c
def copy(self):
"Return a copy of this Compte"
c=Compte()
c._Arbre__nbe=self._Arbre__nbe
for i,f in self._Arbre__cat.items():
c._Arbre__cat[i]=f.copy()
return c
###########################################
#### sorties
def pref(self, l):
"Return the string of counts of l-length words."
if l<0:
return ""
else:
return self.__pref(l,"")
def next(self, s):
"Return the list of [letter, value] which s in the prefix."
if len(s)==0:
l=[]
for i,j in self._Arbre__cat.items():
l.append([i,j._Arbre__nbe])
return l
else:
if self._Arbre__cat.has_key(s[0]):
return self._Arbre__cat[s[0]].next(s[1:])
else:
return []
def has_prefix(self, s):
"""Return True iff s is a STRICT prefix (^ excluded), else False."""
if len(s)==0:
l=len(self._Arbre__cat)
return not (l==0 or (l==1 and self._Arbre__cat.has_key(["^"])))
else:
if self._Arbre__cat.has_key(s[0]):
return self._Arbre__cat[s[0]].has_prefix(s[1:])
else:
return False
def __str__(self):
"""(x.__str__()<==> str(x))"""
return self.__ecrit("")
def words(self,s=""):
"Return the list of the words."
if self._Arbre__cat=={}:
return [s]
if self._Arbre__nbe!=0 and s!="":
l2=[s]
else:
l2=[]
for c,f in self._Arbre__cat.items():
l2+=f.words(s+c)
return l2
################################################
### prive
def __ecrit(self,st):
if self._Arbre__cat=={}:
return st+"\t"+str(self._Arbre__nbe)+"\n"
st2=""
a=self._Arbre__nbe
for i,c in self._Arbre__cat.items():
st3=c.__ecrit(st+i)
st2+=st3
a-=c._Arbre__nbe
if a>0:
st2+=st+"\t"+str(a)+"\n"
return st2
def __add_ds_seq(self, s, deb, fin, lg, f):
""" private """
if fin>=len(s) or deb<0:
return
self.__add_suff(s,deb,deb+lg-2,f)
i=deb
while i<=fin-lg+1:
self.__add_word(s,i,i+lg-1,f)
i+=1
while i<=fin:
self.__add_pref(s,i,fin,lg,f)
i+=1
def __add_ds_seq_sur_alph(self, s, deb, fin, lg, dalph, f):
""" private """
if dalph=={}:
self.__add_ds_seq(s,deb,fin,lg,f)
return
if fin>=len(s) or deb<0:
return
i=fin
j=0
while i>=deb:
if not dalph.has_key(s[i]):
if j!=0:
self.__add_suff(s,i+1,i+j,f)
j=0
else:
self.__add_pref(s, i, i+j, lg, f)
if j<lg-1:
j+=1
i-=1
while j>=1:
self.__add_suff(s,i+1,i+j,f)
j-=1
def __add(self, st, val=1):
"""Recursively increment Compte, with word st and value val=1.
Needs st[] and len(st)."""
self.__add_word(st,0,len(st),val)
def __add_word(self,s,i,j,f):
"""Add $1[$2:$3+1], with value $4."""
self._Arbre__nbe+=f
if i<len(s) and j>=i:
self._Arbre__cat.setdefault(s[i],Compte()).__add_word(s,i+1,j,f)
def __add_pref(self, s, i, j, lg, f):
"""Add $1[$2:$3+1]+'^'*($4-($3-$2)), with value $5."""
self._Arbre__nbe+=f
if j>=i and lg>=1:
self._Arbre__cat.setdefault(s[i],Compte()).__add_pref(s,i+1,j,lg-1,f)
elif lg>=1:
self._Arbre__cat.setdefault("^",Compte()).__add("^"*(lg-1),f)
def __add_suff(self, s, i, j, f):
"""Add '^'+$1[$2:$3+1], with value $4."""
self._Arbre__nbe+=f
self._Arbre__cat.setdefault("^",Compte()).__add_word(s,i,j,f)
def __pref(self, l, st):
if l==0:
return st+"\t"+str(self._Arbre__nbe)+"\n"
if self._Arbre__cat=={}:
return ""
st2=""
for i,c in self._Arbre__cat.items():
st3=c.__pref(l-1,st+i)
st2+=st3
return st2
def __min(self, a):
"""Private part of min."""
c=Compte()
c._Arbre__nbe=min(self._Arbre__nbe,a._Arbre__nbe)
for i,f in self._Arbre__cat.items():
if a._Arbre__cat.has_key(i):
c._Arbre__cat[i]=f.__min(a._Arbre__cat[i])
return c
def __intersects(self, a):
"""Private part of intersects."""
c=Compte()
c._Arbre__nbe=self._Arbre__nbe
for i in a._Arbre__cat:
if self._Arbre__cat.has_key(i):
b=self._Arbre__cat[i].__intersects(a._Arbre__cat[i])
if b._Arbre__nbe!=0:
c._Arbre__cat[i]=b
return c
##################################################################
##################################################################
##################################################################
##### classe Proportion
class Proportion(_Arbre):
def __init__(self, **kw):
"""Keyword arguments:
fic -- Build from filename fic;
str -- Build from string str.
"""
_Arbre.__init__(self)
self.__post=Post()
if kw.has_key('fic'):
self.read_nf(kw['fic'])
elif kw.has_key('str'):
self.read_str(kw['str'])
def read_nf(self, nf):
"Read file of name nf."
self.__init__()
try:
f = open(nf,'rU')
except IOError, e:
print "Unknown file: ", nf
return
s=f.readline()
while s:
t=s.split()
if t!=[]:
if len(t)<2:
print "Problem at line: " + s
self.__init__()
f.close()
return
d=t[0].split("|")
if len(d)!=2:
print "Problem at line: " + s
self.__init__()
f.close()
return
self.__add(d[0],d[1],float(t[1]))
s=f.readline()
f.close()
def read_str(self, st):
"""Read string st, at same format as the file's."""
self.__init__()
lst=st.split("\n")
for s in lst:
t=s.split()
if t!=[]:
if len(t)<2:
print "Problem at line: " + s
self.__init__()
f.close()
return
d=t[0].split("|")
if len(d)!=2:
print "Problem at line: " + s
self.__init__()
f.close()
return
self.__add(d[0],d[1],float(t[1]))
def __iadd__(self, p):
"""Add to self the Proportion p."""
lst=str(p).split('\n')
for s in lst:
t=s.split()
if len(t)==2:
lt=t[0].split("|")
self.__add(lt[0],lt[1],float(t[1]))
self.__somme_a_un()
return self
def __add(self, pr, po, val):
"""Add to the Proportion prior pr and posterior po, with value
val. Needs pr[:] and po[:]"""
if len(pr)==0:
self.__post._Post__add(po,val)
elif pr[-1]=="^":
self._Arbre__cat.setdefault("^",Proportion()).__post._Post__add(po,val)
else:
self._Arbre__cat.setdefault(pr[-1],Proportion()).__add(pr[:-1],po,val)
def read_Compte(self, com, **kw):
"""Build from Compte com:
Keyword argument:
lpost -- length of the posterior words (ie on the right) (default: max);
lprior -- length of the prior words (ie on the left) (default: 0).
"""
lprior=kw.get('lprior',0)
if lprior<0:
print "Bad prior", lprior
return
lpost=kw.get('lpost',-1)
self.__init__()
lst=str(com).split('\n')
for st in lst:
t=st.split()
if len(t)==2:
if lpost>0:
b1=0
while b1>=0:
if not (lprior+lpost-b1>len(t[0]) or
(t[0].startswith("^",lprior-b1) and
(t[0].endswith("^",0,lprior-b1) or lprior==b1))):
tps=t[0][lprior-b1:lprior+lpost-b1]
if lpost+lprior-b1>len(t[0]):
if t[0].endswith("^",):
self.__add(t[0][:lprior-b1],
tps+"^"*(lpost-len(tps)), float(t[1]))
else:
self.__add(t[0][:lprior-b1],tps, float(t[1]))
if t[0].startswith("^",0) and b1<lprior-1:
b1+=1
else:
b1=-1
else:
b1=0
while b1>=0:
if not (t[0].startswith("^",lprior-b1) and
(t[0].endswith("^",0,lprior-b1) or lprior==b1)):
tps=t[0][lprior-b1:]
if len(tps)>0:
self.__add(t[0][:lprior-b1],tps, float(t[1]))
if t[0].startswith("^",0) and b1<lprior-1:
b1+=1
else:
b1=-1
elif len(t)!=0:
print "Problem at ", st
self.__somme_a_un()
def limit_on(self, l):
""" Return a SHALLOW copy of self restricted to the priors
that include the words of list or Compte l.
"""
if isinstance(l,list):
c=Compte()
for w in l:
d=c
i=len(w)
while i>0:
i-=1
d=d._Arbre__cat.setdefault(w[i],Compte())
d._Arbre__nbe=1
d=c
else:
d=c.reverse()
return self.__limit_on(d,d)
def __limit_on(self,cc,c):
r=Proportion()
for k,v in self._Arbre__cat.items():
if cc[k]>0 or c[k]>0:
r._Arbre__cat[k]=v
elif cc.has_prefix(k):
r._Arbre__cat[k]=v.__limit_on(cc._Arbre__cat[k],c)
if (not r._Arbre__cat.has_key(k) or (r._Arbre__cat[k].is_empty() and not r._Arbre__cat[k].has_post())):
r._Arbre__cat[k]=v.__limit_on(c,c)
return r
######################################################
###### Calculs
def __getitem__(self,s):
"""Return the string of posterior of prior s (x.__getitem__(s)
<==> x[s])."""
if len(s)==0:
return str(self.__post)
if self._Arbre__cat.has_key(s[-1]):
return self._Arbre__cat[s[-1]][s[:-1]]
else:
return ""
def alph(self,s=[]):
t=self.__post.alph(s)
return _Arbre.alph(self,t)
def lg_max(self):
"Return the length of the longest word (prior+posterior)."
return max(self.__post.lg_max(),_Arbre.lg_max(self))
def lg_max_prior(self):
"Return the length of the longest prior."
if self._Arbre__cat=={}:
return 0
m=0
for c in self._Arbre__cat.values():
n=c.lg_max_prior()
if m<n:
m=n
return m+1
def lg_max_posterior(self):
"Return the length of the longest posterior."
m=self.__post.lg_max()
for d in self._Arbre__cat.values():
n=d.lg_max_posterior()
if n>m:
m=n
return m
def has_post(self):
"Return True if self has a non-empty posterior."
return self.__post._Arbre__cat!={}
###########################################
#### sorties
def __str__(self):
"""(x.__str__() <==> str(x))"""
return self.__ecrit("")
def __ecrit(self,st):
st2=""
if self.__post._Arbre__cat!={}:
st2+=self.__post.ecrit(st+"|")
for i,f in self._Arbre__cat.items():
st3=f.__ecrit(i+st)
st2+=st3
return st2
def prefixes(self,s=""):
"Return the list of the prefixes."
if self._Arbre__cat=={}:
return [s]
if self.__post.is_empty():
l2=[]
else:
l2=[s]
for c,f in self._Arbre__cat.items():
l2+=f.prefixes(c+s)
return l2
def next(self, s):
"Return the list of [word, value] which s in the prefix."
if len(s)==0:
return self.__post.next()
elif self._Arbre__cat.has_key(s[-1]):
return self._Arbre__cat[s[-1]].next(s[:-1])
elif s[-1]==".":
d=[]
for k in self._Arbre__cat:
d+=self._Arbre__cat[k].next(s[:-1])
return d
else:
return []
def rand_next(self, s):
"""Return a string randomly chosen among the ones which s in
the prefix, following the proportions of this.
"""
suiv=self.next(s)
if suiv==[]:
return ""
y=0
for t in suiv:
y+=t[1]
x=random.random()*y
lsuiv=len(suiv)
while x>=0 and lsuiv>0:
lsuiv-=1
x-=suiv[lsuiv][1]
return suiv[lsuiv][0]
def has_prefix(self, s):
"Return True iff s is a STRICT prefix, else False."
if len(s)==0:
return self.__post._Arbre__cat!={} or self._Arbre__cat!={}
elif self._Arbre__cat.has_key(s[-1]):
return self._Arbre__cat[s[-1]].has_prefix(s[:-1])
else:
return 0
#########################################################
##### prive
def __somme_a_un(self):
self.__post.somme_a_un()
for f in self._Arbre__cat.values():
f.__somme_a_un()
def __str_loglex(self):
if self._Arbre__cat!={}:
s="|{"
for i in self._Arbre__cat:
s+=i+self._Arbre__cat[i].__str_loglex()
if self.__post._Arbre__cat!={}:
s+="!`"+self.__post.loglex(1)+"'"
s+="!!(-100000)}"
else:
s="`"+self.__post.loglex(1)+"'"
return s
##################################################################
##################################################################
##################################################################
##### classe Post
class Post(_Arbre):
def __add(self, po, val):
"""Increase recursively the Post from word po and a value val.
Needs po[:]."""
if len(po)==0:
self._Arbre__nbe+=val
elif po[0]=="^":
if len(po)==1:
self._Arbre__cat.setdefault("^",Post())._Arbre__nbe+=val
else:
self.__add(po[1:],val)
else:
self._Arbre__cat.setdefault(po[0],Post()).__add(po[1:],val)
def somme_a_un(self):
c=self._Arbre__somme()
if c!=0:
self/=c
#############################################################
##### sorties
def loglex(self,i=0):
s=""
if self._Arbre__cat!={}:
if i==1:
s+="|("
else:
s+="|`"
for j,c in self._Arbre__cat.items():
s+=j+c.loglex(0)
s+="!"
if self._Arbre__nbe>0:
s+="!("+`math.log(self._Arbre__nbe)`+")"
else:
s+="!(-100000)"
if self._Arbre__cat!={}:
if i==1:
s+=")"
else:
s+="'"
return s
def __str__(self):
return self.ecrit("")
def ecrit(self,st):
if self._Arbre__nbe!=0:
st2= st+"\t"+`self._Arbre__nbe`+"\n"
else:
st2=""
for i,c in self._Arbre__cat.items():
st3=c.ecrit(st+i)
st2+=st3
return st2
# def __getitem__(self, s):
# """Returns the proportion of s.
# (x.__getitem__(s) <==> x[s])"""
# if s=="":
# return self._Arbre__nbe
# else:
# if self._Arbre__cat.has_key(s[0]):
# return self._Arbre__cat[s[0]][s[1:]]
# else:
# return self._Arbre__nbe
def next(self,s=""):
if self._Arbre__nbe!=0:
l=[[s,self._Arbre__nbe]]
else:
l=[]
for i,f in self._Arbre__cat.items():
l+=f.next(s+i)
return l