-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
2149 lines (1540 loc) · 63 KB
/
functions.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
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
""" functions.py contains functions and classes to phonetize and expand text :
- expandTxt(text) : to expand un texte
- phonTxt(texte, locutor="speaker", exceptions=True, link=False) : to phonetize a text. You can specify a speaker who has specific pronunciations. You can choose to use or not the exception database, and to make the liaisons between word.
---------------------------------------
Detail of classes and functions called by phonTxt() and expandTxt():
- motmin(string) : pass a string to uppercase considering diacritical signs
- romanToArabic(string) : convert roman numbers to arabic numbers
- assemble(list) : assemble a list of phonetized words doing liaisons and adapting finals letters
Class Texte(content):
- tractMails() : makes mails and url explicits for oral
- tractDates() : makes dates explicits for oral
- tractHours() : makes hours explicits for oral
- tractMesures() : makes measurments and symbols explicits for oral
- tractRomanNumbers() : converts roman numbers to arabic numbers and tract symbols and measurment which follow
- tractNumbers() : pass numbers to letters
- tractSigles() : spel acronyms
- minuscules() : pass a string to uppercase considering diacritical signs
- pretrait() : corrects some specific words to anticipate their pronunciation
- charSpe() : deals with special characters
- tractAbbrev() : treats abbreviations
- supDblEsp() : deletes double whitespaces
- listeMots() : tokenizes
- tractSelf() : applies all treatments and returns the word list
Class Mot(content, pos="grammatical category", locuteur="speaker"):
- sFinal(following_letter) : deals with final -s pronunciation according to the first letter of the following word
- minuscules() : pass to uppercase considering diacritical signs
- phonetize(lettre_suivante) : transcribes the word in API
- syllabize() : splits a phonetized word in syllables
- accent() : puts the word stress in a syllabized word
- correct() : corrects pronunciation according to syllabization and word stress
- tract(following_letter, exceptions=True, link=False) : applies all the treatments considering the first letter of the following word
"""
import re
import pickle
#punctuations patterns
ponctuations='\.\?\!;;:,…\[\]\(\)\{\}"«»/\\^_`\*”“„‘´\$\#\%‰\&\+<>=\`\|\~\*\룃¥€¿¡¢¥─—¯¤¯´±°¹²³™¶'
ponctuationsweb='\.\?\!;;:,…\[\]\(\)\{\}"«»\\^_`\*”“„‘´\$\#\%‰\&\+<>=\`\|\~\*\룃¥€¿¡¢¥─—¯¤¯´±°¹²³™¶'
#numbers ----> to complete
chiffresvar={
'0':'zèro',
'1':'un',
'2':'dos',
'3':'tres',
'4':'quatre',
'5':'cinc',
'6':'seis',
'7':'sete',
'8':'ueit',
'9':'nòu',
'10':'detz',
'11':'ònze',
'12':'dotze',
'13':'tretze',
'14':'catòrze',
'15':'quinze',
'16':'setze',
'17':'detz-e-sete',
'18':'detz-e-ueit',
'19':'detz-e-nòu',
'20':'vint',
'30':'trenta',
'40':'quaranta',
'50':'cinquanta',
'60':'seissanta',
'70':'setanta',
'80':'ueitanta',
'90':'novanta',
'100sing':'cent',
'100plur':'cents',
'1000':'mila',
'millionsing':'milion',
'millionplur':'milions',
'milliardsing':'miliard',
'milliardplur':'miliards',
'billionsing':'bilion',
'billionplur':'bilions',
'premier':'primèir',
'second':'segond',
'moins':'mens',
'fois':'còps',
'divise':'devesit per',
};
#comma name ----> to complete
virgule='virgula'
def motmin(texte):
texte=texte.replace('À', 'à')
texte=texte.replace('Á', 'á')
texte=texte.replace('Â', 'â')
texte=texte.replace('Ä', 'ä')
texte=texte.replace('È', 'è')
texte=texte.replace('É', 'é')
texte=texte.replace('Ë', 'ë')
texte=texte.replace('Ê', 'ê')
texte=texte.replace('Ì', 'ì')
texte=texte.replace('Í', 'í')
texte=texte.replace('Ï', 'ï')
texte=texte.replace('Î', 'î')
texte=texte.replace('Ò', 'ò')
texte=texte.replace('Ó', 'ó')
texte=texte.replace('Ô', 'ô')
texte=texte.replace('Ö', 'ö')
texte=texte.replace('Ù', 'ù')
texte=texte.replace('Ú', 'ú')
texte=texte.replace('Û', 'û')
texte=texte.replace('Ü', 'ü')
texte=texte.replace('Ç', 'ç')
texte=texte.lower()
return texte
def romanToArabic(chaine):
romans = {
'M' : 1000,
'CM' : 900,
'D' : 500,
'CD' : 400,
'C' : 100,
'XC' : 90,
'L' : 50,
'XL' : 40,
'X' : 10,
'IX' : 9,
'V' : 5,
'IV' : 4,
'I' : 1,
}
result = 0
for key, value in romans.items():
while chaine.find(key)==0:
result += value;
chaine = chaine[len(key):len(chaine)];
return result
class Texte:
def __init__(self, content):
content=content.strip(" ")
content=" "+content+" "
content=re.sub('([\s]{1,})', ' ', content)
self.content=content
self.prononciation=''
#Processing
#Tags deletion
def tractTags(self):
texte=self.content
texte=texte.replace('\\', '±')
while re.search('="([^"]*?)±"', texte):
texte=re.sub('="([^"]*?)±"', '="\g<1>', texte)
while re.search("='([^']*?)±'", texte):
texte=re.sub("='([^']*?)±'", "='\g<1>’", texte)
texte=re.sub('<([^ >]*?)>', '', texte)
#texte=re.sub('<( ([^> ]+)=("([^"]*?)"|\'([^\']*?)\'))+>', '', texte)
texte=re.sub('<([^ >]+)( ([^> ]+)=("([^"]*?)"|\'([^\']*?)\'))+>', '', texte)
texte=texte.replace('±', '\\')
self.content = texte
#Mails and URL processing
def tractMails(self):
texte=self.content
#URL signs ----> to complete
signesvar={
'point':'punt',
'arobase':'aròba',
'tiret':'tiret',
'souligné':'solinhat',
'slash':'slach',
'http:':'acha te te pe dos punts',
'https:':'acha te te pe èssa dos punts',
'www':'tres ve dobles'
}
# Change punctuations to keep
texte=texte.replace('-', 'tttttt6ttttt')
texte=texte.replace('_', 'tttttt8ttttt')
# Email pattern
motif='(([\W])([^'+ponctuationsweb+'\s]*?)([\.]?)([^'+ponctuationsweb+'\s]*?)@([^'+ponctuationsweb+'\s]*?)\.([^'+ponctuationsweb+'\s]*?)([\W]))'
# For each
for resulta in re.findall(motif, texte):
resulta=resulta[0]
resulta=resulta[1:-1]
resorig=resulta
# Expand punctuations
resulta=resulta.replace('.',' '+signesvar['point']+' ')
resulta=resulta.replace('@',' '+signesvar['arobase']+' ')
resulta=resulta.replace('tttttt6ttttt',' '+signesvar['tiret']+' ')
resulta=resulta.replace('tttttt8ttttt',' '+signesvar['souligné']+' ')
# Replace original text
texte=texte.replace(resorig,resulta)
# ------- Process websites url ------- */
# Change punctuations to keep
texte=texte.replace('/', 'ttttttsttttt')
texte=re.sub('\.([^ ])','ttttttpttttt\g<1>',texte)
# Website pattern beginning with http:// */
motif='(([\W])https?:ttttttstttttttttttsttttt([\w]*?)([\W]))'
for resulta in re.findall(motif, texte):
resulta=resulta[0]
resulta=resulta[1:-1]
resorig=resulta
# Keep final point
resulta=re.sub('ttttttpttttt$','.',resulta)
# Expand punctuations
resulta=resulta.replace('ttttttpttttt',' '+signesvar['point']+' ')
resulta=resulta.replace('ttttttsttttt',' '+signesvar['slash']+' ')
resulta=resulta.replace('http:',' '+signesvar['http:']+' ')
resulta=resulta.replace('https:',' '+signesvar['https:']+' ')
resulta=resulta.replace('tttttt6ttttt',' '+signesvar['tiret']+' ')
resulta=resulta.replace('tttttt8ttttt',' '+signesvar['souligné']+' ')
# Replace original text
texte=texte.replace(resorig,resulta)
# Website pattern beginning with www. */
motif='(([\W])wwwttttttpttttt([\w]*?)([\.]?)([\w]*?)([\W]))';
for resulta in re.findall(motif, texte):
resulta=resulta[0]
resulta=resulta[1:-1]
resorig=resulta
# Keep final point
resulta=re.sub('ttttttpttttt$','.',resulta)
# Expand punctuations
resulta=resulta.replace('ttttttpttttt',' '+signesvar['point']+' ')
resulta=resulta.replace('ttttttsttttt',' '+signesvar['slash']+' ')
resulta=resulta.replace('www',' '+signesvar['www']+' ')
resulta=resulta.replace('tttttt6ttttt',' '+signesvar['tiret']+' ')
resulta=resulta.replace('tttttt8ttttt',' '+signesvar['souligné']+' ')
# Replace original text
texte=texte.replace(resorig,resulta)
# Other website pattern */
motif='(([\W])([\w]*?)ttttttpttttt(org|com|fr|net|eu|it|bzh|cat|tv)ttttttsttttt([\w]*?)([\W]))'
for resulta in re.findall(motif, texte):
resulta=resulta[0]
resulta=resulta[1:-1]
resorig=resulta
# Keep final point
resulta=re.sub('ttttttpttttt$','.',resulta)
# Expand punctuations
resulta=resulta.replace('ttttttpttttt',' '+signesvar['point']+' ')
resulta=resulta.replace('ttttttsttttt',' '+signesvar['slash']+' ')
resulta=resulta.replace('www',' '+signesvar['www']+' ')
resulta=resulta.replace('tttttt6ttttt',' '+signesvar['tiret']+' ')
resulta=resulta.replace('tttttt8ttttt',' '+signesvar['souligné']+' ')
# Replace original text
texte=texte.replace(resorig,resulta)
# Other website pattern */
motif='(([\W])([\w]*?)ttttttpttttt(org|com|fr|net|eu|it|bzh|cat|tv)([\W]))'
for resulta in re.findall(motif, texte):
resulta=resulta[0]
resulta=resulta[1:-1]
resorig=resulta
# Keep final point
resulta=re.sub('ttttttpttttt$','.',resulta)
# Passer les ponctuations en lettre */
resulta=resulta.replace('ttttttpttttt',' '+signesvar['point']+' ')
resulta=resulta.replace('ttttttsttttt',' '+signesvar['slash']+' ')
resulta=resulta.replace('www',' '+signesvar['www']+' ')
resulta=resulta.replace('tttttt6ttttt',' '+signesvar['tiret']+' ')
resulta=resulta.replace('tttttt8ttttt',' '+signesvar['souligné']+' ')
# Replace original text
texte=texte.replace(resorig,resulta)
# Reset punctuations
texte=texte.replace('ttttttsttttt', '/')
texte=texte.replace('ttttttpttttt', '.')
texte=texte.replace('tttttt6ttttt', '-')
texte=texte.replace('tttttt8ttttt', '_')
self.content = texte
#Dates processing
def tractDates(self):
texte=self.content
#Months ----> to complete
moisvar={
'01':'genier',
'02':'febrier',
'03':'març',
'04':'abriu',
'05':'mai',
'06':'junh',
'07':'julhet',
'08':'avost',
'09':'setembre',
'10':'octòbre',
'11':'novembre',
'12':'decembre',
}
#Searching date patterns
#JJ/MM/AAAA and J/M/AAAA
texte=re.sub('(^|[\W])([0-9]{1,2})(\/|\-|\.)([0-9]{1,2})(\/|\-|\.)([0-9]{4})', '\g<1>¬ßj\g<2>jßßm\g<4>mßßa\g<6>a߬', texte)
#JJ/MM/AA and J/M/AA
texte=re.sub('(^|[\W])([0-2])?([0-9])(\/|\-|\.)([0])?([1-9])(\/|\-|\.)([0-9]{2})', '\g<1>¬ßj\g<2>\g<3>jßßm\g<5>\g<6>mßßa\g<8>a߬', texte)
texte=re.sub('(^|[\W])([0-2])?([0-9])(\/|\-|\.)([1])?([0-2])(\/|\-|\.)([0-9]{2})', '\g<1>¬ßj\g<2>\g<3>jßßm\g<5>\g<6>mßßa\g<8>a߬', texte)
texte=re.sub('(^|[\W])([3])?([0-1])(\/|\-|\.)([0])?([1-9])(\/|\-|\.)([0-9]{2})', '\g<1>¬ßj\g<2>\g<3>jßßm\g<5>\g<6>mßßa\g<8>a߬', texte)
texte=re.sub('(^|[\W])([3])?([0-1])(\/|\-|\.)([1])?([0-2])(\/|\-|\.)([0-9]{2})', '\g<1>¬ßj\g<2>\g<3>jßßm\g<5>\g<6>mßßa\g<8>a߬', texte)
#AAAA/JJ/MM and AAAA/J/M
texte=re.sub('(^|[\W])([0-9]{4})(\/)([0-9]{1,2})(\/|\-|\.)([0-9]{1,2})', '\g<1>¬ßj\g<6>jßßm\g<4>mßßa\g<2>a߬', texte)
#AA/JJ/MM and AA/J/M
texte=re.sub('(^|[\W])([0-9]{2})(\/|\-|\.)([0])?([1-9])(\/|\-|\.)([0-2])?([0-9])', '\g<1>¬ßj\g<7>\g<8>jßßm\g<4>\g<5>mßßa\g<2>a߬', texte)
texte=re.sub('(^|[\W])([0-9]{2})(\/|\-|\.)([1])?([0-2])(\/|\-|\.)([0-2])?([0-9])', '\g<1>¬ßj\g<7>\g<8>jßßm\g<4>\g<5>mßßa\g<2>a߬', texte)
texte=re.sub('(^|[\W])([0-9]{2})(\/|\-|\.)([0])?([1-9])(\/|\-|\.)([3])?([0-1])', '\g<1>¬ßj\g<7>\g<8>jßßm\g<4>\g<5>mßßa\g<2>a߬', texte)
texte=re.sub('(^|[\W])([0-9]{2})(\/|\-|\.)([1])?([0-2])(\/|\-|\.)([3])?([0-1])', '\g<1>¬ßj\g<7>\g<8>jßßm\g<4>\g<5>mßßa\g<6>a߬', texte)
#JJ/MM
texte=re.sub('(^|[\W])([0-2])?([0-9])(\/)([0])?([1-9])([\W]|$)', '\g<1>¬ßj\g<2>\g<3>jßßm\g<5>\g<6>m߬\g<7>', texte)
texte=re.sub('(^|[\W])([0-2])?([0-9])(\/)([1])?([0-2])([\W]|$)', '\g<1>¬ßj\g<2>\g<3>jßßm\g<5>\g<6>m߬\g<7>', texte)
texte=re.sub('(^|[\W])([3])?([0-1])(\/)([0])?([1-9])([\W]|$)', '\g<1>¬ßj\g<2>\g<3>jßßm\g<5>\g<6>m߬\g<7>', texte)
texte=re.sub('(^|[\W])([3])?([0-1])(\/)([1])?([0-2])([\W]|$)', '\g<1>¬ßj\g<2>\g<3>jßßm\g<5>\g<6>m߬\g<7>', texte)
#MM/AAAA
texte=re.sub('(^|[\W])([0])([1-9])(\/|\-)([0-9]{4})', '\g<1>¬ßm\g<2>\g<3>mßßa\g<5>a߬', texte)
texte=re.sub('(^|[\W])([1])([0-2])(\/|\-)([0-9]{4})', '\g<1>¬ßm\g<2>\g<3>mßßa\g<5>a߬', texte)
#MM/AA
texte=re.sub('(^|[\W])([0])([1-9])(\/)([0-9])([0-9])([\W]|$)', '\g<1>¬ßm\g<2>\g<3>mßßa\g<5>\g<6>a߬\g<7>', texte)
texte=re.sub('(^|[\W])([1])([0-2])(\/)([0-9])([0-9])([\W]|$)', '\g<1>¬ßm\g<2>\g<3>mßßa\g<5>\g<6>a߬\g<7>', texte)
motif1='(¬(ß([^¬]*?)ß)¬)'
for resulta1 in re.findall(motif1, texte):
resulta1=resulta1[1]
matches=re.search('ßj([^¬]*?)jß', resulta1)
if matches:
jour=matches.group(1)
else:
jour=""
matches=re.search('ßm([^¬]*?)mß', resulta1)
if matches:
mois=matches.group(1)
else:
mois=""
matches=re.search('ßa([^¬]*?)aß', resulta1)
if matches:
an=matches.group(1)
else:
an=""
jour=re.sub('^0', '', jour)
#Changing "one" in "first" ----> to complete
if jour=="1":
jour="1èr"
if re.search('^([1-9])$', mois):
mois="0"+mois
if mois in moisvar:
mois=moisvar[mois]
if jour!="":
if re.search('^([aeiouàâäèéëêíìïîòóôöùúüû])', mois):
mois="d'"+mois
else:
mois="de "+mois
#Adding "de" before year ----> to complete (delete if it doesn't apply to your language)
if mois!="" and an!="":
an="de "+an
nvdate=jour
if nvdate!="" and mois!="":
nvdate=nvdate+" "+mois
elif mois!="":
nvdate=mois
if nvdate!="" and an!="":
nvdate=nvdate+" "+an
texte=re.sub('¬'+resulta1+'¬',nvdate, texte)
texte=re.sub('¬', '', texte)
self.content = texte
#Traitement des heures
def tractHours(self):
texte=self.content
#Durations names ----> to complete
heuresvar={
'heure_sing':'ora',
'heure_plur':'oras',
'minute_sing':'minuta',
'minute_plur':'minutas',
'seconde_sing':'segonda',
'seconde_plur':'segondas',
'centieme_sing':'centen',
'centieme_plur':'centens',
}
#Hours symbols ----> to complete
heuressymbol=['h', 'o', 'ò']
motifs={
#01h01 01 h 01 01o01 01ò01 1h1...
'(^|[\W])(([0-1])?([0-9])|([2])?([0-4])) ?('+'|'.join(heuressymbol)+') ?(([0-5])?([0-9]))([\W]|$)':'\g<1>¬ßh\g<2>hßßm\g<8>m߬\g<11>',
#01h 01 h 01o 01ò...
'(^|[\W])(([0-1])?([0-9])|([2])?([0-4])) ?('+'|'.join(heuressymbol)+')([\W]|$)':'\g<1>¬ßh\g<2>h߬\g<8>',
#01:01 1:01
'(^|[\W])(([0-1])?([0-9])|([2])?([0-4])):(([0-5])?([0-9]))([\W]|$)':'\g<1>¬ßh\g<2>hßßm\g<7>m߬\g<10>',
#01'01"01 1'1"01
'(^|[\W])(([0-5])?([0-9]))\'(([0-5])?([0-9]))(\'\'|")([0-9]{1,2})([\W]|$)':'\g<1>¬ßm\g<2>mßßs\g<5>sßßc\g<9>c߬\g<10>',
#01'01 1'01
'(^|[\W])(([0-5])?([0-9]))\'(([0-5])?([0-9]))([\W]|$)':'\g<1>¬ßm\g<2>mßßs\g<5>s߬\g<8>',
#01"01 1"01
'(^|[\W])(([0-5])?([0-9]))(\'\'|")([0-9]{1,2})([\W]|$)':'\g<1>¬ßs\g<2>sßßc6c߬\g<7>',
}
#On recherche les différents formats de date
for motif, rempl in motifs.items():
while re.search(motif, texte):
texte=re.sub(motif, rempl, texte)
motif1='(¬(ß([^¬]*?)ß)¬)';
for resulta1 in re.findall(motif1, texte):
resulta1=resulta1[1]
matches=re.search('ßh([^¬]*?)hß', resulta1)
if matches:
heure=matches.group(1)
else:
heure=""
matches=re.search('ßm([^¬]*?)mß', resulta1)
if matches:
minu=matches.group(1)
else:
minu=""
matches=re.search('ßs([^¬]*?)sß', resulta1)
if matches:
sec=matches.group(1)
else:
sec=""
matches=re.search('ßc([^¬]*?)cß', resulta1)
if matches:
cent=matches.group(1)
else:
cent=""
nvdate="";
if heure!="":
if heure=="00":
heure="0"
nvdate=nvdate+heure
try:
heure=int(heure)
if heure<2:
nvdate=nvdate+" "+heuresvar['heure_sing']
else:
nvdate=nvdate+" "+heuresvar['heure_plur']
except:
nvdate=nvdate+" "+heuresvar['heure_plur']
nvdate=nvdate+' '
if minu!="" and minu!="0" and minu!="00":
minu=re.sub('^0', '', minu)
nvdate=nvdate+minu
try:
minu=int(minu)
if minu<2:
nvdate=nvdate+" "+heuresvar['minute_sing']
else:
nvdate=nvdate+" "+heuresvar['minute_plur']
except:
nvdate=nvdate+" "+heuresvar['minute_plur']
nvdate=nvdate+' '
if sec!="" and sec!="0" and sec!="00":
sec=re.sub('^0', '', sec)
nvdate=nvdate+sec
try:
sec=int(sec)
if sec<2:
nvdate=nvdate+" "+heuresvar['seconde_sing']
else:
nvdate=nvdate+" "+heuresvar['seconde_plur']
except:
nvdate=nvdate+" "+heuresvar['seconde_plur']
nvdate=nvdate+' '
if cent!="" and cent!="0" and cent!="00":
cent=re.sub('^0', '', cent)
nvdate=nvdate+cent
try:
cent=int(cent)
if cent<2:
nvdate=nvdate+" "+heuresvar['centieme_sing']
else:
nvdate=nvdate+" "+heuresvar['centieme_plur']
except:
nvdate=nvdate+" "+heuresvar['centieme_plur']
nvdate=nvdate+' '
nvdate=re.sub('([ ]{2,})', ' ', nvdate)
nvdate=nvdate.strip(' ')
texte=re.sub('¬'+resulta1+'¬',nvdate, texte)
self.content = texte
#measurments processing
def tractMesures(self):
texte=self.content
#measurments names ----> to complete (singular and plural)
mesuresvar={
'nm':('nanomètre', 'nanomètres'),
'µm':('micromètre', 'micromètres'),
'mm':('millimètre', 'millimètres'),
'cm':('centimètre', 'centimètres'),
'dm':('décimètre', 'décimètres'),
'hm ':('ectomètre', 'ectomètres'),
'km':('quilomètre', 'quilomètres'),
'µs':('miliseconde', 'milisecondes'),
'mn':('minuta', 'minutas'),
'min':('minuta', 'minutas'),
'nL':('nanolitre', 'nanolitres'),
'µl':('microlitre', 'microlitres'),
'µL':('microlitre', 'microlitres'),
'ml':('millilitre', 'millilitres'),
'cl':('centilitre', 'centilitres'),
'cL':('centilitre', 'centilitres'),
'dl':('décilitre', 'décilitres'),
'dL':('décilitre', 'décilitres'),
'daL':('décalitre', 'décalitres'),
'hl':('ectolitre', 'ectolitres'),
'hL':('ectolitre', 'ectolitres'),
'kl':('quilolitre', 'quilolitres'),
'kL':('quilolitre', 'quilolitres'),
'ng':('nanograma', 'nanogramas'),
'µg':('micrograma', 'microgramas'),
'mg':('milligrama', 'milligramas'),
'cg':('centigrama', 'centigramas'),
'dg':('decigrama', 'decigramas'),
'dag':('decagrama', 'decagramas'),
'hg':('ectograma', 'ectogramas'),
'kg':('quilograma', 'quilogramas'),
'Mt':('megatona', 'megatonas'),
'mA':('milliampère', 'milliampères'),
'Amp':('ampère', 'ampères'),
'amp':('ampère', 'ampères'),
'mHz':('millihertz', 'millihertz'),
'Hz':('Hertz', 'Hertz'),
'kHz':('quilohertz', 'quilohertz'),
'MHz':('megahertz', 'megahertz'),
'hPa':('ectopascal', 'ectopascals'),
'MPa':('megapascal', 'megapascals'),
'GPa':('gigapascal', 'gigapascals'),
'TPa':('terapascal', 'terapascals'),
'GV':('gigavòlt', 'gigavòlts'),
'MV':('megavòlt', 'megavòlts'),
'kV':('quilovòlt', 'quilovòlts'),
'hV':('ectovòlt', 'ectovòlts'),
'daV':('decavòlt', 'decavòlts'),
'dV':('decivòlt', 'decivòlts'),
'cV':('centivòlt', 'centivòlts'),
'mV':('millivòlt', 'millivòlts'),
'μV':('microvòlt', 'microvòlts'),
'nV':('nanovòlt', 'nanovòlts'),
'Wb':('Weber', 'Webers'),
'lm':('lumen', 'lumens'),
'sr':('Radian', 'Radians'),
'lx':('Lux', 'Luxs'),
'Bq':('Becquerel', 'Becquerels'),
'Gy':('Gray', 'Grays'),
'kat':('Katal', 'Katals'),
'Ko':('quilòoctet', 'quilòoctets'),
'Kb':('quilòoctet', 'quilòoctets'),
'Mb':('megaoctet', 'megaoctets'),
'Gb':('gigaoctet', 'gigaoctets'),
}
#measurments unities names ----> to complete (singular and plural)
unitesvar={
'm':('mètre', 'mètres'),
's':('segonda', 'segondas'),
'A':('ampère', 'ampères'),
'm2':('mètre cairat', 'mètres cairats'),
'm3':('mètre cubic', 'mètres cubics'),
'm²':('mètre cairat', 'mètres cairats'),
'm³':('mètre cubic', 'mètres cubics'),
'm\/s':('mètre per segonda', 'mètres per segonda'),
'm\/s2':('mètre per segonda cairada', 'mètres per segonda cairada'),
'km\/h':('quilomètre per ora', 'quilomètre per ora'),
'Hz':('hèrtz', 'hèrtzes'),
'Pa':('pascal', 'pascals'),
'V':('vòlt', 'vòlts'),
'carre':'au cairat',
'cube':'au cube',
'puiss':'a la poténcia',
}
#singular determiners ----> to complete
singDeterminers=['lo', 'la', 'un', 'una', 'zero', 'zéro', 'zèro', 'zerò']
texte=re.sub('([a-záàâäèéëêíìïîòóôöùúüûçA-ZÁÀÂÄÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÇ])²([^¹²³⁴⁵⁶⁷⁸⁹])', '\g<1> '+unitesvar['carre']+'\g<2>', texte)
texte=re.sub('([a-záàâäèéëêíìïîòóôöùúüûçA-ZÁÀÂÄÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÇ])³([^¹²³⁴⁵⁶⁷⁸⁹])', '\g<1> '+unitesvar['cube']+'\g<2>', texte)
texte=re.sub('([a-záàâäèéëêíìïîòóôöùúüûçA-ZÁÀÂÄÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÇ0-9 ])([¹²³⁴⁵⁶⁷⁸⁹])', '\g<1> '+unitesvar['puiss']+' \g<2>', texte)
texte=texte.replace("¹","1")
texte=texte.replace("²","2")
texte=texte.replace("³","3")
texte=texte.replace("⁴","4")
texte=texte.replace("⁵","5")
texte=texte.replace("⁶","6")
texte=texte.replace("⁷","7")
texte=texte.replace("⁸","8")
texte=texte.replace("⁹","9")
for mesure, nom in mesuresvar.items():
#Putting singular name after 1 or 0
texte=re.sub('([^0-9 ]|[^0-9] )([0-1])(,([0-9]+))? ?'+mesure+'([\W]¹²³⁴⁵⁶⁷⁸⁹|$)','\g<1>\g<2>\g<3> '+nom[0]+'\g<5>',texte)
#Putting singular name after singular determiners
texte=re.sub('('+'|'.join(singDeterminers)+') '+mesure+'([\W]¹²³⁴⁵⁶⁷⁸⁹|$)','\g<1> '+nom[0]+'\g<2>',texte)
#Otherwise putting plural names
texte=re.sub('([^a-záàâäèéëêíìïîòóôöùúüûçA-ZÁÀÂÄÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÇ])'+mesure+'([\W¹²³⁴⁵⁶⁷⁸⁹]|$)','\g<1> '+nom[1]+'\g<2>',texte)
#On vérifie les carrés
texte=re.sub(nom[1]+' (carrat|cubic)([^s])', nom[1]+' \g<1>s\g<2>' ,texte)
for unite, nom in unitesvar.items():
#On met le nom au singulier après 1 ou 0
texte=re.sub('([^0-9 ]|[^0-9] )([0-1])(,([0-9]+))? ?'+unite+'([\W]|$)','\g<1>\g<2>\g<3> '+nom[0]+'\g<5>',texte)
#On met le nom au pluriel après un autre chiffre
texte=re.sub('([0-9]) ?'+unite+'([\W]|$)','\g<1> '+nom[1]+'\g<2>', texte)
texte=re.sub(unitesvar['m'][0]+' '+unitesvar['carre'], unitesvar['m2'][0], texte)
texte=re.sub(unitesvar['m'][1]+' '+unitesvar['carre'], unitesvar['m2'][1], texte)
texte=re.sub(unitesvar['m'][0]+' '+unitesvar['cube'], unitesvar['m3'][0], texte)
texte=re.sub(unitesvar['m'][1]+' '+unitesvar['cube'], unitesvar['m3'][1], texte)
#Atomes
#atomes names ----> to complete
atomes={
'CO2':'cé o dos',
'O2':'o dos',
'H2O':'atʃa dos o',
}
for code, nom in atomes.items():
texte=re.sub('(^|[\W])'+code+'([\W]|$)','\g<1>'+nom+'\g<2>',texte)
self.content = texte
#Processing roman numbers
def tractRomanNumbers(self):
#ordinal numbers abbreviations ending ----> to complete
terminaisonsord=['e', 'au', 'en', 'n', 'e', 'er', 'era', 'èr', 'èra', 'nd', 'nda', 'd', 'da', 'ra', 'me', 'ma', 'esme', 'sme', 'esma', 'sma']
#words for "century" ----> to complete
siecle=['s.', 'sègle', 'siècle', 'secle', 'siecle']
siecleespap=[]
siecleespav=[]
for s in siecle:
s=s.replace('.', '\.')
s=s+' '
siecleespap.append(s+' ')
siecleespav.append(' '+s)
texte=self.content
#Replacing CD width "cédé" ----> to complete (change "cédé" with the pronunciation of this word in your language)
texte=re.sub('([\W]|^)CD([\W]|$)', '\g<1>cédé\g<2>', texte)
#Searching for patterns
texte=re.sub('([\W]|^)([IVXLCDM]{2,})([\W]|$)', '\g<1>#\g<2>#\g<3>', texte)
texte=re.sub('([\W]|^)([IVXLCDM]{2,})('+'|'.join(terminaisonsord)+')([\W]|$)', '\g<1>#\g<2>#\g<3>\g<4>', texte)
texte=re.sub('([\W]|^)([X])(au|en)([\W]|$)', '\g<1>#\g<2>#\g<3>\g<4>', texte)
texte=re.sub('([\W]|^)('+'|'.join(siecleespap)+')([IVXLCDM])([\W]|$)', '\g<1>\g<2>#\g<3>#\g<4>', texte)
texte=re.sub('([\W]|^)([IVXLCDM])('+'|'.join(siecleespav)+')', '\g<1>#\g<2>#\g<3>', texte)
motif1='(#([IVXLCDM]+)#)'
for resulta1 in re.findall(motif1, texte):
resulta1=resulta1[1]
chiffres=romanToArabic(resulta1)
texte=re.sub('\#'+resulta1+'\#',str(chiffres), texte)
self.content = texte;
#Numbers processing
def tractNumbers(self):
texte=self.content
# Checking for phone numbers
texte=re.sub('(^|[^0-9 ])( ?)0([1-9])([\.\- ])([0-9]{2})\4([0-9]{2})\4([0-9]{2})\4([0-9]{2})( ?)([^0-9 ]|$)', '\g<1> '+chiffresvar['0']+' #¬\g<3>¬# #¬\g<5>¬# #¬\g<6>¬# #¬\g<7>¬# #¬\g<8>¬#\g<9>\g<10>', texte)
texte=re.sub('(^|[^0-9 ])( ?)\+([1-9]{2}) ?([0-9]) ([0-9 ]+)([0-9])( ?)([^0-9 ]|$)', '\g<1>\g<2>+#¬\g<3>¬# #¬\g<4>¬# /___tel___\g<5>\g<6>___tel___/\g<7>\g<8>', texte)
while re.search('/___tel___([^_]*?)([^\#]) ', texte):
texte=re.sub(')/___tel___([^_]*?)([^\#]) ', '/___tel___\g<1>\g<2>¬# #¬', texte)
texte=texte.replace('/___tel___', '#¬')
texte=texte.replace('___tel___/', '¬#')
# Checking for operations
texte=re.sub('([0-9]+) ?- ?([0-9]+)', '\g<1> '+chiffresvar['moins']+' \g<2>', texte)
texte=re.sub('([0-9]+) ?(x|\*) ?([0-9]+)', '\g<1> '+chiffresvar['fois']+' \g<3>', texte)
# Number pattern
motif='(^|[^0-9\¬# ])( ?)([0-9| |]+)((,|\.)([0-9]+)([0-9| |]*?))?( ?)([^0-9\¬# ]|$)'
# Surrounding numbers with #¬ ¬# */
for i in range(0,10):
texte=re.sub(motif,'\g<1>\g<2>#¬\g<3>\g<4>¬#\g<8>\g<9>',texte)
texte=re.sub('#¬ *?¬#', ' ', texte)
texte=re.sub('#¬ ', ' #¬', texte)
texte=re.sub(' ¬#', '¬# ', texte)
while re.search('#¬([^¬]*?) ',texte):
texte=re.sub('#¬([^¬]*?) ', '#¬\g<1>', texte)
# Processing zeros */
#Telling it if it's before a comma
texte=re.sub('#¬ ?(0*?)0 ?¬# ?,', '#¬\g<1>¬# '+chiffresvar['0']+' ,', texte)
#Telling it if it's before another zero
while re.search('#¬ ?(0*?)0 ?¬# ?'+chiffresvar['0']+'', texte):
texte=re.sub('#¬ ?(0*?)0 ?¬# ?'+chiffresvar['0']+'', '#¬\g<1>¬#'+chiffresvar['0']+' '+chiffresvar['0']+'', texte)
#Telling it if it's after a comma
texte=re.sub(', ?#¬ ?0',' '+virgule+' '+chiffresvar['0']+' #¬',texte)
#Telling it if it's at the beginning of a number
texte=re.sub('#¬ ?0', ' '+chiffresvar['0']+' #¬', texte)
#Telling it if it's after a zero
while re.search(''+chiffresvar['0']+' #¬ ?0', texte):
texte=re.sub(''+chiffresvar['0']+' #¬ ?0', ''+chiffresvar['0']+' '+chiffresvar['0']+' #¬\g<1>', texte)
texte=re.sub(' ¬#', '¬# ', texte)
texte=re.sub('#¬ ', ' #¬', texte)
texte=re.sub('#¬([12])¬#', '#¬\g<1>¬#æ', texte)
motif1='(#¬([0-9,\. ]+)¬#)'
for resulta1 in re.findall(motif1, texte):
resulta1=resulta1[0]
#Sorting the result and splitting with commas
resulta1=re.sub('^( *?)#(.*?)#( *?)$', '\g<2>', resulta1)
resulta1=resulta1.strip(" ")
resulta1=resulta1.replace('¬', '')
resulta_expl=resulta1.replace('.', ',')
resultas=resulta_expl.split(',')
# Memorize the initial number
chifra=resulta1
# Initialize final number
resfin="";
# For every side of the comma
for resulta in resultas:
resulta=resulta.replace(' ', '')
# ------- If there are thousands, millions, billions... ------- */
#Millions of millions*/
resulta=re.sub('(.)(...............)$', '\g<1>±\g<2>', resulta)
#Thousand billions*/
resulta=re.sub('(.)(............)$', '\g<1>#\g<2>', resulta)
#Billions
resulta=re.sub('(.)(.........)$', '\g<1>£\g<2>', resulta)
#Millions
resulta=re.sub('(.)(......)$', '\g<1>§\g<2>', resulta)
#Thousands
resulta=re.sub('(.)(...)$', '\g<1>#\g<2>', resulta)
# ------- Putting into letters the last numeral ------- */
# Tens */
resulta=re.sub('10([§|£|,|#|±]|$)',chiffresvar['10']+' \g<1>',resulta)
resulta=re.sub('11([§|£|,|#|±]|$)',chiffresvar['11']+' \g<1>',resulta)
resulta=re.sub('12([§|£|,|#|±]|$)',chiffresvar['12']+' \g<1>',resulta)
resulta=re.sub('13([§|£|,|#|±]|$)',chiffresvar['13']+' \g<1>',resulta)
resulta=re.sub('14([§|£|,|#|±]|$)',chiffresvar['14']+' \g<1>',resulta)
resulta=re.sub('15([§|£|,|#|±]|$)',chiffresvar['15']+' \g<1>',resulta)
resulta=re.sub('16([§|£|,|#|±]|$)',chiffresvar['16']+' \g<1>',resulta)
resulta=re.sub('17([§|£|,|#|±]|$)',chiffresvar['17']+' \g<1>',resulta)
resulta=re.sub('18([§|£|,|#|±]|$)',chiffresvar['18']+' \g<1>',resulta)
resulta=re.sub('19([§|£|,|#|±]|$)',chiffresvar['19']+' \g<1>',resulta)
# Twenties */
resulta=re.sub('20([§|£|,|#|±]|$)',chiffresvar['20']+' \g<1>',resulta)
resulta=re.sub('21([§|£|,|#|±]|$)',chiffresvar['20']+'-e-'+chiffresvar['1']+' \g<1>',resulta)
resulta=re.sub('22([§|£|,|#|±]|$)',chiffresvar['20']+'-e-'+chiffresvar['2']+' \g<1>',resulta)
resulta=re.sub('23([§|£|,|#|±]|$)',chiffresvar['20']+'-e-'+chiffresvar['3']+' \g<1>',resulta)
resulta=re.sub('24([§|£|,|#|±]|$)',chiffresvar['20']+'-e-'+chiffresvar['4']+' \g<1>',resulta)
resulta=re.sub('25([§|£|,|#|±]|$)',chiffresvar['20']+'-e-'+chiffresvar['5']+' ',resulta)
resulta=re.sub('26([§|£|,|#|±]|$)',chiffresvar['20']+'-e-'+chiffresvar['6']+' \g<1>',resulta)
resulta=re.sub('27([§|£|,|#|±]|$)',chiffresvar['20']+'-e-'+chiffresvar['7']+' \g<1>',resulta)
resulta=re.sub('28([§|£|,|#|±]|$)',chiffresvar['20']+'-e-'+chiffresvar['8']+' \g<1>',resulta)
resulta=re.sub('29([§|£|,|#|±]|$)',chiffresvar['20']+'-e-'+chiffresvar['9']+' \g<1>',resulta)
# Units */
resulta=re.sub('1([§|£|,|#|±]|$)',chiffresvar['1']+'\g<1>',resulta)
resulta=re.sub('2([§|£|,|#|±]|$)',chiffresvar['2']+'\g<1>',resulta)
resulta=re.sub('3([§|£|,|#|±]|$)',chiffresvar['3']+'\g<1>',resulta)
resulta=re.sub('4([§|£|,|#|±]|$)',chiffresvar['4']+'\g<1>',resulta)
resulta=re.sub('5([§|£|,|#|±]|$)',chiffresvar['5']+'\g<1>',resulta)
resulta=re.sub('6([§|£|,|#|±]|$)',chiffresvar['6']+'\g<1>',resulta)
resulta=re.sub('7([§|£|,|#|±]|$)',chiffresvar['7']+'\g<1>',resulta)
resulta=re.sub('8([§|£|,|#|±]|$)',chiffresvar['8']+'\g<1>',resulta)
resulta=re.sub('9([§|£|,|#|±]|$)',chiffresvar['9']+'\g<1>',resulta)
resulta=re.sub('0([§|£|,|#|±]|$)',chiffresvar['0']+'\g<1>',resulta)
# ------- Putting into letters the second-to-last numeral ------- */
resulta=re.sub('3([a-z|à|á|è|é|í|ò|ó|ú]+)([§|£|,|#|±]|$)',chiffresvar['30']+' \g<1>\g<2>',resulta)
resulta=re.sub('4([a-z|à|á|è|é|í|ò|ó|ú]+)([§|£|,|#|±]|$)',chiffresvar['40']+' \g<1>\g<2>',resulta)
resulta=re.sub('5([a-z|à|á|è|é|í|ò|ó|ú]+)([§|£|,|#|±]|$)',chiffresvar['50']+' \g<1>\g<2>',resulta)
resulta=re.sub('6([a-z|à|á|è|é|í|ò|ó|ú]+)([§|£|,|#|±]|$)',chiffresvar['60']+' \g<1>\g<2>',resulta)
resulta=re.sub('7([a-z|à|á|è|é|í|ò|ó|ú]+)([§|£|,|#|±]|$)',chiffresvar['70']+' \g<1>\g<2>',resulta)