-
Notifications
You must be signed in to change notification settings - Fork 11
/
rehearsal_revised.py
473 lines (420 loc) · 15.8 KB
/
rehearsal_revised.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
from tqdm import tqdm
import random
import plac
import os
import re
import json
import pyarabic.araby as araby
from prodigy import set_hashes
from prodigy.components.db import connect
#update the labels in LDC to our standard used in prodigy labeled data.
from tqdm import tqdm
import random
import plac
import os
import re
import json
from prodigy import set_hashes
from prodigy.components.db import connect
#update the labels in LDC to our standard used in prodigy labeled data.
def update_ldc_labels(data):
newdata=[]
for d in data:
#well there 4122 records with empty span
if(len(d["spans"])==0):
continue;
newdata.append(d)
label=d["spans"][0]["label"]
if(label=='PERSON" S_OFF="1'):
d["spans"][0]["label"]="PERSON"
elif(label=="PERSON"):
continue;
elif(label== 'GPE" S_OFF="1'):
d["spans"][0]["label"]="GPE"
elif(label=='ORG" E_OFF="1'):
d["spans"][0]["label"]="ORG"
elif(label=='ORG" S_OFF="1'):
d["spans"][0]["label"]="ORG"
elif(label=='ORG'):
continue;
elif(label=="GPE"):
continue;
elif(label=="FAC" or label=="LOC"):
d["spans"][0]["label"]="GPE"
else:
d["spans"][0]["label"]="MISC"
return newdata;
#validate the labels are what we are looking for
def getlabelset(data):
labelset={}
for d in data:
if(len(d["spans"])>0):
la=d["spans"][0]["label"]
else:
continue
if(la in labelset):
labelset[la]=labelset[la]+1
else:
labelset[la]=1
print(labelset)
return labelset
def get_root_filename(onto_dir):
name_files = []
for dirpath, subdirs, files in os.walk(onto_dir):
for fname in files:
if bool(re.search(".name", fname)):
fn = os.path.join(dirpath, fname)
fn = re.sub("\.name", "", fn)
name_files.append(fn)
return name_files
def split_sentence(text):
text = text.strip().split('\n')[1:-1]
return text
def split_doc(text):
text_list = text.strip().split('</DOC>\s<DOC')
ids = [re.findall('<DOC DOCNO="(.+?)">', t)[0] for t in text_list]
text_list = [re.sub('<DOC DOCNO=".+?">', "", t).strip() for t in text_list]
return ids, text_list
def clean_ent(ent):
tag = re.findall('TYPE="(.+?)">', ent)[0]
text = re.findall('>(.+)', ent)[0]
text = re.sub("\$", "\$", text)
text = text.replace('{' , 'ا')
text = text.replace('`' , '')
text = text.replace('-RRB-' , ')')
text = text.replace('-LRB-' , '(')
text = text.replace('-' , '')
text = araby.strip_tashkeel(text)
return (text, tag)
def raw_text(text):
"""Remove entity tags"""
text = re.sub("<ENAMEX .+?>", "", text)
text = re.sub("</ENAMEX>", "", text)
text = text.replace('{' , 'ا')
text = text.replace('`' , '')
text = text.replace('-RRB-' , ')')
text = text.replace('-LRB-' , '(')
text = text.replace('-','')
text = araby.strip_tashkeel(text)
return text
def ent_position(ents, text):
spacy_ents = []
for ent in ents:
ma = re.search(ent[0], text)
ent_tup = (ma.start(), ma.end(), ent[1])
spacy_ents.append(ent_tup)
return spacy_ents
def text_to_spacy(markup):
ents = re.findall("<ENAMEX(.+?)</ENAMEX>", markup)
ents = [clean_ent(ent) for ent in ents]
text = raw_text(markup)
spacy_ents = ent_position(ents, text)
final = (text, {"entities" : spacy_ents})
return final
def onf_to_raw(onf_file):
"""
Take in a path to a .onf Ontonotes file. Return the raw text (as much as possible).
The quotes are usually quite messed up, so this is not going to look like real input text.
"""
with open(onf_file, "r") as f:
onf = f.read()
sentences = re.findall("Plain sentence\:\n\-+?\n(.+?)Treebanked sentence", onf, re.DOTALL)
sentences = [re.sub("\n+?\s*", " ", i).strip() for i in sentences]
paragraph = ' '.join(sentences)
paragraph = paragraph.replace('-RRB-' , ')')
paragraph = paragraph.replace('-LRB-' , '(')
return paragraph
def sent_with_offsets(ner_filename):
"""
Take a .name file and return a sentence list of the kind described here:
https://github.com/explosion/spacy/blob/master/examples/training/training-data.json
"""
with open(ner_filename, "r") as f:
doc = f.read()
sentences = []
onto_sents = split_sentence(doc)
for sent in onto_sents:
offsets = text_to_spacy(sent)
sentences.append(offsets)
return sentences
def dir_to_raw(onto_dir):
fns = get_root_filename(onto_dir)
all_annotations = []
for fn in tqdm(fns):
ner_filename = fn + ".name"
onf_filename = fn + ".onf"
try:
raw = onf_to_raw(onf_filename)
sentences = sent_with_offsets(ner_filename)
final = {"id" : "fake",
"paragraphs" : [
{"raw" : raw,
"sentences" : sentences}]}
all_annotations.append(final)
except Exception as e:
print("Error formatting ", fn, e)
return all_annotations
def onto_to_prodigy_complete(sent):
"""
Make an accepted Prodigy task with all NER spans.
"""
spans = []
for s in sent[1]['entities']:
s = {"start" : s[0], "end" : s[1], "label" : s[2]}
spans.append(s)
prod = {"answer" : "accept",
"text" : sent[0],
"spans" : spans,
"source" : "OntoNotes_rehearsal"}
return prod
@plac.annotations(
dataset=("Name of dataset with Prodigy annotated NER.", "positional", None, str),
multiplier=("Number of OntoNotes annotation to add per newly collected annotation (5?).","positional", None, int),
split=("Should 20 percent of annotated and Onto data be pulled off for eval?", "flag", "s"),
onto_dir=("Location of OntoNotes directory", "positional", None, str))
def main(dataset, multiplier, split, onto_dir="/home/yan/ontonotes-release-5.0/data/files/data/arabic/annotations"):
"""
Mix in OntoNotes NER annotations with new NER annotations from Prodigy to avoid the catatrophic forgetting problem.
Given a Prodigy dataset with new NER annotations, create a new dataset ('augmented_for_training') that also
includes OntoNotes NER sentences mixed in. `prodigy ner.batch-train` can then be called on this dataset
to learn on the new annotations without forgetting the old.
See here for more information on the catastrophic forgetting problem: https://explosion.ai/blog/pseudo-rehearsal-catastrophic-forgetting
"""
print("Reading OntoNotes")
raw_annotations = dir_to_raw(onto_dir)
print("Converting to spaCy spans...")
all_onto = []
for i in raw_annotations:
for s in i['paragraphs'][0]['sentences']:
all_onto.append(onto_to_prodigy_complete(s))
all_onto = list(set_hashes(eg) for eg in all_onto)
print("length all_onto")
print(len(all_onto))
random.shuffle(all_onto)
# get Prodigy annotations
db = connect()
annot = db.get_dataset(dataset)
random.shuffle(annot)
print("Found {0} annotations in {1}".format(len(annot), dataset))
# Get the examples to augment
aug_num = multiplier * len(annot)
print("Augmenting existing examples with {0} OntoNotes sentences".format(aug_num))
#with open("allonto.json","w") as outfile:
# json.dump(all_onto,outfile)
augment = all_onto[0:aug_num]
update_ldc_labels(augment)
print("label set for augmented data:");
getlabelset(augment)
if split:
cutpoint = round(len(annot) * 0.2)
eval_prod = annot[0:cutpoint]
annot = annot[cutpoint:]
eval_onto = all_onto[aug_num:aug_num + 5*cutpoint]
#both = augment + annot
both=augment+all_onto
#both=all_onto
print("length for both")
print(len(both))
random.shuffle(both)
# use a hardcoded rehearsal dataset because we're dropping and don't want
# to take user input here. If it exists, drop it so we can refresh it.
datasetname="augmented_for_training"
exs = db.get_dataset(datasetname)
if exs:
db.drop_dataset(datasetname)
db.add_examples(both, [datasetname])
print("Wrote examples to the Prodigy dataset "+datasetname+" . Use 'ner.batch-train' on that dataset.")
if split:
eo = db.get_dataset("onto_for_eval")
if eo:
db.drop_dataset("onto_for_eval")
ep = db.get_dataset("prodigy_for_eval")
if ep:
db.drop_dataset("prodigy_for_eval")
db.add_examples(eval_onto, ["onto_for_eval"])
db.add_examples(eval_prod, ["prodigy_for_eval"])
print("Wrote eval examples to `prodigy_for_eval` and `onto_for_eval`")
if __name__ == "__main__":
plac.call(main)
#validate the labels are what we are looking for
def getlabelset(data):
labelset={}
for d in data:
if(len(d["spans"])>0):
la=d["spans"][0]["label"]
else:
continue
if(la in labelset):
labelset[la]=labelset[la]+1
else:
labelset[la]=1
return labelset
def get_root_filename(onto_dir):
name_files = []
for dirpath, subdirs, files in os.walk(onto_dir):
for fname in files:
if bool(re.search(".name", fname)):
fn = os.path.join(dirpath, fname)
fn = re.sub("\.name", "", fn)
name_files.append(fn)
return name_files
def split_sentence(text):
text = text.strip().split('\n')[1:-1]
return text
def split_doc(text):
text_list = text.strip().split('</DOC>\s<DOC')
ids = [re.findall('<DOC DOCNO="(.+?)">', t)[0] for t in text_list]
text_list = [re.sub('<DOC DOCNO=".+?">', "", t).strip() for t in text_list]
return ids, text_list
def clean_ent(ent):
tag = re.findall('TYPE="(.+?)">', ent)[0]
text = re.findall('>(.+)', ent)[0]
text = re.sub("\$", "\$", text)
return (text, tag)
def raw_text(text):
"""Remove entity tags"""
text = re.sub("<ENAMEX .+?>", "", text)
text = re.sub("</ENAMEX>", "", text)
return text
def ent_position(ents, text):
spacy_ents = []
for ent in ents:
ma = re.search(ent[0], text)
ent_tup = (ma.start(), ma.end(), ent[1])
spacy_ents.append(ent_tup)
return spacy_ents
def text_to_spacy(markup):
ents = re.findall("<ENAMEX(.+?)</ENAMEX>", markup)
ents = [clean_ent(ent) for ent in ents]
text = raw_text(markup)
spacy_ents = ent_position(ents, text)
final = (text, {"entities" : spacy_ents})
return final
def onf_to_raw(onf_file):
"""
Take in a path to a .onf Ontonotes file. Return the raw text (as much as possible).
The quotes are usually quite messed up, so this is not going to look like real input text.
"""
with open(onf_file, "r") as f:
onf = f.read()
sentences = re.findall("Plain sentence\:\n\-+?\n(.+?)Treebanked sentence", onf, re.DOTALL)
sentences = [re.sub("\n+?\s*", " ", i).strip() for i in sentences]
paragraph = ' '.join(sentences)
return paragraph
def sent_with_offsets(ner_filename):
"""
Take a .name file and return a sentence list of the kind described here:
https://github.com/explosion/spacy/blob/master/examples/training/training-data.json
"""
with open(ner_filename, "r") as f:
doc = f.read()
sentences = []
onto_sents = split_sentence(doc)
for sent in onto_sents:
offsets = text_to_spacy(sent)
sentences.append(offsets)
return sentences
def dir_to_raw(onto_dir):
fns = get_root_filename(onto_dir)
all_annotations = []
for fn in tqdm(fns):
ner_filename = fn + ".name"
onf_filename = fn + ".onf"
try:
raw = onf_to_raw(onf_filename)
sentences = sent_with_offsets(ner_filename)
final = {"id" : "fake",
"paragraphs" : [
{"raw" : raw,
"sentences" : sentences}]}
all_annotations.append(final)
except Exception as e:
print("Error formatting ", fn, e)
return all_annotations
def onto_to_prodigy_complete(sent):
"""
Make an accepted Prodigy task with all NER spans.
"""
spans = []
for s in sent[1]['entities']:
s = {"start" : s[0], "end" : s[1], "label" : s[2]}
spans.append(s)
prod = {"answer" : "accept",
"text" : sent[0],
"spans" : spans,
"source" : "OntoNotes_rehearsal"}
return prod
@plac.annotations(
dataset=("Name of dataset with Prodigy annotated NER.", "positional", None, str),
multiplier=("Number of OntoNotes annotation to add per newly collected annotation (5?).","positional", None, int),
split=("Should 20 percent of annotated and Onto data be pulled off for eval?", "flag", "s"),
onto_dir=("Location of OntoNotes directory", "positional", None, str))
def main(dataset, multiplier, split, onto_dir="/home/yan/ontonotes-release-5.0/data/files/data/arabic/annotations"):
"""
Mix in OntoNotes NER annotations with new NER annotations from Prodigy to avoid the catatrophic forgetting problem.
Given a Prodigy dataset with new NER annotations, create a new dataset ('augmented_for_training') that also
includes OntoNotes NER sentences mixed in. `prodigy ner.batch-train` can then be called on this dataset
to learn on the new annotations without forgetting the old.
See here for more information on the catastrophic forgetting problem: https://explosion.ai/blog/pseudo-rehearsal-catastrophic-forgetting
"""
print("Reading OntoNotes")
raw_annotations = dir_to_raw(onto_dir)
print("Converting to spaCy spans...")
all_onto = []
for i in raw_annotations:
for s in i['paragraphs'][0]['sentences']:
all_onto.append(onto_to_prodigy_complete(s))
all_onto = list(set_hashes(eg) for eg in all_onto)
print("length all_onto")
print(len(all_onto))
random.shuffle(all_onto)
# get Prodigy annotations
db = connect()
annot = db.get_dataset(dataset)
random.shuffle(annot)
print("Found {0} annotations in {1}".format(len(annot), dataset))
# Get the examples to augment
aug_num = multiplier * len(annot)
print("Augmenting existing examples with {0} OntoNotes sentences".format(aug_num))
#with open("allonto.json","w") as outfile:
# json.dump(all_onto,outfile)
newaugment = all_onto[0:aug_num]
update_ldc_labels(newaugment)
print("label set for augmented data:");
augment=[];
for data in newaugment:
if(data['spans'][0]['label']=="" or data['spans'][0]['label']=="MISC"):
continue
augment.append(data)
getlabelset(augment)
if split:
cutpoint = round(len(annot) * 0.2)
eval_prod = annot[0:cutpoint]
annot = annot[cutpoint:]
eval_onto = all_onto[aug_num:aug_num + 5*cutpoint]
#both = augment + annot
both=augment+annot
#both=all_onto
print("length for both")
print(len(both))
random.shuffle(both)
# use a hardcoded rehearsal dataset because we're dropping and don't want
# to take user input here. If it exists, drop it so we can refresh it.
datasetname="augmented_for_training_2"
exs = db.get_dataset(datasetname)
if exs:
db.drop_dataset(datasetname)
db.add_examples(both, [datasetname])
print("Wrote examples to the Prodigy dataset "+datasetname+" . Use 'ner.batch-train' on that dataset.")
if split:
eo = db.get_dataset("onto_for_eval")
if eo:
db.drop_dataset("onto_for_eval")
ep = db.get_dataset("prodigy_for_eval")
if ep:
db.drop_dataset("prodigy_for_eval")
db.add_examples(eval_onto, ["onto_for_eval"])
db.add_examples(eval_prod, ["prodigy_for_eval"])
print("Wrote eval examples to `prodigy_for_eval` and `onto_for_eval`")
if __name__ == "__main__":
plac.call(main)