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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
|
%global _empty_manifest_terminate_build 0
Name: python-opustools-pkg
Version: 0.0.52
Release: 1
Summary: Tools to read OPUS
License: MIT License
URL: https://github.com/Helsinki-NLP/OpusTools
Source0: https://mirrors.aliyun.com/pypi/web/packages/14/25/843d07bee632c269820bbe7e0a0843c9e2af8ad7ebd441970ca2869432ca/opustools_pkg-0.0.52.tar.gz
BuildArch: noarch
%description
## opus_read
### Usage
```
usage: opus_read [-h] -d corpus_name -s langid -t langid [-r version]
[-p {raw,xml,parsed}] [-m M] [-S S] [-T T] [-a attribute]
[-tr TR] [-ln] [-w file_name [file_name ...]]
[-wm {normal,moses,tmx,links}] [-pn] [-f] [-rd path_to_dir]
[-af path_to_file] [-sz path_to_zip] [-tz path_to_zip]
[-cm delimiter] [-pa] [-sa attribute [attribute ...]]
[-ta attribute [attribute ...]] [-ca delimiter]
[--src_cld2 lang_id score] [--trg_cld2 lang_id score]
[--src_langid lang_id score] [--trg_langid lang_id score]
[-id file_name] [-q] [-dl DOWNLOAD_DIR] [-pi] [-v]
```
arguments:
```
-h, --help show this help message and exit
-d corpus_name, --directory corpus_name
Corpus name
-s langid, --source langid
Source language
-t langid, --target langid
Target language
-r version, --release version
Release (default=latest)
-p {raw,xml,parsed}, --preprocess {raw,xml,parsed}
Preprocess-type (raw, xml or parsed, default=xml)
-m MAXIMUM, --maximum MAXIMUM Maximum number of alignments
-S SRC_RANGE, --src_range SRC_RANGE
Number of source sentences in alignments (range is
allowed, eg. -S 1-2)
-T TGT_RANGE, --tgt_range TGT_RANGE
Number of target sentences in alignments (range is
allowed, eg. -T 1-2)
-a attribute, --attribute attribute
Set attribute for filttering
-tr THRESHOLD, --threshold THRESHOLD
Set threshold for an attribute
-ln, --leave_non_alignments_out
Leave non-alignments out
-w file_name [file_name ...], --write file_name [file_name ...]
Write to file. To print moses format in separate
files, enter two file names. Otherwise enter one file
name.
-wm {normal,moses,tmx,links}, --write_mode {normal,moses,tmx,links}
Set write mode
-pn, --print_file_names
Print file names when using moses format
-f, --fast Fast parsing. Faster than normal parsing, if you print
a small part of the whole corpus, but requires the
sentence ids in alignment files to be in sequence.
-rd path_to_dir, --root_directory path_to_dir
Change root directory (default=/proj/nlpl/data/OPUS)
-af path_to_file, --alignment_file path_to_file
Use given alignment file
-sz path_to_zip, --source_zip path_to_zip
Use given source zip file
-tz path_to_zip, --target_zip path_to_zip
Use given target zip file
-cm delimiter, --change_moses_delimiter delimiter
Change moses delimiter (default=tab)
-pa, --print_annotations
Print annotations, if they exist
-sa attribute [attribute ...], --source_annotations attribute [attribute ...]
Set source sentence annotation attributes to be
printed, e.g. -sa pos lem. To print all available
attributes use -sa all_attrs (default=pos lem)
-ta attribute [attribute ...], --target_annotations attribute [attribute ...]
Set target sentence annotation attributes to be
printed, e.g. -ta pos lem. To print all available
attributes use -ta all_attrs (default=pos lem)
-ca delimiter, --change_annotation_delimiter delimiter
Change annotation delimiter (default=|)
--src_cld2 lang_id score
Filter source sentences by their cld2 language id
labels and confidence score, e.g. en 0.9
--trg_cld2 lang_id score
Filter target sentences by their cld2 language id
labels and confidence score, e.g. en 0.9
--src_langid lang_id score
Filter source sentences by their langid.py language id
labels and confidence score, e.g. en 0.9
--trg_langid lang_id score
Filter target sentences by their langid.py language id
labels and confidence score, e.g. en 0.9
-id file_name, --write_ids file_name
Write sentence ids to a file.
-q, --suppress_prompts
Download necessary files without prompting "(y/n)"
-dl DOWNLOAD_DIR, --download_dir DOWNLOAD_DIR
Set download directory (default=current directory)
-pi, --preserve_inline_tags
Preserve inline tags within sentences
-v, --verbose Print prorgess messages when writing results to files
```
### Description
`opus_read` is a script to read sentence alignments stored in XCES align format and prints the aligned sentences to STDOUT. It requires monolingual alignments of sentences in linked XML files. Linked XML files are specified in the "toDoc" and "fromDoc" attributes (see below).
```
<cesAlign version="1.0">
<linkGrp targType="s" toDoc="source1.xml" fromDoc="target1.xml">
<link certainty="0.88" xtargets="s1.1 s1.2;s1.1" id="SL1" />
<linkGrp targType="s" toDoc="source2.xml" fromDoc="target2.xml">
<link certainty="0.88" xtargets="s1.1;s1.1" id="SL1" />
```
Several parameters can be set to filter the alignments and to print only certain types of alignments.
`opus_read` can also be used to filter the XCES alignment files and to print the remaining links in the same
XCES align format. Set the "-wm" flag to "links" to enable this mode.
`opus_read` reads the alignments from zip files. Starting up the script might take some time, if the zip files are large (for example OpenSubtitles in OPUS).
`opus_read` uses `ExhaustiveSentenceParser` by default. This means that each time a `<linkGrp>` tag is found, the corresponding source and target documents are read through and each sentence is stored in a hashmap with the sentence id as the key. This allows the reader to read alignment files that have sentence ids in non-sequential order. Each time a `<linkGrp>` tag is found, the script pauses printing for a second to read through the source and target documents. The duration of the pause depends on the size of the source and target documents.
Using the "-f" flag allows the usage of `SentenceParser`, which is faster than ExhaustiveSentenceParser in cases where only a small part of a corpus is read. `SentenceParser` does not store the sentences in a hashmap. Rather, when it finds a `<link>` tag, it iterates through a sentence file until a sentence id is matched with the sentence id found in the `<link>` tag. SentenceParser can't go backwards, which means that if the ids are not in sequential order in the alignment file, the parser will not find alignment pairs after the sentence id sequence breaks. `SentenceParser` is less reliable than `ExhaustiveSentenceParser`, but using the "-f" flag is beneficial when the whole corpus does not need to be scanned, in other words, when using the "-m" flag.
**Examples:**
Read sentence alignment in XCES align format. Necessary files will be downloaded if they are not found locally:
`opus_read --directory RF --source en --target sv`
Read sentences with specific preprocessing type. (default is xml, which is tokenized text):
`opus_read --directory RF --source en --target sv --preprocess raw`
Leave non-alignments (pairs with no sentences on one side) out
```
opus_read --directory RF \
--source en \
--target sv \
--preprocess raw\
--leave_non_alignments_out
```
Print first 10 alignment pairs:
`opus_read --directory RF --source en --target sv -m 10`
Print XCES align format of all 1:1 sentence alignments:
```
opus_read --directory RF \
--source en \
--target sv \
--src_range 1 \
--tgt_range 1
```
Print alignments with alignment certainty greater than 1.1:
```
opus_read --directory RF \
--source en \
--target sv \
--attribute certainty \
--threshold 1.1
```
Write results to file:
`opus_read --directory RF --source en --target sv --write result.txt`
Write with different output format:
```
opus_read --directory RF \
--source en \
--target sv \
--write result.tmx\
--write_mode tmx
```
Write moses format to one file:
```
opus_read --directory RF \
--source en \
--target sv \
--write en-sv.txt\
--write_mode moses
```
or to two files:
```
opus_read --directory RF \
--source en \
--target sv \
--write en-sv.en en-sv.sv \
--write_mode moses
```
Read sentences using your alignment file. First create an alignment file, for example:
```
opus_read --directory RF \
--source en \
--target sv \
--attribute certainty \
--threshold 1.1 \
--write_mode links \
--write en-sv.links
```
Then use the created alignment file:
`opus_read --directory RF --source en --target sv --alignment_file en-sv.links`
Annotations can be printed with `--print_annotations` if they are included in the sentence files. To print all annotation attributes, specify this with `--source_annotations` and `--target_annotations` flags:
```
opus_read --directory RF \
--source en \
--target sv \
--print_annotations \
--source_annotations all_attrs \
--target_annotations all_attrs
```
Sentences can be filtered by their language id labels and confidence score. First, the language ids need to be added to the sentence files with `opus_langid`. If you have run the previous examples, you should have `RF_latest_xml_en.zip` and `RF_latest_xml_sv.zip` in your current working directory. Apply `opus_langid` to these files:
```
opus_langid --file_path RF_latest_xml_en.zip
opus_langid --file_path RF_latest_xml_sv.zip
```
Now you can filter by language ids. This example uses both cld2 and langid.py language detection confidence scores:
```
opus_read --directory RF \
--source en \
--target sv \
--src_cld2 en 0.99 \
--trg_cld2 sv 0.99 \
--src_langid en 1 \
--trg_langid sv 1
```
**You can also import the module to your python script:**
In `your_script.py`, first import the package:
`import opustools_pkg`
Initialize OpusRead:
```
opus_reader = opustools_pkg.OpusRead(
directory='Books',
source='en',
target='fi')
opus_reader.printPairs()
```
and then run:
%package -n python3-opustools-pkg
Summary: Tools to read OPUS
Provides: python-opustools-pkg
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-opustools-pkg
## opus_read
### Usage
```
usage: opus_read [-h] -d corpus_name -s langid -t langid [-r version]
[-p {raw,xml,parsed}] [-m M] [-S S] [-T T] [-a attribute]
[-tr TR] [-ln] [-w file_name [file_name ...]]
[-wm {normal,moses,tmx,links}] [-pn] [-f] [-rd path_to_dir]
[-af path_to_file] [-sz path_to_zip] [-tz path_to_zip]
[-cm delimiter] [-pa] [-sa attribute [attribute ...]]
[-ta attribute [attribute ...]] [-ca delimiter]
[--src_cld2 lang_id score] [--trg_cld2 lang_id score]
[--src_langid lang_id score] [--trg_langid lang_id score]
[-id file_name] [-q] [-dl DOWNLOAD_DIR] [-pi] [-v]
```
arguments:
```
-h, --help show this help message and exit
-d corpus_name, --directory corpus_name
Corpus name
-s langid, --source langid
Source language
-t langid, --target langid
Target language
-r version, --release version
Release (default=latest)
-p {raw,xml,parsed}, --preprocess {raw,xml,parsed}
Preprocess-type (raw, xml or parsed, default=xml)
-m MAXIMUM, --maximum MAXIMUM Maximum number of alignments
-S SRC_RANGE, --src_range SRC_RANGE
Number of source sentences in alignments (range is
allowed, eg. -S 1-2)
-T TGT_RANGE, --tgt_range TGT_RANGE
Number of target sentences in alignments (range is
allowed, eg. -T 1-2)
-a attribute, --attribute attribute
Set attribute for filttering
-tr THRESHOLD, --threshold THRESHOLD
Set threshold for an attribute
-ln, --leave_non_alignments_out
Leave non-alignments out
-w file_name [file_name ...], --write file_name [file_name ...]
Write to file. To print moses format in separate
files, enter two file names. Otherwise enter one file
name.
-wm {normal,moses,tmx,links}, --write_mode {normal,moses,tmx,links}
Set write mode
-pn, --print_file_names
Print file names when using moses format
-f, --fast Fast parsing. Faster than normal parsing, if you print
a small part of the whole corpus, but requires the
sentence ids in alignment files to be in sequence.
-rd path_to_dir, --root_directory path_to_dir
Change root directory (default=/proj/nlpl/data/OPUS)
-af path_to_file, --alignment_file path_to_file
Use given alignment file
-sz path_to_zip, --source_zip path_to_zip
Use given source zip file
-tz path_to_zip, --target_zip path_to_zip
Use given target zip file
-cm delimiter, --change_moses_delimiter delimiter
Change moses delimiter (default=tab)
-pa, --print_annotations
Print annotations, if they exist
-sa attribute [attribute ...], --source_annotations attribute [attribute ...]
Set source sentence annotation attributes to be
printed, e.g. -sa pos lem. To print all available
attributes use -sa all_attrs (default=pos lem)
-ta attribute [attribute ...], --target_annotations attribute [attribute ...]
Set target sentence annotation attributes to be
printed, e.g. -ta pos lem. To print all available
attributes use -ta all_attrs (default=pos lem)
-ca delimiter, --change_annotation_delimiter delimiter
Change annotation delimiter (default=|)
--src_cld2 lang_id score
Filter source sentences by their cld2 language id
labels and confidence score, e.g. en 0.9
--trg_cld2 lang_id score
Filter target sentences by their cld2 language id
labels and confidence score, e.g. en 0.9
--src_langid lang_id score
Filter source sentences by their langid.py language id
labels and confidence score, e.g. en 0.9
--trg_langid lang_id score
Filter target sentences by their langid.py language id
labels and confidence score, e.g. en 0.9
-id file_name, --write_ids file_name
Write sentence ids to a file.
-q, --suppress_prompts
Download necessary files without prompting "(y/n)"
-dl DOWNLOAD_DIR, --download_dir DOWNLOAD_DIR
Set download directory (default=current directory)
-pi, --preserve_inline_tags
Preserve inline tags within sentences
-v, --verbose Print prorgess messages when writing results to files
```
### Description
`opus_read` is a script to read sentence alignments stored in XCES align format and prints the aligned sentences to STDOUT. It requires monolingual alignments of sentences in linked XML files. Linked XML files are specified in the "toDoc" and "fromDoc" attributes (see below).
```
<cesAlign version="1.0">
<linkGrp targType="s" toDoc="source1.xml" fromDoc="target1.xml">
<link certainty="0.88" xtargets="s1.1 s1.2;s1.1" id="SL1" />
<linkGrp targType="s" toDoc="source2.xml" fromDoc="target2.xml">
<link certainty="0.88" xtargets="s1.1;s1.1" id="SL1" />
```
Several parameters can be set to filter the alignments and to print only certain types of alignments.
`opus_read` can also be used to filter the XCES alignment files and to print the remaining links in the same
XCES align format. Set the "-wm" flag to "links" to enable this mode.
`opus_read` reads the alignments from zip files. Starting up the script might take some time, if the zip files are large (for example OpenSubtitles in OPUS).
`opus_read` uses `ExhaustiveSentenceParser` by default. This means that each time a `<linkGrp>` tag is found, the corresponding source and target documents are read through and each sentence is stored in a hashmap with the sentence id as the key. This allows the reader to read alignment files that have sentence ids in non-sequential order. Each time a `<linkGrp>` tag is found, the script pauses printing for a second to read through the source and target documents. The duration of the pause depends on the size of the source and target documents.
Using the "-f" flag allows the usage of `SentenceParser`, which is faster than ExhaustiveSentenceParser in cases where only a small part of a corpus is read. `SentenceParser` does not store the sentences in a hashmap. Rather, when it finds a `<link>` tag, it iterates through a sentence file until a sentence id is matched with the sentence id found in the `<link>` tag. SentenceParser can't go backwards, which means that if the ids are not in sequential order in the alignment file, the parser will not find alignment pairs after the sentence id sequence breaks. `SentenceParser` is less reliable than `ExhaustiveSentenceParser`, but using the "-f" flag is beneficial when the whole corpus does not need to be scanned, in other words, when using the "-m" flag.
**Examples:**
Read sentence alignment in XCES align format. Necessary files will be downloaded if they are not found locally:
`opus_read --directory RF --source en --target sv`
Read sentences with specific preprocessing type. (default is xml, which is tokenized text):
`opus_read --directory RF --source en --target sv --preprocess raw`
Leave non-alignments (pairs with no sentences on one side) out
```
opus_read --directory RF \
--source en \
--target sv \
--preprocess raw\
--leave_non_alignments_out
```
Print first 10 alignment pairs:
`opus_read --directory RF --source en --target sv -m 10`
Print XCES align format of all 1:1 sentence alignments:
```
opus_read --directory RF \
--source en \
--target sv \
--src_range 1 \
--tgt_range 1
```
Print alignments with alignment certainty greater than 1.1:
```
opus_read --directory RF \
--source en \
--target sv \
--attribute certainty \
--threshold 1.1
```
Write results to file:
`opus_read --directory RF --source en --target sv --write result.txt`
Write with different output format:
```
opus_read --directory RF \
--source en \
--target sv \
--write result.tmx\
--write_mode tmx
```
Write moses format to one file:
```
opus_read --directory RF \
--source en \
--target sv \
--write en-sv.txt\
--write_mode moses
```
or to two files:
```
opus_read --directory RF \
--source en \
--target sv \
--write en-sv.en en-sv.sv \
--write_mode moses
```
Read sentences using your alignment file. First create an alignment file, for example:
```
opus_read --directory RF \
--source en \
--target sv \
--attribute certainty \
--threshold 1.1 \
--write_mode links \
--write en-sv.links
```
Then use the created alignment file:
`opus_read --directory RF --source en --target sv --alignment_file en-sv.links`
Annotations can be printed with `--print_annotations` if they are included in the sentence files. To print all annotation attributes, specify this with `--source_annotations` and `--target_annotations` flags:
```
opus_read --directory RF \
--source en \
--target sv \
--print_annotations \
--source_annotations all_attrs \
--target_annotations all_attrs
```
Sentences can be filtered by their language id labels and confidence score. First, the language ids need to be added to the sentence files with `opus_langid`. If you have run the previous examples, you should have `RF_latest_xml_en.zip` and `RF_latest_xml_sv.zip` in your current working directory. Apply `opus_langid` to these files:
```
opus_langid --file_path RF_latest_xml_en.zip
opus_langid --file_path RF_latest_xml_sv.zip
```
Now you can filter by language ids. This example uses both cld2 and langid.py language detection confidence scores:
```
opus_read --directory RF \
--source en \
--target sv \
--src_cld2 en 0.99 \
--trg_cld2 sv 0.99 \
--src_langid en 1 \
--trg_langid sv 1
```
**You can also import the module to your python script:**
In `your_script.py`, first import the package:
`import opustools_pkg`
Initialize OpusRead:
```
opus_reader = opustools_pkg.OpusRead(
directory='Books',
source='en',
target='fi')
opus_reader.printPairs()
```
and then run:
%package help
Summary: Development documents and examples for opustools-pkg
Provides: python3-opustools-pkg-doc
%description help
## opus_read
### Usage
```
usage: opus_read [-h] -d corpus_name -s langid -t langid [-r version]
[-p {raw,xml,parsed}] [-m M] [-S S] [-T T] [-a attribute]
[-tr TR] [-ln] [-w file_name [file_name ...]]
[-wm {normal,moses,tmx,links}] [-pn] [-f] [-rd path_to_dir]
[-af path_to_file] [-sz path_to_zip] [-tz path_to_zip]
[-cm delimiter] [-pa] [-sa attribute [attribute ...]]
[-ta attribute [attribute ...]] [-ca delimiter]
[--src_cld2 lang_id score] [--trg_cld2 lang_id score]
[--src_langid lang_id score] [--trg_langid lang_id score]
[-id file_name] [-q] [-dl DOWNLOAD_DIR] [-pi] [-v]
```
arguments:
```
-h, --help show this help message and exit
-d corpus_name, --directory corpus_name
Corpus name
-s langid, --source langid
Source language
-t langid, --target langid
Target language
-r version, --release version
Release (default=latest)
-p {raw,xml,parsed}, --preprocess {raw,xml,parsed}
Preprocess-type (raw, xml or parsed, default=xml)
-m MAXIMUM, --maximum MAXIMUM Maximum number of alignments
-S SRC_RANGE, --src_range SRC_RANGE
Number of source sentences in alignments (range is
allowed, eg. -S 1-2)
-T TGT_RANGE, --tgt_range TGT_RANGE
Number of target sentences in alignments (range is
allowed, eg. -T 1-2)
-a attribute, --attribute attribute
Set attribute for filttering
-tr THRESHOLD, --threshold THRESHOLD
Set threshold for an attribute
-ln, --leave_non_alignments_out
Leave non-alignments out
-w file_name [file_name ...], --write file_name [file_name ...]
Write to file. To print moses format in separate
files, enter two file names. Otherwise enter one file
name.
-wm {normal,moses,tmx,links}, --write_mode {normal,moses,tmx,links}
Set write mode
-pn, --print_file_names
Print file names when using moses format
-f, --fast Fast parsing. Faster than normal parsing, if you print
a small part of the whole corpus, but requires the
sentence ids in alignment files to be in sequence.
-rd path_to_dir, --root_directory path_to_dir
Change root directory (default=/proj/nlpl/data/OPUS)
-af path_to_file, --alignment_file path_to_file
Use given alignment file
-sz path_to_zip, --source_zip path_to_zip
Use given source zip file
-tz path_to_zip, --target_zip path_to_zip
Use given target zip file
-cm delimiter, --change_moses_delimiter delimiter
Change moses delimiter (default=tab)
-pa, --print_annotations
Print annotations, if they exist
-sa attribute [attribute ...], --source_annotations attribute [attribute ...]
Set source sentence annotation attributes to be
printed, e.g. -sa pos lem. To print all available
attributes use -sa all_attrs (default=pos lem)
-ta attribute [attribute ...], --target_annotations attribute [attribute ...]
Set target sentence annotation attributes to be
printed, e.g. -ta pos lem. To print all available
attributes use -ta all_attrs (default=pos lem)
-ca delimiter, --change_annotation_delimiter delimiter
Change annotation delimiter (default=|)
--src_cld2 lang_id score
Filter source sentences by their cld2 language id
labels and confidence score, e.g. en 0.9
--trg_cld2 lang_id score
Filter target sentences by their cld2 language id
labels and confidence score, e.g. en 0.9
--src_langid lang_id score
Filter source sentences by their langid.py language id
labels and confidence score, e.g. en 0.9
--trg_langid lang_id score
Filter target sentences by their langid.py language id
labels and confidence score, e.g. en 0.9
-id file_name, --write_ids file_name
Write sentence ids to a file.
-q, --suppress_prompts
Download necessary files without prompting "(y/n)"
-dl DOWNLOAD_DIR, --download_dir DOWNLOAD_DIR
Set download directory (default=current directory)
-pi, --preserve_inline_tags
Preserve inline tags within sentences
-v, --verbose Print prorgess messages when writing results to files
```
### Description
`opus_read` is a script to read sentence alignments stored in XCES align format and prints the aligned sentences to STDOUT. It requires monolingual alignments of sentences in linked XML files. Linked XML files are specified in the "toDoc" and "fromDoc" attributes (see below).
```
<cesAlign version="1.0">
<linkGrp targType="s" toDoc="source1.xml" fromDoc="target1.xml">
<link certainty="0.88" xtargets="s1.1 s1.2;s1.1" id="SL1" />
<linkGrp targType="s" toDoc="source2.xml" fromDoc="target2.xml">
<link certainty="0.88" xtargets="s1.1;s1.1" id="SL1" />
```
Several parameters can be set to filter the alignments and to print only certain types of alignments.
`opus_read` can also be used to filter the XCES alignment files and to print the remaining links in the same
XCES align format. Set the "-wm" flag to "links" to enable this mode.
`opus_read` reads the alignments from zip files. Starting up the script might take some time, if the zip files are large (for example OpenSubtitles in OPUS).
`opus_read` uses `ExhaustiveSentenceParser` by default. This means that each time a `<linkGrp>` tag is found, the corresponding source and target documents are read through and each sentence is stored in a hashmap with the sentence id as the key. This allows the reader to read alignment files that have sentence ids in non-sequential order. Each time a `<linkGrp>` tag is found, the script pauses printing for a second to read through the source and target documents. The duration of the pause depends on the size of the source and target documents.
Using the "-f" flag allows the usage of `SentenceParser`, which is faster than ExhaustiveSentenceParser in cases where only a small part of a corpus is read. `SentenceParser` does not store the sentences in a hashmap. Rather, when it finds a `<link>` tag, it iterates through a sentence file until a sentence id is matched with the sentence id found in the `<link>` tag. SentenceParser can't go backwards, which means that if the ids are not in sequential order in the alignment file, the parser will not find alignment pairs after the sentence id sequence breaks. `SentenceParser` is less reliable than `ExhaustiveSentenceParser`, but using the "-f" flag is beneficial when the whole corpus does not need to be scanned, in other words, when using the "-m" flag.
**Examples:**
Read sentence alignment in XCES align format. Necessary files will be downloaded if they are not found locally:
`opus_read --directory RF --source en --target sv`
Read sentences with specific preprocessing type. (default is xml, which is tokenized text):
`opus_read --directory RF --source en --target sv --preprocess raw`
Leave non-alignments (pairs with no sentences on one side) out
```
opus_read --directory RF \
--source en \
--target sv \
--preprocess raw\
--leave_non_alignments_out
```
Print first 10 alignment pairs:
`opus_read --directory RF --source en --target sv -m 10`
Print XCES align format of all 1:1 sentence alignments:
```
opus_read --directory RF \
--source en \
--target sv \
--src_range 1 \
--tgt_range 1
```
Print alignments with alignment certainty greater than 1.1:
```
opus_read --directory RF \
--source en \
--target sv \
--attribute certainty \
--threshold 1.1
```
Write results to file:
`opus_read --directory RF --source en --target sv --write result.txt`
Write with different output format:
```
opus_read --directory RF \
--source en \
--target sv \
--write result.tmx\
--write_mode tmx
```
Write moses format to one file:
```
opus_read --directory RF \
--source en \
--target sv \
--write en-sv.txt\
--write_mode moses
```
or to two files:
```
opus_read --directory RF \
--source en \
--target sv \
--write en-sv.en en-sv.sv \
--write_mode moses
```
Read sentences using your alignment file. First create an alignment file, for example:
```
opus_read --directory RF \
--source en \
--target sv \
--attribute certainty \
--threshold 1.1 \
--write_mode links \
--write en-sv.links
```
Then use the created alignment file:
`opus_read --directory RF --source en --target sv --alignment_file en-sv.links`
Annotations can be printed with `--print_annotations` if they are included in the sentence files. To print all annotation attributes, specify this with `--source_annotations` and `--target_annotations` flags:
```
opus_read --directory RF \
--source en \
--target sv \
--print_annotations \
--source_annotations all_attrs \
--target_annotations all_attrs
```
Sentences can be filtered by their language id labels and confidence score. First, the language ids need to be added to the sentence files with `opus_langid`. If you have run the previous examples, you should have `RF_latest_xml_en.zip` and `RF_latest_xml_sv.zip` in your current working directory. Apply `opus_langid` to these files:
```
opus_langid --file_path RF_latest_xml_en.zip
opus_langid --file_path RF_latest_xml_sv.zip
```
Now you can filter by language ids. This example uses both cld2 and langid.py language detection confidence scores:
```
opus_read --directory RF \
--source en \
--target sv \
--src_cld2 en 0.99 \
--trg_cld2 sv 0.99 \
--src_langid en 1 \
--trg_langid sv 1
```
**You can also import the module to your python script:**
In `your_script.py`, first import the package:
`import opustools_pkg`
Initialize OpusRead:
```
opus_reader = opustools_pkg.OpusRead(
directory='Books',
source='en',
target='fi')
opus_reader.printPairs()
```
and then run:
%prep
%autosetup -n opustools_pkg-0.0.52
%build
%py3_build
%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
find usr/lib -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
find usr/lib64 -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
find usr/bin -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
find usr/sbin -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
find usr/share/man -type f -printf "\"/%h/%f.gz\"\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .
%files -n python3-opustools-pkg -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Fri Jun 09 2023 Python_Bot <Python_Bot@openeuler.org> - 0.0.52-1
- Package Spec generated
|