-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
498 lines (442 loc) · 18.3 KB
/
preprocess.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
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("infolder", help="Folder containing TXT files")
parser.add_argument("outfolder", help="Output folder")
parser.add_argument("--list", help="TSV containing list of files")
parser.add_argument("--tint", help="Tint URL", default="https://dh.fbk.eu/tint-demo-api/tint/")
parser.add_argument("--conn", help="List of connectives")
parser.add_argument("--skiplist", help="List of file to skip")
parser.add_argument("--cachefolder", help="Folder for Tint cache")
parser.add_argument('--overwrite', action='store_true')
parser.add_argument('--annotators', help="List of annotators' names", nargs="+")
parser.add_argument('--outindex', help="Output index")
args = parser.parse_args()
import os
import random
from pathlib import Path
import requests
import json
import csv
from webanno_tsv import Document, Annotation
from dataclasses import replace
from tqdm import tqdm
from unidecode import unidecode
import re
def isSubArray(A, B):
i = 0
j = 0
n = len(A)
m = len(B)
ret = []
while i < n and j < m:
if A[i] == B[j]:
i += 1
j += 1
if j == m:
# index is +1 just not to confuse 0 and False
ret.append(i - j + 1)
i = i - j + m
j = 0
# return i - j + 1
else:
i = i - j + 1
j = 0
return ret
# https://www.geeksforgeeks.org/python-intersection-two-lists/
def intersection(lst1, lst2):
temp = set(lst2)
lst3 = [value for value in lst1 if value in temp]
return lst3
overwrite = args.overwrite
outindexFile = args.outindex
toDo = {}
fileListInput = args.list
if fileListInput:
with open(fileListInput) as f:
for line in f:
line = line.strip()
parts = line.split("\t")
if len(parts) <= 2:
continue
fileName = parts[1].strip()
if fileName in toDo:
print("File already exists: ", fileName)
continue
if parts[2].strip().lower() == "yes":
toDo[fileName] = True
if parts[2].strip().lower() == "no":
toDo[fileName] = False
skipListInput = args.skiplist
if skipListInput:
with open(skipListInput) as f:
for line in f:
fileName = line.strip()
if fileName not in toDo:
print("Cannot skip: ", fileName)
continue
del toDo[fileName]
annotators = args.annotators
if annotators:
annotators = [re.sub(r"[^A-Z]", "", unidecode(x.upper())) for x in annotators]
# print(len(toDo))
# print(annotators)
# exit()
"""
Documentation for connectives file:
0: token
1: X if the token has another meaning without accent(s)
2: list of POS (negative with ~)
3: list of expressions to skip
"""
# not used right now, it can be added using POS information
connectivesToCheck = set()
checkPos = {}
connectives = set()
checkPosNeg = {}
ignorePhrases = {}
if args.conn:
with open(args.conn) as file:
tsv_file = csv.reader(file, delimiter="\t")
for line in tsv_file:
t = line[0].lower()
connectives.add(t)
if line[3]:
phrases = line[3].split(",")
ignorePhrases[t] = set()
for phrase in phrases:
ignorePhrases[t].add(phrase.lower())
if line[2]:
parts = line[2].split(",")
pos = set()
neg = set()
for part in parts:
part = part.strip()
if part.startswith("~"):
part = part[1:]
if len(part):
neg.add(part)
else:
if len(part):
pos.add(part)
if len(pos) > 0 and len(neg) > 0:
print("Error: pos and neg in the same line")
else:
if len(pos) > 0:
checkPos[t] = pos
if len(neg) > 0:
checkPosNeg[t] = neg
if line[1]:
connectivesToCheck.add(unidecode(t))
else:
connectives.add(unidecode(t))
# print(checkPos)
# print(checkPosNeg)
# print(ignorePhrases)
# exit()
layer_defs = [
('webanno.custom.StrutturaCapoverso', ['Capoverso']),
('webanno.custom.Connettivoproblematico', ['Problematico']),
('webanno.custom.SegmentazioneVirgoladitroppo', ['Virgoladitroppo']),
('webanno.custom.Segmentazione', ['Virgolasplice']),
('webanno.custom.ConnettivoproblematicoTint', ['Tint'])
]
# annotations = [
# Annotation(tokens=doc.tokens[1:2], layer='Layer1', field='Field1', label='ABC'),
# Annotation(tokens=doc.tokens[1:3], layer='Layer2', field='Field3', label='XYZ', label_id=1)
# ]
# sentence = "Il cane ha sia sia sia sia bevuto"
# sp = sentence.split(" ")
# print(isSubArray(sp, ['sia']))
# exit()
input_folder = args.infolder
output_folder = args.outfolder
cache_folder = args.cachefolder
if not os.path.exists(output_folder):
os.makedirs(output_folder)
if cache_folder and not os.path.exists(cache_folder):
os.makedirs(cache_folder)
numFiles = 0
for file in Path(input_folder).rglob('*.txt'):
numFiles += 1
count = 0
skipped = 0
outputIndex = {}
pbar = tqdm(Path(input_folder).rglob('*.txt'), total=numFiles)
for path in pbar:
# for path in Path(input_folder).rglob('*.txt'):
pbar.set_description(path.name)
filename = os.path.join(path.parent, path.name)
# outfile = filename + ".tsv"
# print("Input:", filename)
outName = path.name
if len(toDo) > 0:
if outName not in toDo:
# print("File not in to do list, skipping:", outName)
skipped += 1
continue
prefix = "SI_"
if not toDo[outName]:
outputIndex[outName] = "NO"
prefix = "ZNO_"
else:
if annotators and len(annotators) > 0:
annotatorName = annotators[count % len(annotators)]
outputIndex[outName] = annotatorName
prefix = prefix + annotatorName + "_"
toDo.pop(outName)
random.seed(outName)
r = random.randint(0, 999)
prefix += f'{r:03}' + "_"
outName = prefix + outName
count += 1
outfile = os.path.join(output_folder, outName + ".tsv")
if os.path.exists(outfile) and not overwrite:
# print("File exists, skipping")
continue
with open(filename, 'r') as file:
text = file.read()
if cache_folder and os.path.exists(os.path.join(cache_folder, path.name)):
with open(os.path.join(cache_folder, path.name), "r") as jf:
data = json.load(jf)
else:
myobj = {'text' : text}
# print(" Calling Tint...")
x = requests.post(args.tint, data = myobj)
data = json.loads(x.text)
if cache_folder:
with open(os.path.join(cache_folder, path.name), 'w') as fw:
fw.write(x.text)
# print(" Extracting information...")
sentences = []
rawAnnotations = []
thisIndex = 0
for sentence in data['sentences']:
thisSentence = []
thisSentenceIndex = 0
thisSentenceLower = []
thisSentencePos = []
sentenceStartIndex = thisIndex
# capoverso
b = sentence['characterOffsetBegin']
# print("Text:", sentence['text'])
capoverso = False
if b == 0:
capoverso = True
for i in reversed(range(b)):
t = text[i:i+1]
if t.strip() != "":
break
if t == '\n':
capoverso = True
break
# e = sentence['characterOffsetEnd']
# sentenceText = sentence['text']
# print(sentenceText, text[b:e])
if capoverso:
rawAnnotations.append({
"tokenStart": sentenceStartIndex,
"tokenEnd": sentenceStartIndex + 1,
"layer": 'webanno.custom.StrutturaCapoverso',
"field": 'Capoverso',
"label": 'true'
})
# tokens
tokensMap = {}
for token in sentence['tokens']:
tokensMap[token['index']] = thisSentenceIndex
if token['isMultiwordToken'] and not token['isMultiwordFirstToken']:
continue
thisSentence.append(token['originalText'])
thisSentenceIndex += 1
thisSentenceLower.append(token['originalText'].lower())
thisSentencePos.append(token['pos'].upper())
thisIndex += 1
# commas
for i in range(len(thisSentence)):
if thisSentence[i] == ",":
rawAnnotations.append({
"tokenStart": sentenceStartIndex + i,
"tokenEnd": sentenceStartIndex + i + 1,
"layer": 'webanno.custom.Segmentazione',
"field": 'Virgolasplice',
"label": 'Corretto'
})
# dependencies
deps = sentence['basic-dependencies']
coord_connections = {}
for dep in deps:
# cc to names
if dep['dep'] == "cc":
coord_connections[sentenceStartIndex + tokensMap[dep['dependent']]] = sentenceStartIndex + tokensMap[dep['governor']]
# wrong commas
if dep['dep'] == "nsubj":
tokenStart = min(dep['governor'], dep['dependent'])
tokenEnd = max(dep['governor'], dep['dependent'])
tokenStart = tokensMap[tokenStart]
tokenEnd = tokensMap[tokenEnd]
commaCount = set()
for i in range(tokenEnd - tokenStart + 1):
if thisSentence[i + tokenStart] == ",":
commaCount.add(i + tokenStart)
if len(commaCount) == 1:
for i in commaCount:
rawAnnotations.append({
"tokenStart": sentenceStartIndex + i,
"tokenEnd": sentenceStartIndex + i + 1,
"layer": 'webanno.custom.SegmentazioneVirgoladitroppo',
"field": 'Virgoladitroppo',
"label": 'true'
})
# check connectives
connAnnotations = {}
connTintAnnotations = {}
connOccupied = {}
for connective in connectives:
parts = connective.split(" ")
foundList = isSubArray(thisSentenceLower, parts)
for found in foundList:
found -= 1
tokenStart = thisIndex - len(thisSentenceLower) + found
tokenEnd = tokenStart + len(parts)
if tokenStart in coord_connections:
if thisSentencePos[coord_connections[tokenStart] - thisIndex].startswith("S"):
connTintAnnotations[tokenStart] = {
"tokenStart": tokenStart,
"tokenEnd": tokenEnd,
"layer": 'webanno.custom.ConnettivoproblematicoTint',
"field": 'Tint',
"label": 'coord-name'
}
continue
# check phrases
skipThis = False
if connective in ignorePhrases:
thisConnectiveTokens = []
for i in range(tokenEnd - tokenStart):
thisConnectiveTokens.append(found + 1 + i)
for phrase in ignorePhrases[connective]:
phraseParts = phrase.split(" ")
sets = isSubArray(thisSentenceLower, phraseParts)
for s in sets:
thisPhraseTokens = []
for i in range(len(phraseParts)):
thisPhraseTokens.append(s + i)
# print(thisPhraseTokens)
# print(thisConnectiveTokens)
# print(intersection(thisPhraseTokens, thisConnectiveTokens))
inters = intersection(thisPhraseTokens, thisConnectiveTokens)
if len(inters) > 0:
skipThis = True
if skipThis:
connTintAnnotations[tokenStart] = {
"tokenStart": tokenStart,
"tokenEnd": tokenEnd,
"layer": 'webanno.custom.ConnettivoproblematicoTint',
"field": 'Tint',
"label": 'in-phrase'
}
continue
# check pos
if connective in checkPos:
pass # to do
if connective in checkPosNeg:
present = False
for i in range(len(parts)):
index = tokenStart - thisIndex + i
if thisSentencePos[index] in checkPosNeg[connective]:
present = True
if present:
connTintAnnotations[tokenStart] = {
"tokenStart": tokenStart,
"tokenEnd": tokenEnd,
"layer": 'webanno.custom.ConnettivoproblematicoTint',
"field": 'Tint',
"label": 'pos-not'
}
continue
# remove duplicates
length = tokenEnd - tokenStart
maxLength = 0
totalList = {}
for i in range(length):
index = tokenStart + i
if index in connAnnotations:
totalList[index] = connAnnotations[index]['tokenEnd'] - connAnnotations[index]['tokenStart']
if index in connOccupied:
newIndex = connOccupied[index]
totalList[newIndex] = connAnnotations[newIndex]['tokenEnd'] - connAnnotations[newIndex]['tokenStart']
for i in totalList:
if totalList[i] > maxLength:
maxLength = totalList[i]
if maxLength > 0:
if length > maxLength:
indexToRemove = set()
for index in totalList:
for i in range(totalList[index]):
indexToRemove.add(index + i)
indexToRemove = sorted(indexToRemove, reverse=True)
for i in indexToRemove:
if i in connOccupied:
connOccupied.pop(i)
if i in connAnnotations:
ts = connAnnotations[i]["tokenStart"]
te = connAnnotations[i]["tokenEnd"]
connTintAnnotations[i] = {
"tokenStart": ts,
"tokenEnd": te,
"layer": 'webanno.custom.ConnettivoproblematicoTint',
"field": 'Tint',
"label": 'overlap'
}
connAnnotations.pop(i)
else:
connTintAnnotations[tokenStart] = {
"tokenStart": tokenStart,
"tokenEnd": tokenEnd,
"layer": 'webanno.custom.ConnettivoproblematicoTint',
"field": 'Tint',
"label": 'overlap'
}
continue
for i in range(len(parts)):
connOccupied[tokenStart + i] = tokenStart
connAnnotations[tokenStart] = {
"tokenStart": tokenStart,
"tokenEnd": tokenEnd,
"layer": 'webanno.custom.Connettivoproblematico',
"field": 'Problematico',
"label": 'false'
}
# print(thisSentence)
# print(parts)
# print(found, len(parts))
for i in connAnnotations:
rawAnnotations.append(connAnnotations[i])
for i in connTintAnnotations:
rawAnnotations.append(connTintAnnotations[i])
sentences.append(thisSentence)
doc = Document.from_token_lists(sentences)
annotations = []
for rawAnnotation in rawAnnotations:
annotations.append(Annotation(
tokens=doc.tokens[rawAnnotation['tokenStart']:rawAnnotation['tokenEnd']],
layer=rawAnnotation['layer'],
field=rawAnnotation['field'],
label=rawAnnotation['label']))
doc = replace(doc, annotations=annotations, layer_defs=layer_defs)
# print(" Saving file...")
fout = open(outfile, "w")
fout.write(doc.tsv())
fout.close()
# print(doc.tsv())
if outindexFile:
with open(outindexFile, "w") as fw:
for i in outputIndex:
fw.write(i)
fw.write("\t")
fw.write(outputIndex[i])
fw.write("\n")
print("Total parsed files:", count)
print("Skipped files:", skipped)
# for file in toDo:
# print("Remaining:", file)