-
Notifications
You must be signed in to change notification settings - Fork 1
/
coll-eq.py
340 lines (323 loc) · 14.3 KB
/
coll-eq.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import sys
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import os
nerr=0
dirdone=[]
def valign(t1,t2): # aligns paragraphs breaks in text1 with paragraphs break styles in text2 - aka "vertical alignment"
paras1=re.findall(ur"([^\n]{5,15}\n\n[^\n]{5,15})",t1,re.U|re.MULTILINE)
#print len(paras1), paras1
if paras1 is not None:
for para in paras1:
# protect against invalid expression due to imbalanced parenthesis (+?)
para=para.replace(")","\)")
para=para.replace("(","\(")
# other characters ?
para=para.replace("*","\*")
para=para.replace("+","\+")
para=para.replace("?","\?")
para=para.replace("[","\[")
para=para.replace("]","\]")
paranl=re.sub(u"\n\n",u"\n",para,0,re.U|re.MULTILINE)
parasp=re.sub(u"\n\n",u" ",para,0,re.U|re.MULTILINE)
# print parasp
if re.search(u""+para+u"",t2,re.U|re.MULTILINE) is None:
if re.search(u""+paranl+u"",t2,re.U|re.MULTILINE) is not None:
paranl2=paranl.replace("\)",")")
paranl2=paranl2.replace("\(","(")
paranl2=paranl2.replace("\*","*")
paranl2=paranl2.replace("\+","+")
paranl2=paranl2.replace("\?","?")
paranl2=paranl2.replace("\[","[")
paranl2=paranl2.replace("\]","]")
t1=re.sub(u""+para+u"",paranl2,t1)
elif re.search(u""+parasp+u"",t2,re.U|re.MULTILINE) is not None:
parasp2=parasp.replace("\)",")")
parasp2=parasp2.replace("\(","(")
parasp2=parasp2.replace("\*","*")
parasp2=parasp2.replace("\+","+")
parasp2=parasp2.replace("\?","?")
parasp2=parasp2.replace("\[","[")
parasp2=parasp2.replace("\]","]")
t1=re.sub(u""+para+u"",parasp2,t1)
# else : unknown situation
return t1
print "================================================================="
for dirname, dirnames, files in os.walk('.'):
if '.git' in dirnames: dirnames.remove('.git') # don't go into any .git directories.
for dir in sorted(dirnames):
print "\n",dir
if dir in dirdone : continue
else:
dirdone.append(dir)
dirkib,dirtypist=dir.split("-") # caveat : must be the only hyphen in dir name !!!
if dirtypist=="zup" :
print " (skip)","\n"
continue
else:
dirzup=dirkib+"-zup"
dirdone.append(dirzup)
print "- ",dirzup,"\n"
# print path to all filenames.
nfiledif=0 # nb of files with name mismatch
folder_path='./'+dir
for path, dirs, filenames in os.walk(folder_path):
for filename in sorted(filenames):
#print filename
iroot=filename.find("-"+dirtypist+".txt")
fileroot=filename[0:iroot]
filezup=fileroot+"-zup.txt"
print filename+" - "+filezup
file1=open(os.path.join(path, filename), "rb")
try:
file2=open(os.path.join("./"+dirzup, filezup), "rb")
# attention il va falloir traiter (skip) les cas où les noms diffèrent
except:
file1.close()
print " !!! file missing or name different :",filezup
nfiledif=nfiledif+1
continue
# tout1=file1.read()
tout1=u""
line = file1.readline()
nline=0
while line:
nline=nline+1
try :
tout1=tout1+line.decode("utf-8")
except :
print "tout1 character? line:"+str(nline)+" :\n"+line+"\n"
nerr=nerr+1
pass
line = file1.readline()
# handle Windows EOL
tout1=re.sub(u"\r\n",u"\n",tout1,0,re.U|re.MULTILINE)
# tout2=file2.read()
tout2=u""
line = file2.readline()
nline=0
while line:
nline=nline+1
try :
tout2=tout2+line.decode("utf-8")
except :
print "tout2 character? line:"+str(nline)+" :\n"+line+"\n"
nerr=nerr+1
pass
line = file2.readline()
# handle Windows EOL
tout2=re.sub(u"\r\n",u"\n",tout2,0,re.U|re.MULTILINE)
file1.close()
file2.close()
# check for top-titles in zup : poyi, kalankɛnɛ...
toptitle=re.search(ur"[ ]*([kK]alankɛnɛ[ \.]*[^\:\.]*[\:\.][ \n]*|[kK]ɔrɔnfɛla kunnafɔniw[ ]*[\:\.][ \n]*|[sS]ariya[ ]*[\:\.][ \n]*|[lL]awalebaro[ ]*[\:\.][ \n]*|LAWALEBARO[ ]*[\:\.][ \n]*|[nN]siiri[n]*[ ]*[\:\.][ \n]*|[pP]oyi[ ]*[\:\.][ \n]*|[dD]ukɛnɛ [^\:\.]+[\:\.][ \n]*|[mM]aakɔrɔbaro[ ]*[\:\.][ \n]*)<h>",tout2,re.U|re.MULTILINE)
if toptitle is not None:
toptitletext=toptitle.group(1)
toptitletextn=re.sub(ur"\n",u"",toptitletext).strip()
tout2=re.sub(ur" "+toptitletext+ur"<h>",u"\n<h>"+toptitletextn+u" ",tout2,1,re.U|re.MULTILINE)
if toptitletextn not in tout1:
tout1=re.sub(ur"<h>",u"<h>"+toptitletextn+u" ",tout1,1,re.U|re.MULTILINE)
# si poème, raccrocher les lignes commençant par une minuscule à la ligne précédente (sensée commencer par une majuscule)
if "<h>Poyi" in tout1 :
tout1=re.sub(ur"<br/>\n([a-zɛɔɲŋ])",u" \g<1>",tout1,0,re.U|re.MULTILINE)
tout1=re.sub(ur" ",u" ",tout1,0,re.U|re.MULTILINE)
if "<h>Poyi" in tout2 :
tout2=re.sub(ur"<br/>\n([a-zɛɔɲŋ])",u" \g<1>",tout2,0,re.U|re.MULTILINE)
tout2=re.sub(ur" ",u" ",tout2,0,re.U|re.MULTILINE)
file1=open(os.path.join("./"+dir, filename), "wb")
file1.write(tout1)
file1.close
file2=open(os.path.join("./"+dirzup, filezup), "wb")
file2.write(tout2)
file2.close
print " aligned on :", toptitletextn
if tout1==tout2:
print " =égalité="
pathdone="../colldone/"+dirkib
if not os.path.exists(pathdone):
os.mkdir(pathdone)
print " * ",pathdone
filedone=open(os.path.join(pathdone+"/"+fileroot+".txt"),"wb")
filedone.write(tout1)
filedone.close()
print " => ",fileroot+".txt"
os.remove(os.path.join(path, filename))
os.remove(os.path.join("./"+dirzup, filezup))
else:
# check if it's only a question of paragraphs
tout1flat=re.sub("\n\n"," ",tout1,0,re.U|re.MULTILINE)
tout2flat=re.sub("\n\n"," ",tout2,0,re.U|re.MULTILINE)
if tout1flat==tout2flat:
print " =-- égalité --="
# il faudrait peut-être valider la structure \n<h>...<:h>\n ?
pathdone="../colldone/"+dirkib
if not os.path.exists(pathdone):
os.mkdir(pathdone)
print " * ",pathdone
filedone=open(os.path.join(pathdone+"/"+fileroot+".txt"),"wb")
if len(tout1)<len(tout2) :
filedone.write(tout1)
else :
filedone.write(tout2)
filedone.close()
print " =--> ",fileroot+".txt"
os.remove(os.path.join(path, filename))
os.remove(os.path.join("./"+dirzup, filezup))
else:
# align paragraphs as much as possible:
tout1n=tout1
tout2n=tout2
tout1n=valign(tout1n,tout2n)
tout2n=valign(tout2n,tout1n)
if tout1n!=tout1:
file1=open(os.path.join("./"+dir, filename), "wb")
file1.write(tout1n)
file1.close
tout1=tout1n
print " ! ",filename," paragraphs aligned on", filezup
if tout2n!=tout2:
file2=open(os.path.join("./"+dirzup, filezup), "wb")
file2.write(tout2n)
file2.close
tout2=tout2n
print " ! ",filezup," paragraphs aligned on", filename
# for the following checks, we assume tout1 and tout2 are vertically aligned
# check if it's a question of <c>comments</c> in zup which lack tags in doz/gedz
allcomments=re.findall(ur"<c>([^<]+)</c>",tout2,re.U|re.MULTILINE)
tout1n=tout1
for comment in allcomments:
comment=re.sub("é","é",comment) # doz, gez use monoliths
comment=re.sub("è","è",comment)
comment=comment.replace("(","\(")
comment=comment.replace(")","\)")
#print comment
if re.search("<c>"+comment+"</c>",tout1n,re.U|re.MULTILINE) is None:
commentr=comment.replace("\(","(")
commentr=commentr.replace("\)",")")
tout1n=re.sub(comment,"<c>"+commentr+"</c>",tout1n,0,re.U|re.MULTILINE)
tout1flat=re.sub("\n\n"," ",tout1n,0,re.U|re.MULTILINE)
tout2flat=re.sub("\n\n"," ",tout2,0,re.U|re.MULTILINE)
if tout1flat==tout2flat:
print " =c- égalité -c="
pathdone="../colldone/"+dirkib
if not os.path.exists(pathdone):
os.mkdir(pathdone)
print " * ",pathdone
filedone=open(os.path.join(pathdone+"/"+fileroot+".txt"),"wb")
if len(tout1n)<len(tout2) :
filedone.write(tout1)
else :
filedone.write(tout2)
filedone.close()
print " =c-> ",fileroot+".txt"
os.remove(os.path.join(path, filename))
os.remove(os.path.join("./"+dirzup, filezup))
else:
if tout1n!=tout1:
file1=open(os.path.join("./"+dir, filename), "wb")
file1.write(tout1n)
file1.close
print " ! ",filename," <c>comments inserted</c> as in ", filezup
# missing <ill> in zup (zup often ignores ill for Faraban Jalo and others...)
# only one <ill> in file! (this is why it's important to ignore < il : rest of text: other < (<ill>, <h>...))
# match1=re.search(ur"(\n<h>[^<]+</h>\.\n\n)(<ill>[^<]+</ill>\.\n\n)([^¤<]*)$(?![\r\n])",tout1,re.U|re.MULTILINE)
# match2=re.search(ur"(\n<h>[^<]+</h>\.\n\n)(<ill>[^<]+</ill>\.\n\n)([^¤<]*)$(?![\r\n])",tout2,re.U|re.MULTILINE)
# but we may have above title mentions, and pre-title ... Poyi, Maakɔrɔbaro, Dukɛnɛ...
# more general : ([^¤]*\n[^¤]*<h>[^<]+</h>\.\n\n)(<ill>[^<]+</ill>\.\n\n)([^¤<]*)$(?![\r\n])
# and ([^¤]*\n[^¤]*<h>[^<]+</h>\.\n\n)([^¤<]*)$(?![\r\n])
# match1=re.search(ur"(\n+<h>[^<]+</h>\.\n\n)(<ill>[^<]+</ill>\.\n\n)([^¤<]*)$(?![\r\n])",tout1,re.U|re.MULTILINE)
# match2=re.search(ur"(\n+<h>[^<]+</h>\.\n\n)(<ill>[^<]+</ill>\.\n\n)([^¤<]*)$(?![\r\n])",tout2,re.U|re.MULTILINE)
#print match1
match1=re.search(ur"(\n+<h>[^<]+</h>\.\n\n)(<ill>[^<]+</ill>\.\n\n)([^¤]*)$(?![\r\n])",tout1,re.U|re.MULTILINE)
match2=re.search(ur"(\n+<h>[^<]+</h>\.\n\n)(<ill>[^<]+</ill>\.\n\n)([^¤]*)$(?![\r\n])",tout2,re.U|re.MULTILINE)
if match1 is not None:
ills1=re.findall(ur"<ill>[^<]+</ill>",tout1,re.U|re.MULTILINE)
nbills1=len(ills1)
if nbills1==1 and match2 is None:
ills2=re.findall(ur"<ill>[^<]+</ill>",tout2,re.U|re.MULTILINE)
nbills2=len(ills2)
if nbills2==0 :
ill=match1.group(2)
tout2n=re.sub(ur"(\n+<h>[^<]+</h>\.\n\n)([^¤]*)$(?![\r\n])",u"\g<1>"+ill+u"\g<2>",tout2,0,re.U|re.MULTILINE)
if tout2n!=tout2:
file2=open(os.path.join("./"+dirzup, filezup), "wb")
file2.write(tout2n)
file2.close
tout2=tout2n
print " !! ", filezup, "<ill> missing fixed"
tout2flat=re.sub("\n\n"," ",tout2,0,re.U|re.MULTILINE)
if tout1flat==tout2flat:
print " =<< égalité >>="
pathdone="../colldone/"+dirkib
if not os.path.exists(pathdone):
os.mkdir(pathdone)
print " * ",pathdone
filedone=open(os.path.join(pathdone+"/"+fileroot+".txt"),"wb")
if len(tout1)<len(tout2) :
filedone.write(tout1)
else :
filedone.write(tout2)
filedone.close()
print " =++> ",fileroot+".txt"
os.remove(os.path.join(path, filename))
os.remove(os.path.join("./"+dirzup, filezup))
# only an inversion of <ill> ?
# <ill> before signatures
match2=re.search(ur"\n+<h>[^<]+</h>\.\n\n[^¤]*<ill>[^<]+</ill>\.\n\n[^\n]*$(?![\r\n])",tout2,re.U|re.MULTILINE)
ills2=re.findall(ur"<ill>[^<]+</ill>",tout2,re.U|re.MULTILINE)
nbills2=len(ills2)
#print match2
if match2 is not None and nbills2==1:
tout2=re.sub(ur"(\n+<h>[^<]+</h>\.\n\n)([^¤]*)(<ill>[^<]+</ill>\.\n\n)([^\n]*)$(?![\r\n])","\g<1>\g<3>\g<2>\g<4>",tout2,0,re.U|re.MULTILINE)
file2=open(os.path.join("./"+dirzup, filezup), "wb")
file2.write(tout2)
file2.close
print " <ill> aligned to top"
tout2flat=re.sub("\n\n"," ",tout2,0,re.U|re.MULTILINE)
if tout1flat==tout2flat:
print " =++ égalité ++="
pathdone="../colldone/"+dirkib
if not os.path.exists(pathdone):
os.mkdir(pathdone)
print " * ",pathdone
filedone=open(os.path.join(pathdone+"/"+fileroot+".txt"),"wb")
if len(tout1)<len(tout2) :
filedone.write(tout1)
else :
filedone.write(tout2)
filedone.close()
print " =++> ",fileroot+".txt"
os.remove(os.path.join(path, filename))
os.remove(os.path.join("./"+dirzup, filezup))
# <ill> at very end
match2=re.search(ur"\n+<h>[^<]+</h>\.\n\n[^¤]*<ill>[^<]+</ill>$(?![\r\n])",tout2,re.U|re.MULTILINE)
#print match2
if match2 is not None and nbills2==1:
tout2=re.sub(ur"(\n+<h>[^<]+</h>\.\n\n)([^¤]*)(<ill>[^<]+</ill>)$(?![\r\n])","\g<1>\g<3>\n\n\g<2>",tout2,0,re.U|re.MULTILINE)
file2=open(os.path.join("./"+dirzup, filezup), "wb")
file2.write(tout2)
file2.close
print " <ill> aligned to top"
tout2flat=re.sub("\n\n"," ",tout2,0,re.U|re.MULTILINE)
if tout1flat==tout2flat:
print " =++ égalité ++="
pathdone="../colldone/"+dirkib
if not os.path.exists(pathdone):
os.mkdir(pathdone)
print " * ",pathdone
filedone=open(os.path.join(pathdone+"/"+fileroot+".txt"),"wb")
if len(tout1)<len(tout2) :
filedone.write(tout1)
else :
filedone.write(tout2)
filedone.close()
print " =++> ",fileroot+".txt"
os.remove(os.path.join(path, filename))
os.remove(os.path.join("./"+dirzup, filezup))
# elif <ill> at bottom in tout1 ?
if nfiledif>0 : print nfiledif," file names mismatch, please check !"