-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrepertoires.py3
More file actions
249 lines (220 loc) · 8.41 KB
/
Copy pathrepertoires.py3
File metadata and controls
249 lines (220 loc) · 8.41 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import sys
import sys
#from html.parser import HTMLParser
#parser = HTMLParser()
import html
normalizemetas=re.compile(r'<meta content="([^\"]*)" name="([^\"]*)" />',re.U|re.MULTILINE)
ambiguous=re.compile(r'\<span class\=\"w\".*lemma var.*\<\/span\>')
unknown=re.compile(r'<span class="w" stage="-1">[^<]+<span class="lemma">[^<]+</span>')
sentence=re.compile(r'\<span class\=\"sent\"\>([^<]*)\<')
title=re.compile(r'<meta name="text:title" content="([^\"]*)" />',re.U|re.MULTILINE)
author=re.compile(r'<meta name="author:name" content="([^\"]*)" />',re.U|re.MULTILINE)
wordssearch=re.compile(r'<meta name="_auto:words" content="([^\"]*)" />',re.U|re.MULTILINE)
parsedwords=re.compile(r'\<span class\=\"w\"',re.U)
extraittxt=re.compile(r'<body><p>([^£]*)</p></body>',re.U|re.MULTILINE) # ^< ne marche plus ?
searchwords=re.compile(r'([a-zɛɔɲŋA-ZƐƆƝŊ0-9\-́̀̌̂]+)',re.U|re.MULTILINE)
medianame=re.compile(r'^([a-zA-Z\-\_]+)[0-9]*[a-z]*\_',re.U)
medianumber=re.compile(r'^[a-zA-Z\-\_]+([0-9]*[a-z]*)\_',re.U)
date=re.compile(r'<meta name="text:date" content="([^\"]*)" />',re.U|re.MULTILINE)
datesource=re.compile(r'<meta name="source:date" content="([^\"]*)" />',re.U|re.MULTILINE)
textscript=re.compile(r'<meta name="text:script" content="([^\"]*)" />',re.U|re.MULTILINE)
sourceurl=re.compile(r'<meta name="source:url" content="([^\"]*)" />',re.U|re.MULTILINE)
sourcepagetotal=re.compile(r'<meta name="source:pagetotal" content="([^\"]*)" />',re.U|re.MULTILINE)
sourceyear=re.compile(r'<meta name="source:year" content="([^\"]*)" />',re.U|re.MULTILINE)
import os
# log=open("repertoire.log","w")
outf = open ("repertoires.csv","w")
grandtotal=0
nbrep=0
nfiles=0
for dirname, dirnames, filenames in sorted(os.walk('.')):
if '.git' in dirnames: dirnames.remove('.git') # don't go into any .git directories.
# print path to all subdirectories first.
#for subdirname in dirnames:
# print(os.path.join(dirname, subdirname))
if dirname.endswith("/run"): continue # ignore gparser run directory
print(dirname)
#break
#if select=="" : break #sys.exit("no relevant file (.dis.html, .pars.html, .html) in current directory")
# filenames=os.listdir(".")
toutout=""
toutwords=0
toutmedianame=""
toutmedianumber=""
toutdate=""
filenames=sorted(filenames) # peut-être pas nécessaire
htmlfiles=[]
disfiles=[]
selectfiles=[]
# 1st pass
for filename in filenames :
if not filename.endswith(".html"): continue
if filename.endswith(".pars.html"): continue
elif filename.endswith("repl.html"): continue
if filename.endswith(".dis.html"):
disfiles.append(filename[:-9])
else:
htmlfiles.append(filename[:-5])
if len(disfiles)>0:
select=".dis.html"
selectedfiles=disfiles
elif len(htmlfiles)>0:
select=".html"
selectedfiles=htmlfiles
else:
sys.exit("!!! no .html and no .dis.html files")
#print("!!! no .html and no .dis.html files")
# missing disamb ?
nerrfiles=0
for fname in htmlfiles:
if fname not in disfiles:
print(fname,"missing in .dis.html files")
nerrfiles+=1
# disamb file not same name ?
for fname in disfiles:
if fname not in htmlfiles:
print(fname,"missing in .html files")
nerrfiles+=1
if nerrfiles>0:
#sys.exit("!!! file names mismatch")
print("!!! file names mismatch")
nfiles+=len(selectedfiles)
# 2d pass
for filename in selectedfiles :
filename=filename+select
print(os.path.join(dirname, filename))
fileIN = open(os.path.join(dirname, filename), "r")
if toutmedianame=="":
mn=medianame.search(filename)
if mn :
toutmedianame=mn.group(1)
ismedianumber=medianumber.search(filename)
if ismedianumber!=[] :
toutmedianumber=ismedianumber.group(1)
"""
#tout=fileIN.readlines()
line = fileIN.readline()
tout=""
while line:
tout=tout+line # py2: .decode("utf-8")
line = fileIN.readline()
"""
tout=fileIN.read()
fileIN.close()
tout=normalizemetas.sub(r'<meta name="\g<2>" content="\g<1>" />',tout)
if toutdate=="":
toutdatesearch=date.search(tout)
if toutdatesearch :
toutdate=toutdatesearch.group(1)
else:
toutdatesearch=datesource.search(tout)
if toutdatesearch :
toutdate=toutdatesearch.group(1)
else:
toutdate="23.09.2016"
scriptok=False
scriptsearch=textscript.search(tout)
if scriptsearch:
#print("scriptsearch.group(0):", scriptsearch.group(0))
#print("scriptsearch.group(1):'"+scriptsearch.group(1)+"'")
script=scriptsearch.group(1)
if script!="": scriptok=True
scriptpb=""
if not scriptok:
scriptpb="- ATTENTION PAS DE TEXT-SCRIPT"
print(scriptpb)
titlefound=title.search(tout)
titl=""
if titlefound:
titl=titlefound.group(1)
else: print(" no tile")
#print title.search(tout).group(2)
# log.write("\n"+filename+"\ntitle-1: "+titl+"\n")
if titl!="":
titl=re.sub(r"\"","",titl)
titl=re.sub(r"\;",",",titl) # dangereux pour un comma-separated-values
titl=html.unescape(titl)
auth=""
authors=author.search(tout)
if authors :
auth=authors.group(1)
if select==".dis.html" :
word=wordssearch.search(tout).group(1)
words=int(word)
#elif select==".pars.html" :
# words=len(parsedwords.findall(tout))
# word=str(words)
elif select==".html" :
touttxt=tout
touttxt=re.sub(r"<c>.*</c>"," ",touttxt,0,re.U|re.MULTILINE) # enlever les <c>...</c>
touttxt=re.sub(r"<n>.*</n>"," ",touttxt,0,re.U|re.MULTILINE) # enlever les <n>...</n>
touttxt=re.sub(r"<h>|</h>"," ",touttxt,0,re.U|re.MULTILINE) # enlever les <h> ou </h>
touttxt=re.sub(r"<ill>|</ill>"," ",touttxt,0,re.U|re.MULTILINE) # enlever les <ill> ou </ill>
touttxt=re.sub(r"<ls>|</ls>"," ",touttxt,0,re.U|re.MULTILINE) # enlever les <ls> ou </ls>
touttxt=re.sub(r"<br/>"," ",touttxt,0,re.U|re.MULTILINE) # enlever les <br/>
touttxt=re.sub(r"<p>|</p>","",touttxt,0,re.U|re.MULTILINE) # enlever les marques de paragraphes
touttxt=re.sub(r"^ *- ","",touttxt,0,re.U|re.MULTILINE) # enlever les tirets
touttxt=re.sub(r" - ","",touttxt,0,re.U|re.MULTILINE) # enlever les tirets isolés
touttxt=re.sub(r"<head>[^¤]*</head>","",touttxt,0,re.U|re.MULTILINE) # enlever la section header
touttxt=re.sub(r"</*body>|</*html>","",touttxt,0,re.U|re.MULTILINE) # enlever les tags bidy et html
swords=searchwords.findall(touttxt)
# rappel : searchwords=re.compile(ur'([a-zɛɔɲŋA-ZƐƆƝŊ0-9\-́̀̌̂]+)',re.U)
if swords :
words=len(swords)
word=str(words)
#for iword in swords :
# outf.write(": "+iword+"\n")
url=""
pagetotal=""
year=""
if select==".dis.html":
ambs=len(ambiguous.findall(tout))
unkn=len(unknown.findall(tout))
nambigus=""
if ambs!=0 :
nambigus="ambigus: "+str(ambs)+" "
print(" ambigus:",ambs)
ninconnus=""
if unkn!=0 :
ninconnus="inconnus: "+str(unkn)
print(" inconnus:",unkn)
toutout=toutout+titl+"; ; "+filename+"; ; "+word+"; "+auth+"; "+nambigus+ninconnus+scriptpb+"\n"
else:
diffauth=""
nauth_fn=0
fileauth=re.search(r"[0-9\-]*\_[0-9]*([a-z\_]*)\-",filename)
if fileauth :
authshort=fileauth.group(1)
nauth_fn=1
if "_" in authshort:
spauthshort=authshort.split("_")
nauth_fn=len(spauthshort)
nauth_meta=0
if auth!="":
nauth_meta=1
if "|" in auth:
spauth=auth.split("|")
nauth_meta=len(spauth)
if nauth_fn!=nauth_meta :
diffauth=" "+str(nauth_fn)+"/"+str(nauth_meta)+" ?"
result=sourceurl.search(tout)
if result: url=str(result.group(1))
result=sourcepagetotal.search(tout)
if result: pagetotal=str(result.group(1))
result=sourceyear.search(tout)
if result: year=str(result.group(1))
toutout=toutout+titl+"; ; "+filename+"; ; "+word+"; "+auth+"; "+year+"; "+pagetotal+"; "+url+"; "+diffauth+scriptpb+"\n"
toutwords=toutwords+words
if toutout!="" :
toutout=toutmedianame+" "+toutmedianumber+" "+toutdate+"; ; ; ; "+str(toutwords)+"; ;\n" +toutout+"\n\n"
outf.write(toutout)
grandtotal=grandtotal+toutwords
nbrep=nbrep+1
outf.write("TOTAL for "+str(nbrep)+" issues ; ; ; ; "+str(grandtotal)+"; ;\n")
outf.close()
print("\n",nfiles," files")
print("TOTAL for "+str(nbrep)+" issue(s) "+str(grandtotal)+" words\n")