File: collat.py

package info (click to toggle)
collatinus 7.14-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 11,580 kB
  • ctags: 1,114
  • sloc: xml: 233,732; python: 7,114; pascal: 2,579; ml: 1,400; makefile: 98
file content (1122 lines) | stat: -rwxr-xr-x 37,181 bytes parent folder | download
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
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
#!/usr/bin/python
# -*- coding: Latin-1 -*-
entete = """
###############################################################################
#
#    This file is part of COLLATINVS.
#
#    COLLATINVS 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.
#
#    COLLATINVS 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 COLLATINVS; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
###############################################################################
"""

import os, string, re
print "Collatinus pour python\n  chargement du lexique..."

# manipulation de chanes

# =====================================================
# morphologie
def Nombre(i):
   Lnombres = [' ', 'singulier', 'pluriel']
   return Lnombres[i]
    
Lmodeles = ['uita', 'amicus', 'puer', 'ager', 'templum', 
      'miles', 'ciuis', 'corpus', 'mare', 'manus', 'res',  
      'bonus', 'miser', 'pulcher', 'fortis', 'uetus', 'acer',
      'amo', 'moneo', 'lego', 'capio', 'audio', 'sum', 'eo',
      'imitor', 'uereor', 'sequor', 'patior', 'potior',
      'pronoms', 'invaria']
  
def Modele(i):  # ligne 1: 0-4; 2: 5-10; 3: 11-16; 4: 17-23; 5: 24-28; 6: 29,30
   global Lmodeles
   return Lmodeles[i]

def numero_du_modele(m):
   global Lmodeles
   return Lmodeles.index(m)
   
def MorphoK(m):
   if m < 11:
      return "nominatif singulier"
   elif m < 17:
      return "nominatif masculin singulier"
   elif m < 29:
      return "1re singulier prsent indicatif actif"
   else: 
      return ""
      
def DesK(m):
   if m == 0:
      return 'a'
   elif m in [1, 9, 11]: #== 1 or m == 9 or m == 11:
      return 'us'
   elif 1 < m < 4 or 11 < m < 14:
      return 'er'
   elif m == 4:
      return 'um'
   elif m == 10:
      return 'es'
   elif m == 17 or m == 19:
      return 'o'
   elif m == 18 or m == 23:
      return 'eo'
   elif 19 < m < 22:
      return 'io'
   elif m == 22:
      return 'sum'
   elif m == 24 or m == 26:
      return 'or'
   elif m == 25:
      return 'eor'
   else:
      return 'ior'
   
def Cas(i):   
   Lcas = ['', 'nominatif', 'vocatif', 'accusatif', 'gnitif', 'datif', 'ablatif']
   return Lcas[i]

def Genre(i):
   Lgenres = ['', 'masculin', 'fminin', 'neutre']
   return Lgenres[i]
   
def Personne(i):
   Lpersonnes = ['', '1re', '2me', '3me']
   return Lpersonnes[i]
   
def Temps(i):
   Ltemps = ['', 'prsent', 'futur', 'imparfait', 'parfait',
      'futur antrieur', 'plus-que-parfait']
   return Ltemps[i]
   
def Mode(i):
   Lmodes = ['', 'indicatif', 'subjonctif', 'impratif', 'infinitif',
     'participe', 'grondif',  'adjectif verbal']
   return Lmodes[i]
   
def Voix(i):
   Lvoix = ['', 'actif', 'passif']
   return Lvoix[i]
   
# =====================================================   
   
# =====================================================
# classe tdes
class tdes:   
   def __init__(self, gr, c, gn, n, d, p, t, mde, v, mdl, r):
      self.graphie = gr
      self.cas = int(c)
      self.genre = int(gn)
      self.nombre = int(n)
      self.degre = int(d)
      self.personne = int(p)
      self.temps = int(t)
      self.mode = int(mde)
      self.voix = int(v)
      self.modele = int(mdl)
      self.radnum = int(r)
         
   def morpho(self):  
      """retourne la morphologie de l'instance en utilisant les listes ci-dessus. """
      return '%s %s %s %s %s %s %s (%s) ' % (Cas(self.cas), 
         Genre(self.genre), Personne(self.personne), 
         Nombre(self.nombre), Temps(self.temps), Mode(self.mode), 
         Voix(self.voix), Modele(self.modele))   
         
   def affiche(self):
      print self.morpho()
            
# =====================================================

# =====================================================
#~ classe tirregs
class tirreg(tdes):
   def __init__(self, gr, k, c, gn, n, p, t, mde, v, mdl):
      # graphie, canon, cas, genre, nombre, personne, temps, mode, voix, modele
      self.graphie = gr
      self.cas = int(c)
      self.genre = int(gn)
      self.nombre = int(n)
      self.personne = int(p)
      self.temps = int(t)
      self.mode = int(mde)
      self.voix = int(v)
      self.modele = int(mdl)
      self.canon = k

# =====================================================

# =====================================================
class tentree:
   def __init__(self, m, g, t, r2, r3):
      # modle, graphie, texte, radicaux 2 et 3
      self.modele = int(m)
      self.graphie = g
      self.texte = t
      #~ intgration des radicaux...
      self.r2 = r2
      self.r3 = r3
 
   def doc(self):
      """ retourne le lemme suivi de ses donnes 
      morphologiques, et des traductions franaises """
      return '%s %s' % (self.graphie, self.texte)

   def dochtml(self):
      """ comme doc, mais le lemme est en <b> gras</b> 
          prvoit l'intgration dans un environnement <ol></ol> ou <ul></ul>"""
      return '<li><b>%s</b> %s</li>' % (self.graphie, self.texte)

   def docLaTeX(self):
      """ Nunc carmen nostimus 
          prvoit l'intgration dans un environnemen \\begin{itemize} ou {enumerate}"""
      return '\item \\textbf{%s} %s' % (self.graphie, self.texte)

   def affiche(self):
       print self.doc()
       pass
      
   def ecris(self, f):
      """ crit les donnes du lemme dans le fichier f) """
      f.write('<canon>\n')
      #f.write('<id_canon>' + self.id_canon + '</id_canon>\n')
      f.write('   <graphie>' + self.graphie + '</graphie>\n')
      f.write('   <modele>' + str(self.modele) + '</modele>\n')
      try: 
         f.write('   <R2>' + self.r2 + '</R2>\n')
      except:
	 pass
      try:
         f.write('   <R3>' + self.r3 + '</R3>\n')
      except:
         pass
      f.write('   <texte>' + self.texte + '</texte>\n')
      f.write('</canon>\n')
# =====================================================

# =====================================================
class tradical:  
   def __init__(self, g, k, m):
      self.graphie = g
      self.canon = k
      self.modele = m
    
   def kanon(self):
       """ retourne l'entre de lexique correspondant au radical """  
       return lexique[self.canon]
         
   def affiche(self, i):
      print "%s : radical %d de %s" % (self.graphie, i, self.canon)

# =====================================================

# =====================================================
# chargement du lexique

def datede(file):
   """ retourne la date du fichier file, et la chaine 'nil' 
   en cas d'absence du fichier."""
   if os.path.isfile(file):
      return os.stat(file).st_mtime
   else:
      return 'nil'

# lexicorum creatio :
lexique = {}
radicaux = {}
radicaux[2] = {}
radicaux[3] = {}
desinences = {}
irregs = {}

def a_lemmatibus():
   m = 0
   t = g = r2 = r3 = ""
   fdata = open('lemmata')
   linea = fdata.readline()
   print 'lemmata leguntur...'
   while string.find(linea, '---desinentiae---') < 0:
      eclats = re.split('[|]', linea)
      g = eclats[0]
      m = int(eclats[1])
      r2 = eclats[2]
      r3 = eclats[3]
      # tentree(m, g, t, r2, r3):
      # miles|5|milit||itis, m. : soldat
      lexique[g] = tentree(m, g, eclats[4], r2, r3) 
      # peupler les radicaux
      # tradical(g, k, m):
      if r2 > "":
        if not radicaux[2].has_key(r2):
            radicaux[2][r2] = {}
            radicaux[2][r2][0] = tradical(r2, g, m)
        else:
            radicaux[2][r2][len(radicaux[2][r2])] = tradical(r2, g, m)
      if r3 > "":
         if not radicaux[3].has_key(r3):
            radicaux[3][r3] = {}
            radicaux[3][r3][0] = tradical(r3, g, m)
         else:         
            radicaux[3][r3][len(radicaux[3][r3])] = tradical(r3, g, m)
      linea = fdata.readline()
   
   # dsinences
   print 'desinentiae leguntur...'
   linea = fdata.readline() 
   while string.find(linea, '---irregulares---') < 0:
      eclats = re.split('[|]', linea)
      g = eclats[0]
      # graphie,cas,genre,nombre,degr,personne,temps,mode,voix,modle,radical
      des = tdes(g, eclats[1], eclats[2], eclats[3], eclats[4],
         eclats[5], eclats[6], eclats[7], eclats[8], eclats[9], eclats[10])
      if not desinences.has_key(g):
         #~ ajout de la cl et de la dsinence
         desinences[g] = {}
         desinences[g][0] = des
      #~ ajout de la dsinence dans la cl      
      else :
         desinences[g][len(desinences[g])] = des
      linea = fdata.readline() 

   # irrguliers
   print 'irregulares leguntur...'
   linea = fdata.readline()
   while string.find(linea, '---finis---') < 0:
      eclats = re.split('[|]', linea)
      g = eclats[0]
      # graphie, canon, cas, genre, nombre, temps, mode, voix, modele
      # afferte|adfero|0|0|2|afferte|adfero|0|0|22|1|3|1|0
      irreg = tirreg(g, eclats[1], eclats[2], eclats[3], eclats[4],\
         eclats[5], eclats[6], eclats[7], eclats[8], eclats[9]) #, eclats[10])  
      if not irregs.has_key(g):
         #~ ajout de la cl et de la forme
         irregs[g] = {}
         irregs[g][0] = irreg
      else:
         #~ ajout de la forme dans la cl      
         irregs[g][len(irregs[g])] = irreg
      linea = fdata.readline()

   fdata.close()

def a_xml():   
   """ chargement xml. Un drapeau est arm pour
   gnrer le fichier prcompil lemmata"""
   def decrasse(l, balise):
       """ ridicule parseur xml en trois lignes : 
           dbarasse une ligne de <balise> et de </balise>"""
       r = string.replace(l, '<%s>' % (balise), '')
       r = string.replace(r, '</%s>' % (balise), '')
       return string.strip(r)

   #~ 1. entres
   print "ab xml lemmata leguntur"
   fichier = open('canons.xml')
   ligne = ""
   m = 0
   t = g = r2 = r3 = ""

   # passer l'en-tte et les commentaires
   while string.find(ligne, "<collatinus") < 0:
      ligne = fichier.readline()

   # lecture et parsage   
   while string.find(ligne, "</collatinus") < 0:
      ligne = fichier.readline()
      if string.find(ligne, "<modele>") > -1:
         m = decrasse(ligne, "modele")
      elif string.find(ligne, "<graphie>") > -1:
         g = decrasse(ligne, "graphie")
      elif string.find(ligne, "<texte>") > -1:
         t = decrasse(ligne, "texte")
      elif string.find(ligne, "<R2>") > 1:
         r2 = decrasse(ligne, "R2")
      elif string.find(ligne, "<R3>") > 1:
         r3 = decrasse(ligne, "R3")
      elif string.find(ligne, "</canon>") > -1:
         if lexique.has_key(g):
            g = g + ' (2)'
         lexique[g] = tentree(int(m), g, t, r2, r3) 
         if r2 > "":
            if not radicaux[2].has_key(r2):
	       radicaux[2][r2] = {}
               radicaux[2][r2][0] = tradical(r2, g, int(m))
            else:
	       radicaux[2][r2][len(radicaux[2][r2])] = tradical(r2, g, int(m))
	       lexique[g].r2 = r2
	    r2 = ""
	 if r3 > "":
	    if not radicaux[3].has_key(r3):
                 radicaux[3][r3] = {}
                 radicaux[3][r3][0] = tradical(r3, g, int(m))
	    else:         
               radicaux[3][r3][len(radicaux[3][r3])] = tradical(r3, g, int(m))
               lexique[g].r3 = r3
            r3 = ""
   fichier.close()  

   #~ 2. dsinences
   print "ab xml desinentiae leguntur..."
   fichier = open('desinences.xml')
   # passer l'en-tte et les commentaires
   while string.find(ligne, "<collat") < 0:
      ligne = fichier.readline()
   # initialisation de la premire dsinence
   ligne = '';
   cas = genre = nombre = degre = personne = mode = temps\
      = voix = modele = radnum = '0'
   #~ boucle de lecture
   while string.find(ligne, "</collat") < 0:
      ligne = fichier.readline()   
      if string.find(ligne, "<graphie>") > -1:
         g = decrasse(ligne, "graphie")
      elif string.find(ligne, "cas") > -1:
         cas = decrasse(ligne, "cas")
      elif string.find(ligne, "<genre>") > -1:
         genre = decrasse(ligne, "genre")
      elif string.find(ligne, "<nombre>") > -1:
         nombre = decrasse(ligne, "nombre")
      elif string.find(ligne, "<degre>") > -1:
         degre = decrasse(ligne, "degre")
      elif string.find(ligne, "<personne>") > -1:
         personne = decrasse(ligne, "personne")
      elif string.find(ligne, "<temps>") > -1:
         temps = decrasse(ligne, "temps")
      elif string.find(ligne, "<mode>") > -1:
         mode = decrasse(ligne, "mode")
      elif string.find(ligne, "<voix>") > -1:
         voix = decrasse(ligne, "voix")
      elif string.find(ligne, "<modele>") > -1:
         modele = decrasse(ligne, "modele")
      elif string.find(ligne, "<R>") > -1:
         radnum = decrasse(ligne, "R")
      elif string.find(ligne, "</desinence>") > -1:
      #~ def __init__(self, gr, c, gn, n, d, p, t, mde, v, mdl, r):
         des = tdes(g, cas, genre, nombre, degre, personne,\
            temps, mode, voix, modele, radnum)
         if not desinences.has_key(g):
         #~ ajout de la cl et de la dsinence
            desinences[g] = {}
            desinences[g][0] = des
         else :
         #~ ajout de la dsinence dans la cl      
            desinences[g][len(desinences[g])] = des
         #~ rinitialisation graphie et modele ont toujours une valeur   
         cas = genre = nombre = degre = personne = temps = mode = voix = '0'
   fichier.close()   

   #~ irrguliers
   print "ab xml irregulares leguntur..."
   fichier = open("irregs.xml")
   # passer l'en-tte et les commentaires
   while string.find(ligne, "<collat") < 0:
      ligne = fichier.readline()
   ligne = '';
   cas = genre = nombre = degre = personne\
      = mode = temps = voix = modele = '0'

   #~ boucle de lecture
   while string.find(ligne, "</collat") < 0:
      ligne = fichier.readline()   
      if string.find(ligne, "<graphie>") > -1:
         g = decrasse(ligne, "graphie")
      elif string.find(ligne, "cas") > -1:
         cas = decrasse(ligne, "cas")
      elif string.find(ligne, "<genre>") > -1:
         genre = decrasse(ligne, "genre")
      elif string.find(ligne, "<nombre>") > -1:
         nombre = decrasse(ligne, "nombre")
      elif string.find(ligne, "<degre>") > -1:
         degre = decrasse(ligne, "degre")
      elif string.find(ligne, "personne>") > -1:
         personne = decrasse(ligne, "personne")
      elif string.find(ligne, "<temps>") > -1:
         temps = decrasse(ligne, "temps")
      elif string.find(ligne, "<mode>") > -1:
         mode = decrasse(ligne, "mode")
      elif string.find(ligne, "<voix>") > -1:
         voix = decrasse(ligne, "voix")
      elif string.find(ligne, "<modele>") > -1:
         modele = decrasse(ligne, "modele")
      elif string.find(ligne, "<canon>") > -1:
         canon = decrasse(ligne, "canon")
      elif string.find(ligne, "</irreg>") > -1:
       #~ irreg __init__(self, gr, k, c, gn, n, p, t, mde, v, mdl)
         irreg = tirreg(g, canon, cas, genre, nombre, personne,
           temps, mode, voix, modele)
	 if not irregs.has_key(g):
	    #~ ajout de la cl et de la dsinence
	    irregs[g] = {}
	    irregs[g][0] = irreg
	    #~ ajout de la dsinence dans la cl      
	 else:
	    irregs[g][len(irregs[g])] = irreg
	    #~ rinitialisation graphie et modele ont toujours une valeur   
         cas = genre = nombre = degre = personne = temps = mode = voix = '0'
   fichier.close()

   # prcompilation:
   fpre = open('lemmata', 'w')
   # drapeaux : 0 = lemmes ; 1 = desinences ; 2 = irreguliers
   for lemma in lexique.keys():
      linea = lemma + '|' + str(lexique[lemma].modele)
      linea += '|' + lexique[lemma].r2 +'|' + lexique[lemma].r3 
      linea += '|' + lexique[lemma].texte+'\n'
      fpre.write(linea)

   fpre.write('---desinentiae---\n')   
   for desin in desinences.keys():
      for i in desinences[desin].keys():
         linea = desin + '|'
         linea += str(desinences[desin][i].cas) + '|' 
	 linea += str(desinences[desin][i].genre) + '|' 
	 linea += str(desinences[desin][i].nombre) + '|' 
         linea += str(desinences[desin][i].degre) + '|'\
            + str(desinences[desin][i].personne) + '|'\
	    + str(desinences[desin][i].temps) + '|' 
         linea += str(desinences[desin][i]. mode) + '|'\
            + str(desinences[desin][i].voix) + '|'\
	    + str(desinences[desin][i].modele)\
	    + '|' + str(desinences[desin][i].radnum) + '\n'
         fpre.write(linea) 

   fpre.write('---irregulares---\n')   
   # graphie, canon, cas, genre, nombre, personne, temps, mode, voix, modele
   for irr in irregs.keys():
      for i in irregs[irr].keys():
         linea = irr + '|'
         linea += irregs[irr][i].canon + '|' + str(irregs[irr][i].cas)+'|' 
         linea += str(irregs[irr][i].genre) + '|'\
	    + str(irregs[irr][i].nombre)+'|'
         linea += str(irregs[irr][i].personne) +'|'\
	    +str(irregs[irr][i].temps)+'|'
         linea += str(irregs[irr][i]. mode)+'|'+str(irregs[irr][i].voix)+'|'\
           + str(irregs[irr][i].modele) + '\n'
      fpre.write(linea)
   fpre.write('---finis---')
   fpre.close()

# dtection des fichiers de donnes et de leurs dates de modification   
datecanons = datede('canons.xml')
datelemmata = datede('lemmata')
# print "date l %s date xml %s " % (datelemmata, datecanons)
if datelemmata == 'nil':
  # print 'pas de fichier lemmata'
  a_xml()
elif datecanons == 'nil':
  a_lemmatibus()
elif datelemmata > datecanons > 0:
  a_lemmatibus() 
else:
  a_xml() 

print "  ", len(lexique), " entrees ;"
print len(desinences), " desinences ;"
print len(radicaux[2]), "radicaux 2 ;"
print len(radicaux[3]), "radicaux 3 ;"
print len(irregs), "irreguliers."

# =====================================================
# consultation du lexique 
def dico():
   m = "dico"
   while m != "q":
      if m > "":
         if lexique.has_key(m):
            lexique[m].affiche()
         else:
            print "introuvable"
         m = raw_input("entre (q pour quitter) : ")
# ===================================================== 

# =====================================================
# insertion d'un lemme
def l_adde(g, m, r2, r3, t):
   """ l_adde siue_lemmam adde 
       ajoute  la liste des canons le lemme 
       de graphie g, de modle m,
       de radicaux r3 et r3, 
       et de dfinition t."""
   if lexique.has_key(g):
         g = g + ' (2)'
   nm = numero_du_modele(m)	 
   lexique[g] = tentree(nm, g, t, r2, r3) 
   if r2 > "":
      if not radicaux[2].has_key(r2):
            radicaux[2][r2] = {}
            radicaux[2][r2][0] = tradical(r2, g, nm)
      else:
            radicaux[2][r2][len(radicaux[2][r2])] = tradical(r2, g, nm)
   if r3 > "":
      if not radicaux[3].has_key(r3):
            radicaux[3][r3] = {}
            radicaux[3][r3][0] = tradical(r3, g, int(m))
      else:         
            radicaux[3][r3][len(radicaux[3][r3])] = tradical(r3, g, nm)
   l_scribe()
   return g + " a t ajout au lexique"

# ===================================================== 

# =====================================================
# criture du lexique
def l_scribe():
   """ lexicum scribe
       Ecrit le lexique sur disque.
   """
   global entete
   print "Dictionnaire de Collatinus\n"
   print "--------------------------\n"
   f = open("./canons.xml", "w")
   f.write(entete)
   f.write("\n<collatinus>\n")
   cles = lexique.keys()
   cles.sort()
   for cle in cles:
      lexique[cle].ecris(f) 
   #for (graphie, entree) in lexique.items():
   #   entree.ecris(f)
   f.write("</collatinus>")
   f.close()
   print("Le lexique a t crit dans le fichier canons.xml")

# ===================================================== 

# =====================================================
# consultation des dsinences
def dicodes():
   mot = ""
   while mot != "q":
      if mot != "":   
         if desinences.has_key(mot):
            for i in range(len(desinences[mot])):
               desinences[mot][i].affiche()
         else:
            print "introuvable"           
      mot = raw_input("dsinence ? (q pour quitter) ")
# =====================================================

# =====================================================
#~ consultation des radicaux
def dicorad():
   mot = ""
   while mot != "q":
      if mot > "":
         for i in [2, 3]:
            if radicaux[i].has_key(mot):
               for iRad in range(len(radicaux[i][mot])):
                  radicaux[i][mot][iRad].affiche(i)
      mot = raw_input("radical ? (q pour quitter) ")      

# =====================================================
# recherche des dsinences possibles d'un mot
def desforme():
   mot = ""
   while not mot == "q":
      if mot > '':
         for i in range(len(mot)):
            if desinences.has_key(mot[i:]):
               Des = desinences[mot[i:]]
               for iMorph in range(len(Des)):
                   print Des[iMorph].graphie,
                   Des[iMorph].affiche()
      mot = raw_input("forme ? (q pour quitter) ")
# =====================================================

# =====================================================
# analyses morphologiques d'un mot
def analysesDe(mot):
   resultat = {}
   #~ chercher les formes canoniques
   if lexique.has_key(mot):
      resultat[mot] = []
      resultat[mot].append(MorphoK(lexique[mot].modele))
   if lexique.has_key(mot + ' (2)'):
      mot = mot + ' (2)'
      resultat[mot] = []
      resultat[mot].append(MorphoK(lexique[mot].modele))
   #~ chercher les formes irrgulires
   if irregs.has_key(mot):
      for iIrr in range(len(irregs[mot])):
         Irr = irregs[mot][iIrr]
         if not resultat.has_key(Irr.canon):
            resultat[Irr.canon] = []
         resultat[Irr.canon].append(Irr.morpho())   
   # chercher toutes les dsinences
   for i in range(len(mot)):
      grDes = mot[i:]
      grRad = mot[:i]
      if desinences.has_key(grDes):
         lesDes = desinences[grDes]
         for iDes in range(len(lesDes)):
            Des = lesDes[iDes]
            grMorpho = Des.morpho()
            if Des.radnum > 1:
               if radicaux[Des.radnum].has_key(grRad):
                  radix = radicaux[Des.radnum][grRad]
                  for iRad in range(len(radix)):
                     if Des.modele == radix[iRad].modele and lexique.has_key(radix[iRad].canon)\
                        and  radix[iRad].modele == lexique[radix[iRad].canon].modele:
                     #~ si la cl n'existe pas dans resultat
                        if not resultat.has_key(radix[iRad].canon):
                            resultat[radix[iRad].canon] = []
                            resultat[radix[iRad].canon].append(grMorpho)                         
                        else:
                           resultat[radix[iRad].canon].append(grMorpho)
            else: # if Des[iDes].modele > 13:
               K = grRad + DesK(Des.modele)     
               if lexique.has_key(K)\
                  and Des.modele == lexique[K].modele:
                     if not resultat.has_key(K):
                        resultat[K] = []     
                        resultat[K].append(grMorpho)
                     else: 
                        resultat[K].append(grMorpho)
               if lexique.has_key(K + ' (2)')\
                  and Des.modele == lexique[K + ' (2)'].modele:
                     K = K + ' (2)'
                     if not resultat.has_key(K):
                        resultat[K] = []     
                        resultat[K].append(grMorpho)
                     else: 
                        resultat[K].append(grMorpho)
   return resultat

# =====================================================
#~ chane de retour d'une analyse

def ijuv(forme):
   # transformation v-u et i-J
   r = string.replace(forme, 'v', 'u')
   r = string.replace(r, 'j', 'i')
   return r

def chaineAnalyse(forme):
    f = ijuv(forme)
    res = analysesDe(f)
    if res == {}:
        return "?"
    else  :
        retour = ""
        for i in range(len(res)):
            K = res.keys()[i]
            retour = retour + lexique[K].doc() 
            for iK in range(len(res[K])):
                retour = retour + '    '+ res[K][iK] + '\n' 
        return retour

# =====================================================
# dialogue d'analyse morphologique

def dialogueAnalyse():
   forme = ""
   while forme != "q":
      print chaineAnalyse(forme)  
      forme = raw_input("forme ? (q pour quitter) ")
      
# =====================================================
# Analyse totale : texte, morpho, Dictionnaire Jeanneau

# Dfinition des volumes du Jeanneau 
def volume(e):    
   volumes=
   {"abzz":"Dico-a01.html",
    "aczz":"Dico-a02.html",
    "adnzz":"Dico-a03.html",
    "aegzz":"Dico-a04.html",
    "ajzz":"Dico-a05.html",
    "alzz":"Dico-a06.html",
    "amzz":"Dico-a07.html",
    "aozz":"Dico-a08.html",
    "aqzz":"Dico-a09.html",
    "arzz":"Dico-a10.html",
    "aszz":"Dico-a11.html",
    "azz":"Dico-a12.html",
    "bzz":"Dico-b.html",
    "camzz":"Dico-c01.html",
    "carzz":"Dico-c02.html",
    "cayzz":"Dico-c03.html",
    "cezz":"Dico-c04.html",
    "cirzz":"Dico-c05.html",
    "clazz":"Dico-c06.html",
    "cofzz":"Dico-c07.html",
    "colzz":"Dico-c08.html",
    "comzz":"Dico-c09.html",
    "confzz":"Dico-c10.html",
    "conszz":"Dico-c11.html",
    "coxzz":"Dico-c12.html",
    "cuizz":"Dico-c13.html",
    "czz":"Dico-c14.html",
    "deczz":"Dico-d01.html",
    "deqzz":"Dico-d02.html",
    "dexzz":"Dico-d03.html",
    "dipzz":"Dico-d04.html",
    "diszz":"Dico-d05.html",
    "dunzz":"Dico-d06.html",
    "dzz":"Dico-d07.html",
    "efzz":"Dico-e01.html",
    "elzz":"Dico-e02.html",
    "erzz":"Dico-e03.html",
    "exazz":"Dico-e04.html",
    "exlzz":"Dico-e05.html",
    "exszz":"Dico-e06.html",
    "ezz":"Dico-e07.html",
    "fazz":"Dico-f01.html",
    "fezz":"Dico-f02.html",
    "fizz":"Dico-f03.html",
    "fozz":"Dico-f04.html",
    "frzz":"Dico-f05.html",
    "fzz":"Dico-f06.html",
    "gzz":"Dico-g.html",
    "hzz":"Dico-h.html",
    "imbzz":"Dico-i01.html",
    "impzz":"Dico-i02.html",
    "inczz":"Dico-i03.html",
    "infezz":"Dico-i04.html",
    "inmzz":"Dico-i05.html",
    "inspzz":"Dico-i06.html",
    "intepzz":"Dico-i07.html",
    "interzz":"Dico-i08.html",
    "invezz":"Dico-i09.html",
    "izz":"Dico-i10.html",
    "jzz":"Dico-j.html",
    "kzz":"Dico-k.html",
    "latzz":"Dico-l01.html",
    "leozz":"Dico-l02.html",
    "lixzz":"Dico-l03.html",
    "lubzz":"Dico-l04.html",
    "lzz":"Dico-l05.html",
    "malzz":"Dico-m01.html",
    "maxzz":"Dico-m02.html",
    "mezz":"Dico-m03.html",
    "mnzz":"Dico-m04.html",
    "mozz":"Dico-m05.html",
    "mzz":"Dico-m06.html",
    "nazz":"Dico-n01.html",
    "nenzz":"Dico-n02.html",
    "nexzz":"Dico-n03.html",
    "nizz":"Dico-n04.html",
    "nozz":"Dico-n05.html",
    "nzz":"Dico-n06.html",
    "obmzz":"Dico-o01.html",
    "obtzz":"Dico-o02.html",
    "olluszz":"Dico-o03.html",
    "opuszz":"Dico-o04.html",
    "ozz":"Dico-o05.html",
    "parzz":"Dico-p01.html",
    "pelvzz":"Dico-p02.html",
    "perczz":"Dico-p03.html",
    "perlzz":"Dico-p04.html",
    "perszz":"Dico-p05.html",
    "pidzz":"Dico-p06.html",
    "pluzz":"Dico-p07.html",
    "porzz":"Dico-p08.html",
    "praegzz":"Dico-p09.html",
    "praeszz":"Dico-p10.html",
    "prizz":"Dico-p11.html",
    "promzz":"Dico-p12.html",
    "proutzz":"Dico-p13.html",
    "pzz":"Dico-p14.html",
    "quazz":"Dico-q01.html",
    "quezz":"Dico-q02.html",
    "quizz":"Dico-q03.html",
    "qzz":"Dico-q04.html",
    "razz":"Dico-r01.html",
    "rejzz":"Dico-r02.html",
    "reszz":"Dico-r03.html",
    "rhzz":"Dico-r04.html",
    "rzz":"Dico-r05.html",
    "sazz":"Dico-s01.html",
    "sczz":"Dico-s02.html",
    "sezz":"Dico-s03.html",
    "smzz":"Dico-s04.html",
    "sozz":"Dico-s05.html",
    "sqzz":"Dico-s06.html",
    "stzz":"Dico-s07.html",
    "subzz":"Dico-s08.html",
    "sumzz":"Dico-s09.html",
    "szz":"Dico-s10.html",
    "tazz":"Dico-t01.html",
    "tezz":"Dico-t02.html",
    "tolzz":"Dico-t03.html",
    "tramzz":"Dico-t04.html",
    "trizz":"Dico-t05.html",
    "tzz":"Dico-t06.html",
    "umzz":"Dico-u01.html",
    "urzz":"Dico-u02.html",
    "uterzz":"Dico-u03.html",
    "uzz":"Dico-u04.html",
    "vazz":"Dico-v01.html",
    "verzz":"Dico-v02.html",
    "viezz":"Dico-v03.html",
    "vixzz":"Dico-v04.html",
    "vzz":"Dico-v05.html",
    "xzz":"Dico-x.html",
    "zzz":"Dico-z.html"}
   for k in volumes.keys():
      if e < k: return volumes[k]
   return "Error.html"
   
           
def ui2vj(f):
        f = re.sub('^ua', 'va', f)
        f = re.sub('^ue', 've', f)
        f = re.sub('^ui', 'vi', f)
        f = re.sub('^uo', 'vo', f)
        f = re.sub('^uu', 'vu', f)
        f = re.sub('^ia', 'ja', f)
        f = re.sub('^ie', 'je', f)
        f = re.sub('^ii', 'ji', f)
        f = re.sub('^io', 'jo', f)
        f = re.sub('^iu', 'ju', f)        
        f = re.sub('[aeiou]ua', '[aeiou]va', f)
        f = re.sub('[aeiou]ue', '[aeiou]ve', f)
        f = re.sub('[aeiou]ui', '[aeiou]vi', f)
        f = re.sub('[aeiou]uo', '[aeiou]vo', f)
        f = re.sub('[aeiou]uu', '[aeiou]vu', f)
        f = re.sub('[aeiou]ia', '[aeiou]ja', f)
        f = re.sub('[aeiou]ie', '[aeiou]je', f)
        f = re.sub('[aeiou]ii', '[aeiou]ji', f)
        f = re.sub('[aeiou]io', '[aeiou]jo', f)
        f = re.sub('[aeiou]iu', '[aeiou]ju', f)
        return f

def analyseTotale(f):
    """ Renvoie pour la forme f
       1. ses lemmes
       2. leurs analyses morpho
       3. l'entre Jeanneau correspondante. """
    # ajout d'espaces en dbut et fin pour assurer la mise en tableau
    # des premier et dernier mots.
    echec = 0
    retour = ''
    if f != "":
            # analyse morpho complte
            morpho = analysesDe(f)
    if morpho == '?':              
            # essayer en minuscules
            morpho = analysesDe(ijuv(string.lower(f)))
    # essais suffixes
    if f[-3:] == 'que':
            morpho = analysesDe(f[:-3])
    if morpho == {}:
            retour = retour + '<b>' + f + '</b> : ?'
            echec = 1
    if not echec:
        # calcul de chaque lemme
        retour = retour + '<ol>\n'
        for iLemme in range(len(morpho)):
                # item de liste
                retour = retour + '<li>'
                # lemme en gras
                K = morpho.keys()[iLemme]
                retour = retour + 'lemme : ' +  K + '<br>'
                retour = retour + 'morphologies : '
                for iK in range(len(morpho[K])):
                        retour = retour + morpho[K][iK] + ', '
                 # recherche dans le jeanneau
                retour = retour + '<br>\ndictionnaire Jeanneau : <br>\n'
                K = string.lower(ui2vj(K))
                vol = 'jano/' + volume(K)
                fjeanneau = open(vol)
                tjeanneau = fjeanneau.read() + "<a name";
                fjeanneau.close()
                regex = re.compile('<a name="' + K + '">(.*?)<a name',  (re.I | re.S))
                article = regex.findall(tjeanneau)
                for iart in range(len(article)):
                        article[iart] = re.sub('(<LI>|</LI>|<li>|</li>)', '', article[iart])
                        retour = retour + article[iart]
                retour = retour + '</li>'
        retour = retour + '</ol>\n'
    return retour

# =====================================================
# lemmatisation d'un texte, retour type liste

def listelemmes(t):
   """renvoie dans une liste de chaines 
      le lexique utilis par le texte t."""
   retour = []
   # quivalences j->i et v->u
   t = ijuv(t)
   # requete d'analyse morpho :
   # suppression de la ponctuation et du texte entre [], mise en tableau
   t = re.sub('\[.*\]','', t)
   # ajout d'espaces en dbut et fin pour assurer la mise en tableau
   # des premier et dernier mots.
   t = " " + t + " "
   tableau = re.split('[^a-zA-Z]', t)
   # lemmatisation de chaque mot
   for iTab in range(len(tableau)):
       f = tableau[iTab]
       lignes = analysesDe(f)
       if lignes == {}:              
           # essayer en minuscules
           lignes = analysesDe(ijuv(string.lower(f)))
       # essais suffixes
       if f[-3:] == 'que':
           lignes.update(analysesDe(f[:-3]))
       if lignes == {}:
           L = f + ' : ?'
           try:
              retour.index(L)
           except:
              retour.append(L)
       else:
           for iLin in range(len(lignes)):
               L = lexique[lignes.keys()[iLin]].doc()
               try:
                   retour.index(L)
               except:
                   retour.append(L)
   # tri et ajout au rsultat
   retour.sort()
   # liminer le : ? en premire ligne.
   del retour[0]
   for i in range(len(retour)):
         retour.append(retour[i])
   return retour

#=====================================================
# lemmatisation d'un fichier, retour texte.
def lemmatise_fichier(f):
   fichier = open(f)
   t = fichier.read()
   liste = listelemmes(t)
   for l in range(len(liste)):
      print liste[l]

def lemmatisem(m, format):
      """ lemmatisation d'un seul mot """
      retour = ""
      dico = [] 
      lignes = analysesDe(m)
      if lignes == {}:              
          # essayer en minuscules
          lignes = analysesDe(ijuv(string.lower(m)))
      # essais suffixes
      if m[-3:] == 'que':
         lignes.update(analysesDe(m[:-3]))
      if lignes == {}:
	 if format == 'texte':
	        L = m + ' : ?'
	 elif format == 'html':
                L = '<li><b>' + m + '</b> : ?</li>'
	 elif format == 'LaTeX':
	        L = '\item \\textbf{' + m + '} : --'
         try:
                dico.index(L)
         except:
                dico.append(L)
      else:
         for iLin in range(len(lignes)):
	        if format == 'texte':
                   L = lexique[lignes.keys()[iLin]].doc()
		elif format == 'html':
                   L = lexique[lignes.keys()[iLin]].dochtml()
		else: 
                   L = lexique[lignes.keys()[iLin]].docLaTeX()
                try:
                   dico.index(L)
                except:
                   dico.append(L)
      # tri et ajout au rsultat
      dico.sort()
      # liminer le : ? en premire ligne.
      for i in range(len(dico)):
         retour = retour + dico[i] + '\n'
      return retour


# =====================================================
# lemmatisation d'un texte, retour en html.
			    
def lemmatise(t, format):
   """renvoie dans un tableau html de deux colonnes
      le lexique utilis par le texte t."""
   # requte d'ajout
   if t[0:2] == 'a:':
         # sparation morpho/texte
	 tableau = re.split ('\|', t[2:])
         # mise en tableau des paramtres
	 morpho =  re.split ('[^a-zA-Z]', tableau[0]) 
	 texte = tableau[1] 
	 # morpho[0] : graphie
	 # morpho[1] : modle
	 # morpho[2] : radical 2
	 # morpho[3] : radical 3
	 print "appel de ",morpho[0], " ", morpho[1], " ", morpho[2], " ", texte
	 return l_adde (morpho[0], morpho[1], morpho[2], morpho[2], texte)
	 
   # quivalences j->i et v->u
   t = ijuv(t)
   # requete d'analyse morpho :
   if t[0:2] == 'm:': 
          t = t[2:]
          return analyseTotale(t)
   # suppression de la ponctuation et du texte entre [], mise en tableau
   t = re.sub('\[.*\]','', t)
   # ajout d'espaces en dbut et fin pour assurer la mise en tableau
   # des premier et dernier mots.
   t = " " + t + " "
   tableau = re.split('[\W]', t)
   # pourquoi des items vides  chaque bout ?
   retour = ''
   dico = []
   # lemmatisation de chaque mot
   for iTab in range(len(tableau)):
          f = tableau[iTab]
          lignes = analysesDe(f)
          if lignes == {}:              
             # essayer en minuscules
             lignes = analysesDe(ijuv(string.lower(f)))
          # essais suffixes
          if f[-3:] == 'que':
             lignes.update(analysesDe(f[:-3]))
          if lignes == {}:
	     if format == 'texte':
	        L = f + ' : ?'
	     elif format == 'html':
                L = '<li><b>' + f + '</b> : ?</li>'
	     elif format == 'LaTeX':
	        L = '\item \\textbf{' + f + '} : --'
             try:
                dico.index(L)
             except:
                dico.append(L)
          else:
             for iLin in range(len(lignes)):
	        if format == 'texte':
                   L = lexique[lignes.keys()[iLin]].doc()
		elif format == 'html':
                   L = lexique[lignes.keys()[iLin]].dochtml()
		else: 
                   L = lexique[lignes.keys()[iLin]].docLaTeX()
                try:
                   dico.index(L)
                except:
                   dico.append(L)
   # tri et ajout au rsultat
   dico.sort()
   # liminer le : ? en premire ligne.
   del dico[0]
   for i in range(len(dico)):
         retour = retour + dico[i]
   return retour

#~ test du module 
if __name__ == '__main__':
    dialogueAnalyse()