-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotation_projection.py
621 lines (555 loc) · 23.9 KB
/
annotation_projection.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
import os
from typing import Optional, List
from tokenization.conll2text import conll2text
from tokenization.utils import count_lines
from projection.annotation_proyection import dataset_projection
import argparse
def generate_alignments(
source_train: Optional[str],
source_dev: Optional[str],
source_test: Optional[str],
target_train: Optional[str],
target_dev: Optional[str],
target_test: Optional[str],
source_augmentation: Optional[str],
target_augmentation: Optional[str],
output_dir: str,
output_name: str,
do_fastalign: bool = False,
do_mgiza: bool = False,
do_simalign: bool = True,
do_awesome: bool = False,
remove_awesome_model: bool = True,
model_name_or_path: str = "bert-base-multilingual-cased",
):
"""
Generate word alignments for the given datasets.
:param str source_train: Path to the source language training dataset. A txt file, one sentence per line.
:param str source_dev: Path to the source language development dataset. A txt file, one sentence per line.
:param str source_test: Path to the source language test dataset. A txt file, one sentence per line.
:param str target_train: Path to the target language training dataset. A txt file, one sentence per line.
:param str target_dev: Path to the target language development dataset. A txt file, one sentence per line.
:param str target_test: Path to the target language test dataset. A txt file, one sentence per line.
:param str source_augmentation: Path to the source language augmentation dataset. A txt file, one sentence per line.
:param str target_augmentation: Path to the target language augmentation dataset. A txt file, one sentence per line.
:param str output_dir: Path to the output directory.
:param str output_name: Name of the output files
:param bool do_fastalign: Whether to generate word alignments with fastalign.
:param bool do_mgiza: Whether to generate word alignments with mgiza.
:param bool do_simalign: Whether to generate word alignments with simalign.
:param bool do_awesome: Whether to generate word alignments with awesome.
:param bool remove_awesome_model: Whether to remove the trained awesome model after the alignment generation.
:param str model_name_or_path: Hugginface Hub model name or path to a local model. Used for simalign and awesome.
"""
# 1) Sanity checks
assert source_train or source_dev or source_test, f"Nothing to do"
assert target_train or target_dev or target_test, f"Nothing to do"
assert (source_train is not None and target_train is not None) or (
source_train is None and target_train is None
), f"Source train: {source_train}. Target train: {target_train}"
assert (source_dev is not None and target_dev is not None) or (
source_dev is None and target_dev is None
), f"Source dev: {source_dev}. Target dev: {target_dev}"
assert (source_test is not None and target_test is not None) or (
source_test is None and target_test is None
), f"Source test: {source_test}. Target test: {target_test}"
assert (source_augmentation is not None and target_augmentation is not None) or (
source_augmentation is None and target_augmentation is None
)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Projection
source_paths: List[str] = []
target_paths: List[str] = []
if source_train:
source_paths.append(source_train)
target_paths.append(target_train)
if source_dev:
source_paths.append(source_dev)
target_paths.append(target_dev)
if source_test:
source_paths.append(source_test)
target_paths.append(target_test)
if do_mgiza:
from mgiza.generate_alignments import generate_word_alignments_mgiza
output_names = []
if source_train:
output_names.append(output_name + ".mgiza.train")
if source_dev:
output_names.append(output_name + ".mgiza.dev")
if source_test:
output_names.append(output_name + ".mgiza.test")
print(
f"Generate word alignments Mgiza.\n"
f"Source paths: {source_paths}.\n"
f"Target paths: {target_paths}.\n"
f"source_parallel_corpus: {source_augmentation}.\n"
f"target_parallel_corpus: {target_augmentation}.\n"
f"Output names: {output_names}.\n"
f"Output_dir: {output_dir}.\n"
)
generate_word_alignments_mgiza(
source_paths=source_paths,
target_paths=target_paths,
source_parallel_corpus=[source_augmentation]
if source_augmentation
else None,
target_parallel_corpus=[target_augmentation]
if target_augmentation
else None,
output_names=output_names,
output_dir=output_dir,
)
if do_fastalign:
from fast_align.generate_alignments import generate_word_alignments_fast_align
output_names = []
if source_train:
output_names.append(output_name + ".fast_align.train")
if source_dev:
output_names.append(output_name + ".fast_align.dev")
if source_test:
output_names.append(output_name + ".fast_align.test")
print(
f"Generate word alignments Fast Align.\n"
f"Source paths: {source_paths}.\n"
f"Target paths: {target_paths}.\n"
f"source_parallel_corpus: {source_augmentation}.\n"
f"target_parallel_corpus: {target_augmentation}.\n"
f"Output names: {output_names}.\n"
f"Output_dir: {output_dir}.\n"
)
generate_word_alignments_fast_align(
source_paths=source_paths,
target_paths=target_paths,
source_parallel_corpus=[source_augmentation]
if source_augmentation
else None,
target_parallel_corpus=[target_augmentation]
if target_augmentation
else None,
output_names=output_names,
output_dir=output_dir,
)
if do_simalign:
from SimAlign.generate_alignments import generate_word_alignments_simalign
if source_train and target_train:
print(
f"Generate word alignments SimAlign. "
f"source_file: {source_train}. "
f"target_file: {target_train}. "
f"output: {os.path.join(output_dir, f'{output_name}.simalign.train')}"
)
generate_word_alignments_simalign(
source_file=source_train,
target_file=target_train,
output=os.path.join(output_dir, f"{output_name}.simalign.train"),
model=model_name_or_path,
)
if source_dev and target_dev:
print(
f"Generate word alignments SimAlign. "
f"source_file: {source_dev}. "
f"target_file: {target_dev}. "
f"output: {os.path.join(output_dir, f'{output_name}.simalign.dev')}"
)
generate_word_alignments_simalign(
source_file=source_dev,
target_file=target_dev,
output=os.path.join(output_dir, f"{output_name}.simalign.dev"),
model=model_name_or_path,
)
if source_test and target_test:
print(
f"Generate word alignments SimAlign. "
f"source_file: {source_test}. "
f"target_file: {target_test}. "
f"output: {os.path.join(output_dir, f'{output_name}.simalign.test')}"
)
generate_word_alignments_simalign(
source_file=source_test,
target_file=target_test,
output=os.path.join(output_dir, f"{output_name}.simalign.test"),
model=model_name_or_path,
)
if do_awesome:
from awesome.generate_alignments import generate_word_alignments_awesome
output_names = []
if source_train:
output_names.append(output_name + ".awesome.train.talp")
if source_dev:
output_names.append(output_name + ".awesome.dev.talp")
if source_test:
output_names.append(output_name + ".awesome.test.talp")
print(
f"Generate word alignments awesome.\n"
f"Source paths: {source_paths}.\n"
f"Target paths: {target_paths}.\n"
f"source_parallel_corpus: {source_augmentation}.\n"
f"target_parallel_corpus: {target_augmentation}.\n"
f"Output names: {output_names}.\n"
f"Output_dir: {output_dir}.\n"
)
generate_word_alignments_awesome(
source_paths=source_paths,
target_paths=target_paths,
source_parallel_corpus=[source_augmentation]
if source_augmentation
else None,
target_parallel_corpus=[target_augmentation]
if target_augmentation
else None,
output_names=output_names,
output_dir=output_dir,
remove_tmp_dir=remove_awesome_model,
model_name_or_path=model_name_or_path,
)
def run_projection(
source_train: Optional[str],
source_dev: Optional[str],
source_test: Optional[str],
target_train: Optional[str],
target_dev: Optional[str],
target_test: Optional[str],
source_augmentation: Optional[str],
target_augmentation: Optional[str],
output_dir: str,
output_name: str,
do_fastalign: bool = False,
do_mgiza: bool = False,
do_simalign: bool = True,
do_awesome: bool = False,
remove_awesome_model: bool = True,
model_name_or_path: str = "bert-base-multilingual-cased",
remove_puncs: bool = True,
fill_gap_size: int = 1,
use_existing_alignments: bool = False,
):
"""
Perform annotation projection for the given datasets.
:param str source_train: Path to the source language training dataset. A tsv file.
:param str source_dev: Path to the source language development dataset. A tsv file.
:param str source_test: Path to the source language test dataset. A tsv file.
:param str target_train: Path to the target language training dataset. A txt file, one sentence per line.
:param str target_dev: Path to the target language development dataset. A txt file, one sentence per line.
:param str target_test: Path to the target language test dataset. A txt file, one sentence per line.
:param str source_augmentation: Path to the source language augmentation dataset. A txt file, one sentence per line.
:param str target_augmentation: Path to the target language augmentation dataset. A txt file, one sentence per line.
:param str output_dir: Path to the output directory.
:param str output_name: Name of the output files
:param bool do_fastalign: Whether to generate word alignments with fastalign.
:param bool do_mgiza: Whether to generate word alignments with mgiza.
:param bool do_simalign: Whether to generate word alignments with simalign.
:param bool do_awesome: Whether to generate word alignments with awesome.
:param bool remove_awesome_model: Whether to remove the trained awesome model after the alignment generation.
:param str model_name_or_path: Hugginface Hub model name or path to a local model. Used for simalign and awesome.
:param bool remove_puncs: If a source word is aligned to a punctuation mark, we remove the alignment.
Use True if you are projection named entities or labels with a small number of words.
Use false for argumentation datasets and datasets in which the labels are long sentences.
:param int fill_gap_size: If the projected label is split in two or more parts, we fill the gap if the gap size is
less or equal than fill_gap_size. Else we will choose the largest label and remove the other part.
Use True 1 if you are projection named entities or labels with a small number of words.
Use a larger value for argumentation datasets and datasets in which the labels are long sentences.
:param bool use_existing_alignments: Whether to use existing word alignments instead of generating new ones. You
must use the same --output_dir and --output_name as the one used to generate the word alignments. You must
also use the same train, dev and test files.
"""
if not os.path.exists(output_dir):
os.makedirs(output_dir)
assert source_train or source_dev or source_test, f"Nothing to do"
assert target_train or target_dev or target_test, f"Nothing to do"
assert (source_train is not None and target_train is not None) or (
source_train is None and target_train is None
), f"Source train: {source_train}. Target train: {target_train}"
assert (source_dev is not None and target_dev is not None) or (
source_dev is None and target_dev is None
), f"Source dev: {source_dev}. Target dev: {target_dev}"
assert (source_test is not None and target_test is not None) or (
source_test is None and target_test is None
), f"Source test: {source_test}. Target test: {target_test}"
assert (source_augmentation is not None and target_augmentation is not None) or (
source_augmentation is None and target_augmentation is None
)
if source_train:
source_train_txt = os.path.join(
output_dir, os.path.basename(os.path.splitext(source_train)[0]) + ".txt"
)
conll2text(input_path=source_train, sentences_output_path=source_train_txt)
lines_source = count_lines(input_path=source_train_txt)
lines_target = count_lines(input_path=target_train)
assert lines_source == lines_target, (
f"The number of lines in the source and target files are different.\n"
f"Source ({source_train_txt}): {lines_source}\n"
f"Target ({target_train}): {lines_target}"
)
else:
source_train_txt = None
if source_dev:
source_dev_txt = os.path.join(
output_dir, os.path.basename(os.path.splitext(source_dev)[0]) + ".txt"
)
conll2text(input_path=source_dev, sentences_output_path=source_dev_txt)
lines_source = count_lines(input_path=source_dev_txt)
lines_target = count_lines(input_path=target_dev)
assert lines_source == lines_target, (
f"The number of lines in the source and target files are different.\n"
f"Source ({source_dev_txt}): {lines_source}\n"
f"Target ({target_dev}): {lines_target}"
)
else:
source_dev_txt = None
if source_test:
source_test_txt = os.path.join(
output_dir, os.path.basename(os.path.splitext(source_test)[0]) + ".txt"
)
conll2text(input_path=source_test, sentences_output_path=source_test_txt)
lines_source = count_lines(input_path=source_test_txt)
lines_target = count_lines(input_path=target_test)
assert lines_source == lines_target, (
f"The number of lines in the source and target files are different.\n"
f"Source ({source_test_txt}): {lines_source}\n"
f"Target ({target_test}): {lines_target}"
)
else:
source_test_txt = None
if source_augmentation:
lines_source = count_lines(input_path=source_augmentation)
lines_target = count_lines(input_path=target_augmentation)
assert lines_source == lines_target, (
f"The number of lines in the source and target files are different.\n"
f"Source ({source_augmentation}): {lines_source}\n"
f"Target ({target_augmentation}): {lines_target}"
)
if not use_existing_alignments:
generate_alignments(
source_train=source_train_txt,
target_train=target_train,
source_dev=source_dev_txt,
target_dev=target_dev,
source_test=source_test_txt,
target_test=target_test,
source_augmentation=source_augmentation,
target_augmentation=target_augmentation,
output_dir=output_dir,
output_name=output_name,
do_fastalign=do_fastalign,
do_mgiza=do_mgiza,
do_simalign=do_simalign,
do_awesome=do_awesome,
remove_awesome_model=remove_awesome_model,
model_name_or_path=model_name_or_path,
)
else:
print(
f"--use_existing_alignments is set to True. We will attempt to use the existing word alignments."
)
alignment_list = []
if do_mgiza:
alignment_list.append("mgiza")
if do_fastalign:
alignment_list.append("fast_align")
if do_simalign:
alignment_list.append("simalign")
if do_awesome:
alignment_list.append("awesome")
dataset_list = []
if source_train:
dataset_list.append("train")
if source_dev:
dataset_list.append("dev")
if source_test:
dataset_list.append("test")
output_files: List[str] = []
for alignment_method in alignment_list:
for dataset_split in dataset_list:
if alignment_method == "mgiza" or alignment_method == "fast_align":
alignments_path = os.path.join(
output_dir,
f"{output_name}.{alignment_method}.{dataset_split}.grow_diag_final-and.talp",
)
elif alignment_method == "simalign":
alignments_path = os.path.join(
output_dir,
f"{output_name}.{alignment_method}.{dataset_split}.itermax.talp",
)
elif alignment_method == "awesome":
alignments_path = os.path.join(
output_dir,
f"{output_name}.{alignment_method}.{dataset_split}.talp",
)
else:
raise ValueError(f"{alignment_method} not supported")
if dataset_split == "train":
source_dataset = source_train
target_dataset = target_train
elif dataset_split == "dev":
source_dataset = source_dev
target_dataset = target_dev
elif dataset_split == "test":
source_dataset = source_test
target_dataset = target_test
else:
raise ValueError(f"{dataset_split} dataset split not supported")
dataset_projection(
source_dataset=source_dataset,
target_sentences=target_dataset,
alignments_path=alignments_path,
batch_size=10000,
output_path=os.path.join(
output_dir, f"{output_name}.{alignment_method}.{dataset_split}.tsv"
),
remove_puncs=remove_puncs,
fill_gap_size=fill_gap_size,
)
output_files.append(
os.path.join(
output_dir, f"{output_name}.{alignment_method}.{dataset_split}.tsv"
)
)
if source_train_txt:
os.remove(source_train_txt)
if source_dev_txt:
os.remove(source_dev_txt)
if source_test_txt:
os.remove(source_test_txt)
print("Done!")
print("Output files:")
print("\n".join(output_files))
print("\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Perform annotation projection for a given dataset."
)
parser.add_argument(
"--source_train",
default=None,
type=str,
help="Path to the source training file. TSV format",
)
parser.add_argument(
"--target_train",
default=None,
type=str,
help="Path to the target training file. A txt file with one sentence per line",
)
parser.add_argument(
"--source_dev",
default=None,
type=str,
help="Path to the source development file. TSV format",
)
parser.add_argument(
"--target_dev",
default=None,
type=str,
help="Path to the target development file. A txt file with one sentence per line",
)
parser.add_argument(
"--source_test",
default=None,
type=str,
help="Path to the source test file. TSV format",
)
parser.add_argument(
"--target_test",
default=None,
type=str,
help="Path to the target test file. A txt file with one sentence per line",
)
parser.add_argument(
"--source_augmentation",
default=None,
type=str,
help="Path to the source augmentation file. A txt file with one sentence per line",
)
parser.add_argument(
"--target_augmentation",
default=None,
type=str,
help="Path to the target augmentation file. A txt file with one sentence per line",
)
parser.add_argument(
"--output_dir",
type=str,
help="Path to the output directory",
)
parser.add_argument(
"--output_name",
type=str,
help="Name of the output file",
)
parser.add_argument(
"--do_mgiza",
action="store_true",
help="Whether to generate alignments using mgiza",
)
parser.add_argument(
"--do_fastalign",
action="store_true",
help="Whether to generate alignments using fast_align",
)
parser.add_argument(
"--do_simalign",
action="store_true",
help="Whether to generate alignments using simalign",
)
parser.add_argument(
"--do_awesome",
action="store_true",
help="Whether to generate alignments using awesome",
)
parser.add_argument(
"--remove_awesome_model",
action="store_true",
help="Whether to remove the trained awesome model after the alignment is generated",
)
parser.add_argument(
"--model_name_or_path",
default="bert-base-multilingual-cased",
type=str,
help="Huggingface Hub model name or path to a local model",
)
parser.add_argument(
"--do_not_remove_puncs",
action="store_false",
help="If a source word is aligned to a punctuation mark, we remove the alignment. "
"Do not set this flag if you are projection named entities or labels with a small number of words. "
"Use false for argumentation datasets and datasets in which the labels are long sentences.",
)
parser.add_argument(
"--fill_gap_size",
default=1,
type=int,
help="If the projected label is split in two or more parts, we fill the gap if the gap size is less or equal "
"than fill_gap_size. Else shouldwe will choose the largest label and remove the other part. "
"Use True 1 if you are projection named entities or labels with a small number of words. "
"Use a larger value for argumentation datasets and datasets in which the labels are long sentences.",
)
parser.add_argument(
"--use_existing_alignments",
action="store_true",
help="If set, the script will use the existing alignments in the output directory instead of generating "
"new ones. You must use the same --output_dir and --output_name as the previous run and the same "
"train, dev and test files.",
)
args = parser.parse_args()
run_projection(
source_train=args.source_train,
target_train=args.target_train,
source_dev=args.source_dev,
target_dev=args.target_dev,
source_test=args.source_test,
target_test=args.target_test,
source_augmentation=args.source_augmentation,
target_augmentation=args.target_augmentation,
output_dir=args.output_dir,
output_name=args.output_name,
do_mgiza=args.do_mgiza,
do_fastalign=args.do_fastalign,
do_simalign=args.do_simalign,
do_awesome=args.do_awesome,
remove_awesome_model=args.remove_awesome_model,
model_name_or_path=args.model_name_or_path,
remove_puncs=args.do_not_remove_puncs,
fill_gap_size=args.fill_gap_size,
use_existing_alignments=args.use_existing_alignments,
)