-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
293 lines (233 loc) · 8.92 KB
/
utils.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
import PyPDF2
import docx
import ntpath
import wget
import os
import shutil
import numpy as np
import re
import fitz
def path_leaf(path : str):
"""
Returns the name of a file given its path
https://stackoverflow.com/questions/8384737/extract-file-name-from-path-no-matter-what-the-os-path-format
"""
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
def is_supported(file_name : str):
"""Verifies if a file is supported by the application"""
supported_extension = [".txt", ".md", ".pdf", ".doc", ".docx"]
_, extension = os.path.splitext(file_name)
if extension in supported_extension :
return True
else :
return False
def text_is_valid(text : str):
"""Verifies if a clause is treatable/scalable"""
return text.strip().rstrip().replace("\n", "").replace("\r", "").replace("\t", "") # != ""
def can_be_subsubtitle1(e : str) :
regex_subsubtitle = '^\w+\.|\(?\w+\)'
regex_subsubtitle_no_romain = '^\w{,1}\.|\(?\w{,1}\) '
romain_set = {"i", "v", "x", ".", ")", "("}
e_strip = e.strip()
m = re.match(regex_subsubtitle, e_strip)
if m :
if set(e).issubset(romain_set) :
return "romain"
else :
if len(e) <= 3 and re.match(regex_subsubtitle_no_romain, e_strip) :
if ('.' in e_strip or e_strip[1] == ')') and len(e_strip) == 2 :
return "non_romain"
def extract_subsubtitle1(dico) :
dico_temp = {}
"""
for k, v in dico.copy().items():
current = len(v)
for index, e in enumerate(reversed(v)) :
if can_be_subsubtitle1(e) :
dico_temp[k + '-'+ e] = v[index:current]
del dico[k][index : current]
current = index
dico.update(dico_temp)
"""
return dico
def convert_to_clauses(content : list):
regex_title = '[0-9]+\.\w?'
regex_subtitle = '[0-9]+'
fake_string = '__fake__'
dico = {}
current = '0.'
dico[current] = []
a = " ".join(content)
b = a.split("\n \n")
for c in b :
if text_is_valid(text = c) :
if re.search(regex_title, c) :
v = c.split(".")
k = v[0]
v = v[1]
m = re.match(regex_subtitle, v.strip())
if m :
span = m.span()
current = k + '.' + v.strip()[span[0]:span[1]]
v = v[span[1]:]
else :
current = k + '.'
if current in dico.keys():
i = 1
while k+fake_string+str(i) in dico.keys():
i += 1
current = k+fake_string+str(i)
try :
dico[current].append(v)
except KeyError :
dico[current] = [v]
else:
dico[current].append(c)
dico = extract_subsubtitle1(dico)
dico2 = {}
for k, v in dico.items():
dico2[k] = ' '.join(v).replace("\n", "")
return dico2
import re
import fitz
def text_is_valid(text : str):
return text.strip().rstrip().replace("\n", "").replace("\r", "").replace("\t", "") # != ""
def can_be_subtitle_or_subsubtitle(e : str) :
regex_subsubtitle = '^\w+\.\w?|^\(?\w+\)\w?'
regex_subsubtitle_no_romain = '^\w{,1}\.\w?|^\(?\w{,1}\)\w?'
romain_set = {"i", "v", "x", ".", ")", "("}
e_strip = e.replace("'", "")
#print("e_strip", e_strip)
m = re.match(regex_subsubtitle, e_strip)
if m :
v = e_strip.split(" \n")
id = v[0]
if set(id).issubset(romain_set) :
return "romain"
else :
if len(id) <= 3 and re.match(regex_subsubtitle_no_romain, id) :
if ('.' in id or id[1] == ')') and len(id) == 2 :
return "no_romain"
def can_be_title(e : str):
regex_title = '[0-9]+\. \\n\w?'
if re.search(regex_title, e) :
return " \n"
else :
regex_title = '[0-9]+\.\w?'
return " "
def pyMuPDF_clauses_extraction(document):
doc = fitz.open(document)
fake_string = '__fake__'
current = '0.'
current_title = current
current_subtitle = ""
current_subsubtitle = ""
dico = {}
dico[current] = []
for page in doc :
blocks = page.getText("blocks")
for line in blocks :
line = line[4]
if text_is_valid(line) :
a = can_be_title(line)
b = can_be_subtitle_or_subsubtitle(line)
if a or b :
#print("==0", line)
v = line.split(a)
id = v[0]
text = a.join(v[1:])
current = id
"""
if a :
current_title = id
current = id
if b == "no_romain" :
current_subtitle = id
current = (current_title +"." if current_title else "") + id
if b == "romain" :
current_subsubtitle = id
current = (current_title +"." if current_title else "")+ (current_subtitle +"." if current_subtitle else "")+ id
"""
if current in dico.keys():
i = 1
while id+fake_string+str(i) in dico.keys():
i += 1
current = id+fake_string+str(i)
try :
dico[current].append(text)
#print(current, current_title, "2 === ",line)
except :
dico[current] = [text]
#print(current, current_title, "1 === ",line)
else :
#print(repr(line))
dico[current].append(line)
#print(current, current_title, "3 === ",line)
for k , v in dico.copy().items():
dico[k] = "\n".join(v)
return dico
def get_content(document):
"""reads and separates a document file (pdf, docx, doc, txt, md) into a list of clauses"""
_, extension = os.path.splitext(document)
clauses = None
join = " "
if extension == ".pdf" :
"""
pdfReader = PyPDF2.PdfFileReader(open(document, "rb"))
content = [pdfReader.getPage(page).extractText() for page in range(pdfReader.numPages)]
content = [text for text in content if text_is_valid(text = text)]
"""
clauses = pyMuPDF_clauses_extraction(document)
content = list(clauses.values())
elif extension in [".doc", ".docx"] :
doc = docx.Document(open(document, "rb"))
content = doc.paragraphs
content = [para.text for para in content if text_is_valid(text = para.text)]
clauses = {i : clause for i, clause in enumerate(content)}
else :
content = open(document, "r").read()
content = content if text_is_valid(text = content) else ""
join = "\n\n"
content = content.split(join)
clauses = {i : clause for i, clause in enumerate(content)}
return content, extension, clauses, join
def get_ktrain_predict_method(ktrain_predictor):
"""Returns the prediction method from a ktrain predictor"""
def predict_method(eula):
if type(eula) == str :
eula = [eula]
else :
assert type(eula) == list
predictor = ktrain_predictor.predict_proba
output = []
for text in eula :
y = predictor(text)
if type(y) == np.ndarray :
#output.append({"acceptability" : float(y[0]), "unacceptability" : float(y[1])})
output.append(y)
else :
#output.append({"acceptability" : int(1-y), "unacceptability" : int(y)})
output.append([1-y, y])
return output
return predict_method
def download(output_path, to_load, base_url = "", free_after_download_and_load = False):
cache_path = output_path
if not os.path.isdir(cache_path):
os.mkdir(cache_path)
for model_name, files in to_load.items():
model_path = os.path.join(cache_path, model_name)
if not os.path.isdir(model_path):
os.mkdir(model_path)
for file_name in files :
file_path = os.path.join(model_path, file_name)
file_url = os.path.join(base_url, model_name, file_name).replace("\\", "/")
if not os.path.isfile(file_path):
wget.download(
file_url, file_path
)
if free_after_download_and_load :
try:
shutil.rmtree(model_path)
except OSError:
pass