forked from computationalstylistics/stylo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
textnorm.py
1192 lines (1052 loc) · 44.9 KB
/
textnorm.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#**************************************************
# 2020 text normalisation Python3 Lib, Prof. Charlotte Schubert Alte Geschichte, Leipzig
# DEF: A text normalization is everything done to equalize encoding, appearance
# and composition of a sequence of signs called a string (adopted as text). A sequence is an ordered set.
# A pattern is a distinguished sequence. There are two goals of
# normalization: The first is a common ground of signs and the second is a
# reduction of differences between two sequences of signs. Not every
# normalization step is useful for every comparison task! Remember:
# Sometimes it is important to not equalize word forms (sign members of a sequence) and
# sometimes it is important.
'''
GPLv3 copyrigth
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http:#www.gnu.org/licenses/>.
'''
'''PYTHON3'''
import unicodedata, re
#globals
false = False
true = True
doUVlatin = false;
analysisNormalform = "NFKD";
dispnormalform = "NFC";
inschrift = True
notprivalpha = [];#["ἀΐω"];
#"de" Akzente richtig, oder falsch????
listofelusion = { "δ᾽":"δὲ","δ'":"δὲ", "ἀλλ’": "ἀλλά", "ἀνθ’": "ἀντί", "ἀπ’": "ἀπό", "ἀφ’": "ἀπό","γ’": "γε","γένοιτ’": "γένοιτο","δ’": "δέ","δι’": "διά","δύναιτ’": "δύναιτο","εἶτ’": "εἶτα","ἐπ’": "ἐπί","ἔτ’": "ἔτι","ἐφ’": "ἐπί","ἡγοῖντ’": "ἡγοῖντο","ἵν’": "ἵνα","καθ’": "κατά","κατ’": "κατά","μ’": "με","μεθ’": "μετά","μετ’": "μετά","μηδ’": "μηδέ","μήδ’": "μηδέ","ὅτ’": "ὅτε","οὐδ’": "οὐδέ","πάνθ’": "πάντα","πάντ’": "πάντα","παρ’": "παρά","ποτ’": "ποτε","σ’": "σε","ταῦθ’": "ταῦτα","ταῦτ’": "ταῦτα","τοῦτ’": "τοῦτο","ὑπ’": "ὑπό","ὑφ’": "ὑπό"}
satzzeichen = [".", ";", ",", ":", "!", "?", "·", ")", "("]
#original abschrift, Klammerbehandlungfließtext
#Inschriften Klammersystem
#https:#apps.timwhitlock.info/js/regex#
lueckeBestimmt = re.compile( r"\[[Ͱ-Ͼἀ-῾|◌̣ ]+\]" ) #l0
lueckeinZeile = re.compile( r"\[\-\-\-\]" ) #klasse l1
lueckeinZeile2 = re.compile( r"\[3\]" ) #lueckeinZeile, klasse l1
lueckeausZeile = re.compile( r"\[\-\-\-\-\-\-\]" ) #klasse l2
lueckeausZeile2 = re.compile( r"\[6\]" ) #Luecke im Umfang einer Zeile, Klasse l2
lueckeunbest = re.compile( r"\]\[" ) # Klasse l3
zeilenende = re.compile( r" \/ " ) # Klasse l4
zeilenendeDigit = re.compile( r" \/ \d+ " ) # Klasse l4
zeilenanfang = re.compile( r" \| " ) # Zeilenanfang, Klasse l5
zeilenanfangDigit = re.compile( r" \| \d+ " ) # Zeilenanfang, Klasse l5
aufabk = re.compile( r"\(\)" ) #Auflösung von Abkürzungen, Klasse l6
beschaedigt = re.compile( r"\[nurbuchstabenoderleer\]" ) #beschädigt oder undeutlich, klasse l7
getilgt = re.compile( r"\{\}" ) # Tilgung, Klasse l8
rasiert = re.compile( r"\[\[\]\]" ) #Rasur, Klasse l9
ueberschr = re.compile( r"\<\<\>\>" ) # Überschrieben, Klasse l10
tilgrewrite = re.compile( r"\<\<\[\[\]\]\>\>" ) #Tilgung Wiedereinfügung, Klasse l11
punktunter = "◌̣ "; #Punkt unter Buchstaben - Buchstabe nur Teilweise erhalten -- später, Klasse l12
anzgriechbuch = re.compile( r" \.+ " ) #Anzahl unbestimmabrer griechischen Bustaben, Klasse l13
anzlatbuchs = re.compile( r" \++ " ) #Anzahl unbestimmbarer römsicher Buchstaben, Klasse l14
korrdeseditors = re.compile( r"\<\>" ) #Korrektur des Editors, Klasse l15
#**************************************************
# Section 0000
# helper
#**************************************************
ronum = {#not perfect
"i" :1,
"ii" :1,
"iii" :1,
"iiii" :1,
"iv" :1,
"v" :1,
"vii" :1,
"viii" :1,
"ix" :1,
"x" :1,
"xi" :1,
"xii" :1,
"xiii" :1,
"xiv" :1,
"xv" :1,
"xvi" :1,
"xvii" :1,
"xviii" :1,
"xix" :1,
"xx" :1,
"xxi" :1,
"xxii" :1,
"xxiii" :1,
"xxiv" :1,
"xxv" :1,
"xxvi" :1,
"xxvii" :1,
"xxviii" :1,
"xxix" :1,
"xxx" :1,
"xxxi" :1,
"xxxii" :1,
"xxxiii" :1,
"xxxiv" :1,
"xxxv" :1,
"xxxvi" :1,
"xxxvii" :1,
"xxxviii" :1,
"xxxix" :1,
"xl" :1,
"xli" :1,
"xlii" :1,
"xliii" :1,
"xliv" :1,
"xlv" :1,
"xlvi" :1,
"xlvii" :1,
"xlviii" :1,
"xlix" :1,
"l" :1,
"li" :1,
"lii" :1,
"liii" :1,
"liv" :1,
"lv" :1,
"lvi" :1,
"lvii" :1,
"lviii" :1,
"lix" :1,
"lx" :1,
"lxi" :1,
"lxii" :1,
"lxiii" :1,
"lxiv" :1,
"lxv" :1,
"lxvi" :1,
"lxvii" :1,
"lxviii" :1,
"lxix" :1,
"lxx" :1,
"lxxi" :1,
"lxxii" :1,
"lxxiii" :1,
"lxxiv" :1,
"lxxv" :1,
"lxxvi" :1,
"lxxvii" :1,
"lxxviii" :1,
"lxxix" :1,
"lxxx" :1,
"lxxxi" :1,
"lxxxii" :1,
"lxxxiii" :1,
"lxxxiv" :1,
"lxxxv" :1,
"lxxxvi" :1,
"lxxxvii" :1,
"lxxxviii" :1,
"lxxxix" :1,
"xc" :1,
"xci" :1,
"xcii" :1,
"xciii" :1,
"xciv" :1,
"xcv" :1,
"xcvi" :1,
"xcvii" :1,
"xcviii" :1,
"xcix" :1,
"c":1
}
grnum = {#not perfect
"α" :1,
"β" :1,
"γ" :1,
"δ" :1,
"ε" :1,
"ϛ" :1,
"ζ" :1,
"η" :1,
"θ" :1,
"ι" :1,
"ια" :1,
"ιβ" :1,
"ιγ" :1,
"ιδ" :1,
"ιε" :1,
"ιϛ" :1,
"ιζ" :1,
"ιη" :1,
"ιθ" :1,
"κ" :1,
"κα" :1,
"κβ" :1,
"κγ" :1,
"κδ" :1,
"κε" :1,
"κϛ" :1,
"κζ" :1,
"κη" :1,
"κθ" :1,
"λ" :1,
"λα" :1,
"λβ" :1,
"λγ" :1,
"λδ" :1,
"λε" :1,
"λϛ" :1,
"λζ" :1,
"λη" :1,
"λθ" :1,
"μ" :1,
"μα" :1,
"μβ" :1,
"μγ" :1,
"μδ" :1,
"με" :1,
"μϛ" :1,
"μζ" :1,
"μη" :1,
"μθ" :1,
"ν" :1,
"να" :1,
"νβ" :1,
"νγ" :1,
"νδ" :1,
"νε" :1,
"νϛ" :1,
"νζ" :1,
"νη" :1,
"νθ" :1,
"ξ" :1,
"ξα" :1,
"ξβ" :1,
"ξγ" :1,
"ξδ" :1,
"ξε" :1,
"ξϛ" :1,
"ξζ" :1,
"ξη" :1,
"ξθ" :1,
"ο" :1,
"οα" :1,
"οβ" :1,
"ογ" :1,
"οδ" :1,
"οε" :1,
"οϛ" :1,
"οζ" :1,
"οη" :1,
"οθ" :1,
"π" :1,
"πα" :1,
"πβ" :1,
"πγ" :1,
"πδ" :1,
"πε" :1,
"πϛ" :1,
"πζ" :1,
"πη" :1,
"πθ" :1,
"ϟ" :1,
"ϟα" :1,
"ϟβ" :1,
"ϟγ" :1,
"ϟδ" :1,
"ϟε" :1,
"ϟϛ" :1,
"ϟζ" :1,
"ϟη" :1,
"ϟθ" :1,
"ρ" : 1
}
def isnumber( maybe ):
#do romannumbers
if( maybe.isdigit() ):
return True
elif( maybe in ronum ):
return True
elif( maybe in grnum ):
return True
return False
#**************************************************
# Section 000
# basic UNICODE NORMAL FORM
#**************************************************
def setAnaFormTO( fnew ):
analysisNormalform = fnew
def setDisplFormTO( fnew ):
dispnormalform = fnew
def normarrayk( aarray ):
replacearray = {};
for p in aarray:
replacearray[ disambiguDIAkritika( unicodedata.normalize( analysisNormalform, p ) ) ] = aarray[ p ];
return replacearray;
def normarrayval( aarray ): # by reference ????
for p in aarray:
aarray[ p ] = disambiguDIAkritika( unicodedata.normalize( analysisNormalform, aarray[ p ] ) );
#def normatextwordbyword( text, wichnorm ):
# spt = text.split( " " )
# lele = len( spt )
# for w in range( lele ):
# nw = normatext( spt[ w ], wichnorm )
# spt[ w ] = nw
# return " ".join( spt )
def normatexttoanaenc( text ):
#print(text, len(text))
return normatext( text, analysisNormalform )
def normatext( text, wichnorm ):
spt = text.split( " " )
for w in range( len( spt ) ):
nw = sameuninorm( spt[ w ], wichnorm )
spt[ w ] = nw;
return " ".join( spt )
# def takes sting and normalform string (for example "NFD")
def sameuninorm( aword, wichnorm ):
return unicodedata.normalize( wichnorm, aword )
vokaleGRI = {"ι":1,"υ":1,"ε":1,"ο":1,"α":1,"ω":1,"η":1}
buchstGRI = {"Α":"A", "α":"a", "Β":"B", "β":"b", "Γ":"G", "γ":"g", "Δ":"D", "δ":"d", "Ε":"E", "ε":"e", "Ζ":"Z", "ζ":"z", "Η":"H", "η":"h", "Θ":"Th", "θ":"th", "Ι":"I", "ι":"i", "Κ": "K", "κ":"k", "Λ":"L", "λ":"l", "Μ":"M", "μ":"m", "Ν":"N", "ν":"n", "Ξ":"Xi", "ξ":"xi", "Ο":"O", "ο":"o", "Π":"P", "π":"p", "Ρ":"R", "ρ":"r", "Σ":"S", "σ":"s", "ς":"s", "Τ":"T", "τ":"t", "Υ":"U", "υ":"u", "Φ":"Ph", "φ":"ph", "Χ":"X", "χ":"x", "Ψ":"Ps", "ψ":"ps", "Ω":"O", "ω":"o"}
buchstLAT = {"d":1, "g":1, "p":1, "t":1, "c":1, "k":1, "q":1, "qu":1, "ph":1, "th":1, "ch":1, "x":1, "z":1, "f":1, "v":1, "s":1, "m":1, "n":1, "l":1, "r":1, "a":1,"i":2,"e":3,"o":4,"u":5,"v":6, "y":7}
groups = {"γγ":["n", "g"], "γκ":["n", "c"], "γξ":["n","x"], "γχ":["n", "ch"], "ηυ":["ē", "u"]}; #only great letters??????? what is with that?
behauchung = { "῾":"h" };
buchsCoptic = {"ϐ": "B", "ϑ":"Th", "ϱ":"r", "ϰ":"k", "ϒ":"y", "ϕ":"ph", "ϖ":"p", "Ϝ":"W", "ϝ":"w", "Ϙ":"Q","ϙ":"q", "Ϟ":"ḳ", "ϟ":"ḳ", "Ϲ":"S", "Ⲥ":"S", "ⲥ":"s", "ϲ":"s", "Ͻ":"S", "ͻ":"s","Ϳ ":"j","ϳ":"j","Ͱ":"h","ͱ":"h","Ⲁ":"A","ⲁ":"a",
"ϴ":"t","Ⲑ":"t","ⲑ":"t","ϵ":"e","϶":"e","Ϸ":"Sh","ϸ":"sh", "ϼ":"P","Ϡ":"S","ϡ":"S","Ⳁ":"S","ⳁ":"s",
"Ͳ":"Ss", "ͳ":"ss", "Ϻ":"S","ϻ":"s", "Ϣ":"š","ϣ":"š", "Ϥ":"F","ϥ":"f", "Ϧ":"X", "Ⳉ":"X",
"ϧ":"x","ⳉ":"x", "Ϩ":"H", "ϩ":"h", "Ϫ":"J", "ϫ":"j", "Ϭ":"C","ϭ":"c","Ϯ":"Di","ϯ":"di",
"Ͼ":"S", "Ͽ":"S", "ͼ":"s", "ͽ":"s", "Ⲃ":"B","ⲃ":"b","Ⲅ":"G","ⲅ":"g", "Ⲇ":"D", "ⲇ":"d", "Ⲉ":"E", "ⲉ":"e",
"Ⲋ":"St", "ⲋ":"st", "Ⲍ":"Z", "ⲍ":"z", "Ⲏ":"ê", "ⲏ":"ê", "Ⲓ":"I", "ⲓ":"i", "Ⲕ":"K", "ⲕ":"k",
"Ⲗ":"L", "ⲗ":"l", "Ⲙ":"M", "ⲙ":"m", "Ⲛ":"N","ⲛ":"n", "Ⲝ":"ks", "ⲝ":"ks", "Ⲟ ":"O", "ⲟ":"o",
"Ⲡ":"B", "ⲡ":"b", "Ⲣ":"R","ⲣ":"r", "Ⲧ":"T", "ⲧ":"t", "Ⲩ":"U", "ⲩ":"u", "Ⲫ":"F","ⲫ":"f","Ⲭ":"Kh", "ⲭ":"kh",
"Ⲯ":"Ps", "ⲯ":"ps", "Ⲱ":"ô", "ⲱ":"ô", "Ͷ":"W", "ͷ":"w"}; #
spai1 = re.compile( "\u2002".encode("utf-8").decode("utf-8") );#enspacing
spai2 = re.compile( "\u2000".encode("utf-8").decode("utf-8") );#enquad
def sameallspacing( astr ):
astr = re.sub( spai1, ' ', astr)
astr = re.sub( spai2, ' ', astr)
return astr
diam1 = re.compile( "\u0027".encode("utf-8").decode("utf-8") )
diam2 = re.compile( r"'" )
diam3 = re.compile( "\u1FBD".encode("utf-8").decode("utf-8") )
def disambiguDIAkritika( astr ):
astr = re.sub( diam1, "\u2019", astr)
astr = re.sub( diam2, "\u2019", astr)
astr = re.sub( diam3, "\u2019", astr)
return astr
def gravisakut( astr ):
astr = re.sub( diacriticsunicodeRegExp[10], "\u00B4", astr )
return astr
def disambiguadashes( astring ):
astring = re.sub( cleangeviert, '-', astring)
astring = re.sub( cleanhalbgeviert, '-', astring)
astring = re.sub( cleanziffbreitergeviert, '-', astring)
astring = re.sub( cleanviertelgeviert, '-', astring)
astring = re.sub( cleanklgeviert, '-', astring)
astring = re.sub( cleanklbindstrichkurz, '-', astring)
astring = re.sub( cleanklbindstrichvollbreit, '-', astring)
return astring
def ExtractDiafromBuchst( buchst ):
toitter = list( unicodedata.normalize( analysisNormalform, buchst ) );
b = [];
d = [];
for t in range( len( toitter ) ):
co = toitter[t].lower( );
if( co in buchstGRI or co in buchsCoptic or co in buchstLAT ):
b.append( toitter[t] );
else:
d.append( toitter[t] );
return ["".join( d ), "".join( b )];
def ExtractDiafromBuchstText( atext ):
t = ""
spli = atext.split( " " )
lspli = len( spli )
for i in range( lspli ):
t += "[ "+", ".join( ExtractDiafromBuchst( spli[ i ] ) )+" ]"
return t
def replaceBehauchung( adiakstring ): #Latin
if( "῾" in adiakstring ):
return "h"+adiakstring.replace( "῾","" );
else:
return adiakstring;
def Expandelision( aword ):
if( aword in listofelusion ):
return listofelusion[ aword ]
else:
return aword
def ExpandelisionText( atext ):
t = "";
wds = atext.split( " " )
lwds = len( wds )
for w in range( lwds):
t += " "+ Expandelision( wds[ w ] )
return t
def TraslitAncientGreekLatin( astring ):
wordlevel = delligaturen( unicodedata.normalize( "NFC", iotasubiotoad( unicodedata.normalize( "NFD" , astring.strip() ) ) ) ).split(" "); #care for iotasubscriptum, Ligature
#de δ’ !!!
romanized = [];
for w in range( len( wordlevel ) ):
buchstlevel = list( wordlevel[ w ] );
grouped = [];
notlastdone = true;
extractedida2 = "";
extracteBUCHST2 = "";
for b in range( 1, len( buchstlevel ) ):
if( buchstlevel[ b-1 ] == "" ):
continue;
zwischenerg1 = ExtractDiafromBuchst( buchstlevel[ b-1 ] );
zwischenerg2 = ExtractDiafromBuchst( buchstlevel[ b ] );
extractedida1 = zwischenerg1[0];
extractedida2 = zwischenerg2[0];
extracteBUCHST1 = zwischenerg1[1];
extracteBUCHST2 = zwischenerg2[1];
if( extracteBUCHST1+extracteBUCHST2 in groups and not "¨" in extractedida2 ): #wenn kein trema über dem zweiten buchstaben - diaresis keine Zusammenziehung (synresis)
gou = groups[ extracteBUCHST1+extracteBUCHST2 ];
grouped.append( unicodedata.normalize( "NFC", gou[0]+replaceBehauchung(extractedida1)+gou[1]+replaceBehauchung(extractedida2) ) )
buchstlevel[ b ] = "";#dealread in groupand revistible
notlastdone = false;
else:
if( extracteBUCHST1 in buchstGRI ):
grouped.append( unicodedata.normalize( "NFC", buchstGRI[extracteBUCHST1]+replaceBehauchung(extractedida1) ) )
else:
if( extracteBUCHST1 in buchsCoptic ):
grouped.append( unicodedata.normalize( "NFC", buchsCoptic[extracteBUCHST1]+replaceBehauchung(extractedida1) ) )
else:
#realy not - leave IT
grouped.append( buchstlevel[ b-1 ] );
notlastdone = true;
if( notlastdone ):
if( extracteBUCHST2 in buchstGRI ):
grouped.append( unicodedata.normalize( "NFC", buchstGRI[extracteBUCHST2]+replaceBehauchung(extractedida2) ) )
else:
if( extracteBUCHST2 in buchsCoptic ):
grouped.append( unicodedata.normalize( "NFC", buchsCoptic[extracteBUCHST2]+replaceBehauchung(extractedida2) ) )
else:
#realy not - leave IT
grouped.append( buchstlevel[ len( buchstlevel )-1 ] );
romanized.append( "".join( grouped ) );
return " ".join( romanized );
#**************************************************
# Section 00
# basic cleaning and string conversion via regexp
#**************************************************
cleanhtmltags = re.compile( r"<.*?>" );
cleanhtmlformat1 = re.compile( ' ' )
regEbr1 = re.compile( "<br/>" );
regEbr2 = re.compile( "<br>" )
cleanNEWL = re.compile( '\\n' )
cleanRETL = re.compile( '\\r' )
cleanTA = re.compile( '\\t' )
cleanstrangehochpunkt =re.compile( r'‧' )
cleanthisbinde =re.compile( r'—' )
cleanthisleer =re.compile( '\xa0'.encode("utf-8").decode("utf-8") ) #byte string is not allowed -
cleanleerpunkt =re.compile( r' \.' )
cleanleerdoppelpunkt =re.compile( r' :' )
cleanleerkoma =re.compile( r' ,' )
cleanleersemik =re.compile( r' ;' )
cleanleerausrufe =re.compile( r' !' )
cleanleerfrege =re.compile( r' \?' )
# breakdown typographic letiances "Bindestriche und Geviertstriche"
cleanklbindstrichvollbreit =re.compile( r'-' )
cleanklbindstrichkurz =re.compile( r'﹣' )
cleanklgeviert =re.compile( r'﹘' )
cleanviertelgeviert =re.compile( r'‐' )
cleanziffbreitergeviert =re.compile( r'‒' )
cleanhalbgeviert =re.compile( r'–' )
cleangeviert =re.compile( r'—' )
escspitzeL =re.compile( r'<' )
escspitzeR =re.compile( r'>' )
def spitzeklammernHTML( astr ):
astr = re.sub( escspitzeL, '<', astr )
astr = re.sub( escspitzeR, '>', astr )
return astr
def basClean( astring ):
astring = re.sub( cleanNEWL, " <br/>", astring )
astring = re.sub( cleanRETL, " <br/>", astring)
astring = re.sub( cleanstrangehochpunkt,"·", astring)
astring = re.sub( cleanthisbinde," — ", astring)
astring = re.sub( cleanthisleer, ' ', astring)
astring = re.sub( cleanleerpunkt, '.', astring)
astring = re.sub( cleanleerdoppelpunkt, ':', astring)
astring = re.sub( cleanleerkoma, ',', astring)
astring = re.sub( cleanleersemik, ';', astring)
astring = re.sub( cleanleerausrufe, '!', astring)
astring = re.sub( cleanleerfrege, '?', astring)
astring = disambiguadashes( astring )
# remove hyphens
ws = astring.split(" ");
ca = [];
halfw = "";
secondhalf = "";
for w in range( len( ws ) ):
if( "-" in ws[w] ):
h = ws[w].split( "-" );
halfw = h[0].replace(" ", "");
secondhalf = h[1].replace(" ", "");
if( "]" in secondhalf ):
hh = h[1].split("]");
if( len( hh[1] ) > 1 ):
ca.append( halfw + hh[1] + " " + hh[0] + "]<br/>" );
halfw = "";
secondhalf = "";
elif( "<br/>" != ws[w] and ws[w] != "" and ws[w] != " " and halfw != "" ):
if( "]" in ws[w] ):
secondhalf = ws[w].replace(" ", "");
else:
ca.append( halfw + ws[w].replace("<br/>", "") + " " + secondhalf + "<br/>" ); #trennstriche
halfw = "";
secondhalf = "";
else:
if( ws[w] != "" ): #remove mehrfache leerstellen
ca.append( ws[w] );
return " ".join( ca );
def ohnesatzzeichen( wliste ):
lsatzz = len( satzzeichen )
lwdl = len( wliste )
for sa in range( lsatzz ):
for w in range( lwdl ):
wliste[ w ] = "".join( wliste[ w ].split( satzzeichen[ sa ]))
return wliste;
def replaceOPENINGandCLOSING( astopen, astclose, strstr):
no = strstr.split( astclose )
NO = ""
for n in no:
NO += n.split( astopen )[0]
return NO
#usage: replaceWordsfromarray( ["in", "cum", "et", "a", "ut"], stringggg )
def replaceWordsfromarray( arr, replacement,strstr ):
for a in arr:
strstr = strstr.replace( arr[a], replacement )
return strstr
#**************************************************
# Section 0
# word leve conversions:
# alpha privativum
# alpha copulativum
# Klammersysteme editorische
#**************************************************
def hasKEY( alist, thekey ): #fkt should move
if( thekey in alist ):
return True
else:
return False
def AlphaPrivativumCopulativum( aword ):
if( aword in notprivalpha ):
return aword
else:
if( len( aword ) >= 2 ):
z = unicodedata.normalize( analysisNormalform, aword[1] )
if( "α" in unicodedata.normalize( analysisNormalform, aword[0] ) and
("ι" in z or "υ" in z or "ε" in z or "ο" in z or "α" in z or "ω" in z or "η")
and
"\u0308" in z ):
return aword[0] +" "+ aword[1:]
else:
return aword
else:
return aword
'''
buchs = list( delall( aword ) )
if( len( buchs ) < 2 ):
return aword
if( buchs[0] == "α" ): #erste Buchstabe alpha
if( hasKEY( vokaleGRI , buchs[1] ) ): # zweiter ein Vokal
b2dia = ExtractDiafromBuchst(aword[1])[0]
if( "\u0308" in b2dia ): #zweiter Buchstabe mit Trema, erste Buchstabe mit spiritus lenis
return aword[0] +" "+ aword[1:]
else:
return aword
else:
return aword
else:
return aword
'''
def AlphaPrivativumCopulativumText( atext ):
t = ""
spli = atext.split( " " )
lspli = len( spli )
for l in range( lspli ):
#print(spli[ l ], AlphaPrivativumCopulativum( spli[ l ] ) )
t += " "+AlphaPrivativumCopulativum( spli[ l ] )
return t
def testprivatalpha():
#drittes Beispiel müsste raus genommen werden
bsp = ["ἀϊδής", "ἀΐδιος", "ἀΐω", "ἀΐσθω", "ἀΐλιος", "Ἅιδης", "ἀϊών", "αἰών", "ἀΐσσω", "ἀΐδηλος", "ἀΐζηλος", "ἀΐσδηλος", "ἄϊδρις", "ἀϊστόω", "ἀΐσυλος", "αἴσῠλος", "ἄϋλος", "αὐλός", "ἀϊών", "αἰών", ];
Strout = "";
for b in range( len( bsp ) ):
Strout += "Eingabe "+ bsp[ b ]+ " Ausgabe "+ AlphaPrivativumCopulativum( bsp[b] ) +"\n";
print( Strout )
#**************************************************
# Section 1
# unicode related comparing and norming, handling of diacritics
#**************************************************
# array of unicode diacritics (relevant for polytonic greek)
diacriticsunicodeRegExp = [
re.compile('\u0313'.encode("utf-8").decode("utf-8") ),
re.compile("\u0314".encode("utf-8").decode("utf-8") ),
re.compile("\u0300".encode("utf-8").decode("utf-8") ),
re.compile("\u0301".encode("utf-8").decode("utf-8")),
re.compile("\u00B4".encode("utf-8").decode("utf-8")), #akkut 4
re.compile("\u02CA".encode("utf-8").decode("utf-8")),
re.compile("\u02B9".encode("utf-8").decode("utf-8")),
re.compile("\u0342".encode("utf-8").decode("utf-8")),
re.compile("\u0308".encode("utf-8").decode("utf-8")),
re.compile("\u0304".encode("utf-8").decode("utf-8")),
re.compile("\u0306".encode("utf-8").decode("utf-8")) ] #Gravis 10
# def takes string, splits it with jota subscriptum and joins the string again using jota adscriptum
regJotaSub = re.compile( '\u0345'.encode("utf-8").decode("utf-8") )
def iotasubiotoad( aword ):
return re.sub( regJotaSub, "ι", aword )
#return "ι".join( aword.split( u'\u0345' ) );
# def takes "one word"
def ohnediakritW( aword ):
for dia in range( len( diacriticsunicodeRegExp ) ):
aword = re.sub( diacriticsunicodeRegExp[ dia ], "", aword )
return aword
def capitali( astring ):
return astring.capitalize();
# precompiled regular expressions
strClean1 = re.compile( r'’' )
strClean2 = re.compile( r'\'')
strClean3 = re.compile( r'᾽' )
#strClean4 = re.compile( r'´' )#Akut
#strClean5 = re.compile( r'˝' )#Doppelakut
#strClean6 = re.compile( r'˘' )#Breve
#strClean7 = re.compile('\u032E'.encode("utf-8").decode("utf-8") )#Breve darunter
#strClean8 = re.compile( r'¸' )#Cedille
#strClean9 = re.compile( r'`' )#Gravis
#strClean10 = re.compile( '\u0314'.encode("utf-8").decode("utf-8") )#Spiritus Asper
#strClean11 = re.compile( '\u0313'.encode("utf-8").decode("utf-8") )#Spiritus Lenis
#strClean12 = re.compile( '\u0342'.encode("utf-8").decode("utf-8") )#Circumflex
#strClean13 = re.compile( r'~' )#Tilde
#strClean14 = re.compile( r'¨' )#Trema
#Haken, Hatschek
# def takes a string replaces some signs with regexp and oth
def nodiakinword( aword ):
spt = unicodedata.normalize( analysisNormalform, aword )
spt = re.sub(strClean1, "", spt)
spt = re.sub(strClean2, "", spt)
spt = re.sub(strClean3, "", spt)
#spt = re.sub(strClean4, "", spt)
#aword = re.sub(strClean5, "", aword)
#aword = re.sub(strClean6, "", aword)
#aword = re.sub(strClean7, "", aword)
#aword = re.sub(strClean8, "", aword)
#aword = re.sub(strClean9, "", aword)
#aword = re.sub(strClean10, "", aword)
#aword = re.sub(strClean11, "", aword)
#aword = re.sub(strClean12, "", aword)
#aword = re.sub(strClean13, "", aword)
#aword = re.sub(strClean14, "", aword)
return iotasubiotoad( ohnediakritW( spt ) )
#**************************************************
# Section 2: deleting things that could be not same in two texts
#**************************************************
# def take a string and deletes diacritical signes, ligatures, remaining interpunction, line breaks, capital letters to small ones, equalizes sigma at the end of greek words, and removes brakets
def delall( text ):
#print(text)
if( doUVlatin ): # convert u to v in classical latin text
text = delji( deluv( delklammern( sigmaistgleich( delgrkl( delligaturen( delinterp( delmakup( delumbrbine( delnumbering( delunknown( deldiak( text))))))))))))
else:
text = delklammern( sigmaistgleich( delgrkl( delligaturen( delinterp( delmakup( delumbrbine( delnumbering( delunknown( deldiak( text ) ) ) ) ) ) ) )))
#print("###############")
#print(text)
return text
#del numbering
numeringReg1 = re.compile( r'\[([0-9\.\:\; ]+)\]' )
numeringReg2 = re.compile( r'\[[M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})]+\]' )
numeringReg3 = re.compile( r'\(([A-Za-z0-9\.\:\; ]+|([0-9\.\:\; ]+))\)' ) #runde klammern mit zahlen und buchstaben
def delnumbering( text ): #untested
text = re.sub( numeringReg1, "", text)
text = re.sub( numeringReg2, "", text)
text = re.sub( numeringReg3, "", text)
return text
# precompiled regular expressions of the relevant ligatures
regEstigma = re.compile( '\u03DA'.encode("utf-8").decode("utf-8") )
regEstigmakl = re.compile( '\u03DB'.encode("utf-8").decode("utf-8") )
regEomikonyplsi = re.compile( r'ȣ' )
regEomikonyplsiK = re.compile( r'Ȣ' )
regEUk = re.compile( r'Ꙋ' )
regEuk = re.compile( r'ꙋ' )
regEkai = re.compile( r'ϗ' )
regEKai = re.compile( r'Ϗ' )
regEl1 = re.compile( '\u0223'.encode("utf-8").decode("utf-8") )
regEl2 = re.compile( '\u0222'.encode("utf-8").decode("utf-8") )
regEl3 = re.compile( '\u03DB'.encode("utf-8").decode("utf-8") )
# def take a string and replaces all occorences of a regular expression
def delligaturen( text ):
text = re.sub( regEstigma, "στ", text)
text = re.sub( regEstigmakl, "στ", text)
text = re.sub( regEUk, "Υκ", text)
text = re.sub( regEuk, "υκ", text)
text = re.sub( regEomikonyplsi, "ου", text)
text = re.sub( regEomikonyplsiK, "ου", text)
text = re.sub( regEkai, "καὶ", text)
text = re.sub( regEKai, "Καὶ", text)
text = re.sub( regEl1, "\u039F\u03C5".encode("utf-8").decode("utf-8"), text)
text = re.sub( regEl2, "\u03BF\u03C5".encode("utf-8").decode("utf-8"), text)
text = re.sub( regEl3, "\u03C3\u03C4".encode("utf-8").decode("utf-8"), text);
return text
# def takes string and splits it into words, than normalizes each word, joins the string again
def deldiak( text ):
spt = text.split( " " ); #seperate words
for wi in range( len( spt ) ):
spt[ wi ] = nodiakinword( spt[ wi ] );
return " ".join( spt );
regEdoppelP = re.compile( r':' )
regEeinfahP = re.compile( r'\.' )
regEkomma = re.compile( r',' )
regEsemiK = re.compile( r';' )
regEhochP = re.compile( r'·' )
regEausr = re.compile( r'!' )
regEfarge = re.compile( r'\\?' )
regEan1 = re.compile( r'“' )
regEan5 = re.compile( r'„' )
regEan2 = re.compile( r'”' )
regEan3 = re.compile( r'"' )
regEan4 = re.compile( r"'" )
regpipe = re.compile( r"|" )
# def takes a string and replaces interpunction
def delinterp( text ):
text = re.sub(regEdoppelP, "", text)
text = re.sub(regEeinfahP, "", text)
text = re.sub(regEkomma, "", text)
text = re.sub(regEsemiK, "", text)
text = re.sub(regEhochP, "", text)
text = re.sub(regEausr, "", text)
text = re.sub(regEfarge, "", text)
text = re.sub(regEan1, "", text)
text = re.sub(regEan2, "", text)
text = re.sub(regEan3, "", text)
text = re.sub(regEan4, "", text)
text = re.sub(regEan5, "", text)
text = re.sub(regpipe, "", text)
return text
# function takes a string and replaces some unknown signs
regU1 = re.compile( r"†" )
regU2 = re.compile( r"\*" )
regU3 = re.compile( r"⋖" )
regU4 = re.compile( r"#" )
regU5 = re.compile( r"⸎" )
regU6 = re.compile( r"☽" )
regU7 = re.compile( r"☾" )
regU8 = re.compile( r"⁙" )
def delunknown( text ):
text = re.sub(regU1, "", text)
text = re.sub(regU2, "", text)
text = re.sub(regU3, "", text)
text = re.sub(regU4, "", text)
text = re.sub(regU5, "", text)
text = re.sub(regU6, "", text)
text = re.sub(regU7, "", text)
text = re.sub(regU8, "", text)
return text
# def takes string and replace html line breakes
def delumbrbine( text ):
#text = re.sub(regEbr1, "", text)
#text = re.sub(regEbr2, "", text)
#return text
return umbrtospace( text )
def umbrtospace( text ):
text = re.sub(cleanNEWL, " ", text)
text = re.sub(cleanRETL, " ", text)
text = re.sub(cleanTA, " ", text)
text = re.sub(regEbr1, " ", text)
text = re.sub(regEbr2, " ", text)
return text
# first version, a little more...
def delmakup( text ):
text = re.sub(cleanhtmltags, "", text)
text = re.sub(cleanhtmlformat1, "", text)
return text
# ...
def delgrkl( text ):
return text.lower();
# def takes string and converts tailing sigma to inline sigma (greek lang)
regEtailingsig = re.compile( r"ς" )
def sigmaistgleich( text ):
return re.sub(regEtailingsig, "σ", text);
regEkla1 = re.compile( r"\(" )
regEkla2 = re.compile( r"\)" )
regEkla3 = re.compile( r"\{" )
regEkla4 = re.compile( r"\}" )
regEkla5 = re.compile( r"\[" )
regEkla6 = re.compile( r"\]" )
regEkla7 = re.compile( r"<" )
regEkla8 = re.compile( r">" )
regEkla9 = re.compile( r"⌈" )
regEkla10 = re.compile( r"⌉" )
regEkla11 = re.compile( r"‹" )
regEkla12 = re.compile( r"›" )
regEkla13 = re.compile( r"«" )
regEkla14 = re.compile( r"»" )
regEkla15 = re.compile( r"⟦" )
regEkla16 = re.compile( r"⟧" )
regEkla17 = re.compile( '\u3008'.encode("utf-8").decode("utf-8") )
regEkla18 = re.compile( '\u3009'.encode("utf-8").decode("utf-8") )
regEkla19 = re.compile( '\u2329'.encode("utf-8").decode("utf-8") )
regEkla20 = re.compile( '\u232A'.encode("utf-8").decode("utf-8") )
regEkla21 = re.compile( '\u27E8'.encode("utf-8").decode("utf-8") )
regEkla22 = re.compile( '\u27E9'.encode("utf-8").decode("utf-8") )
#⌋
#⌊ -- wir brauchen eine Funktion die die signis criticis Zeichenbehandelt!!!
# def take sstring and replaces the brakets
def delklammern( text ):
text = re.sub(regEkla1, "",text)
text = re.sub(regEkla2, "",text)
text = re.sub(regEkla3, "",text)
text = re.sub(regEkla4,"",text)
text = re.sub(regEkla5,"",text)
text = re.sub(regEkla6,"",text)
text = re.sub(regEkla7,"",text)
text = re.sub(regEkla8,"",text)
text = re.sub(regEkla9,"",text)
text = re.sub(regEkla10,"",text)
text = re.sub(regEkla11,"",text)
text = re.sub(regEkla12,"",text)
text = re.sub(regEkla13,"",text)
text = re.sub(regEkla14,"",text)
text = re.sub(regEkla15,"",text)
text = re.sub(regEkla16,"",text)
text = re.sub(regEkla17,"",text)
text = re.sub(regEkla18,"",text)
text = re.sub(regEkla19,"",text)
text = re.sub(regEkla20,"",text)
text = re.sub(regEkla21,"",text)
text = re.sub(regEkla22,"",text);
return text
def deledklammern( text ):
text = re.sub(regEkla1, "",text)
text = re.sub(regEkla2, "",text)
text = re.sub(regEkla3, "",text)
text = re.sub(regEkla4,"",text)
text = re.sub(regEkla5,"",text)
text = re.sub(regEkla6,"",text)
text = re.sub(regEkla9,"",text)
text = re.sub(regEkla10,"",text)
text = re.sub(regEkla11,"",text)
text = re.sub(regEkla12,"",text)
text = re.sub(regEkla13,"",text)
text = re.sub(regEkla14,"",text)
text = re.sub(regEkla15,"",text)
text = re.sub(regEkla16,"",text)
text = re.sub(regEkla17,"",text)
text = re.sub(regEkla18,"",text)
text = re.sub(regEkla19,"",text)
text = re.sub(regEkla20,"",text)
text = re.sub(regEkla21,"",text)
text = re.sub(regEkla22,"",text);
return text
regEuv = re.compile( r"u" )
# def takes string and replaces u by v, used in classical latin texts
def deluv( text ):
return re.sub( regEuv, "v", text );
regEji = re.compile( r"j" )
# def takes string and replaces j by i, used in classical latin texts
def delji( text ):
return re.sub( regEji, "i", text );
def TrennstricherausText( astring ):
return " ".join( Trennstricheraus( disambiguadashes(astring).split( " " ) ) )
def Trennstricheraus( wliste ): #\n version
ersterteil = ""
zweiterteil = ""
neueWLISTE = []
lele = len( wliste )
for w in range( lele ):
#print(wliste[ w ], ersterteil, zweiterteil)
if( len( ersterteil ) == 0 ):
if( "-" in wliste[ w ] ):
eUNDz = wliste[ w ].split( "-" )
if( len( eUNDz[1] ) > 0 ):
zweiohnenewline = eUNDz[1].split( "\n" )
neueWLISTE.append(eUNDz[0]+zweiohnenewline[len(zweiohnenewline)-1 ])
else:
ersterteil = eUNDz[0]
#print(eUNDz.length, eUNDz)
else: #nix - normales wort
neueWLISTE.append( wliste[ w ] )
else: # es gab eine Trennung und die ging über zwei Listenzellen
if( not "[" in wliste[ w ] and not "]" in wliste[ w ] ):
zweiteralsliste = wliste[ w ].split( "\n" )
#print("split", zweiteralsliste, wliste[ w ], ersterteil+zweiteralsliste[ zweiteralsliste.length-1 ])
neueWLISTE.append(ersterteil+zweiteralsliste[ len(zweiteralsliste)-1 ] )
ersterteil = ""
else: #klammern behandeln
#wenn ich hier kein append auf der neune Wortliste mache, dann lösche ich damit die geklammerten sachen
if( "[" in wliste[ w ] and "]" in wliste[ w ] ): #klammern in einem Wort
zweiteralsliste = wliste[ w ].split( "]" )
neueWLISTE.append( ersterteil+zweiteralsliste[1].substring(1, len(zweiteralsliste[1])-1) )
#print("NO SPLIT", ersterteil+zweiteralsliste[1].substring(1, zweiteralsliste[1].length-1))
elif( "[" in wliste[ w ] ):
zweiteralsliste = wliste[ w ].split( "[" )
neueWLISTE.append( ersterteil+"".join( zweiteralsliste ) )
else: #nur schließende Klammer
zweiteralsliste = wliste[ w ].split( "]" )
neueWLISTE.append( ersterteil+zweiteralsliste[1] )
#print("NO SPLIT", ersterteil+zweiteralsliste[1].substring(1, zweiteralsliste[1].length-1))
if(ersterteil != "" and zweiterteil == "" ):
neueWLISTE.append( ersterteil+"-" )
return neueWLISTE
def Interpunktiongetrennt( wliste ):
neuewliste = []
for sa in range( len( satzzeichen ) ):
for w in range( len( wliste ) ):
if( satzzeichen[ sa ] in wliste[ w ] ):
neuewliste.append( "".join( wliste[ w ].split( satzzeichen[ sa ] ) ) )
neuewliste.append( satzzeichen[ sa ] )
else:
neuewliste.append( wliste[ w ] )
wliste = neuewliste
neuewliste = []
return wliste
def UmbruchzuLeerzeichen( atext ):
return " ".join( atext.split("\n") )
def iotasubiotoadL( wliste ):
lwdl = len( wliste )
for w in range( lwdl ):
wliste[ w ] = iotasubiotoad( wliste[ w ] )
return wliste