Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions opustools_pkg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ usage: opus_read [-h] -d corpus_name -s langid -t langid [-r version]
[--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] [-n regex]
[-N regex] [-cs CHUNK_SIZE] [--doc_level] [--len_name N] [-v]
[-N regex] [-cs CHUNK_SIZE] [--doc_level] [--len_name N] [-v]
[--synthetic]
```


arguments:

```
Expand Down Expand Up @@ -121,12 +123,19 @@ arguments:
--doc_level Print full documents
--len_name N Show the first N charaters of file names when displaying progress. -1 to show full names (default=50)
-v, --verbose Print progress messages
--synthetic Use the synthetic corpus API when downloading files
```

### Description

`opus_read` is a script to read sentence alignments stored in XCES align format and print 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).

Use `--synthetic` to download and read a corpus from the synthetic corpus API, for example:

```
opus_read --synthetic --directory Europarl --source bg --target mk
```

```
<cesAlign version="1.0">
<linkGrp targType="s" toDoc="source1.xml" fromDoc="target1.xml">
Expand Down Expand Up @@ -428,7 +437,7 @@ usage: opus_cat [-h] -d DIRECTORY -l LANGUAGE [-i] [-m MAXIMUM]
[-pp {raw,xml}] [-p] [-f FILE_NAME]
[-r RELEASE] [-pa] [-sa SET_ATTRIBUTE [SET_ATTRIBUTE ...]]
[-ca CHANGE_ANNOTATION_DELIMITER] [-rd path_to_dir]
[-dl DOWNLOAD_DIR]
[-dl DOWNLOAD_DIR] [--synthetic]
```

arguments:
Expand Down Expand Up @@ -460,12 +469,19 @@ arguments:
Change root directory (default=/projappl/nlpl/data/OPUS)
-dl DOWNLOAD_DIR, --download_dir DOWNLOAD_DIR
Set download directory (default=current directory)
--synthetic Use the synthetic corpus API when downloading files
```

### Description

Read a document from OPUS and print to STDOUT

Use `--synthetic` to download and read a document from a synthetic corpus, for example:

```
opus_cat --synthetic --directory Europarl --language en
```

**Examples:**

Read a corpus:
Expand Down Expand Up @@ -501,7 +517,7 @@ opus_cat --directory RF --language en --file_name RF/xml/en/1996.xml
```
usage: opus_get [-h] [-s SOURCE] [-t TARGET] [-d DIRECTORY] [-r RELEASE]
[-p {raw,xml,parsed,mono,moses,tmx,truecaser,ud,freq,smt,dic}]
[-l] [-ll] [-lc] [--local_db] [-db DATABASE]
[-l] [-ll] [-lc] [--local_db] [--synthetic] [-db DATABASE]
[-dl DOWNLOAD_DIR] [-q] [-u] [-w]
```

Expand All @@ -527,6 +543,7 @@ arguments:
-lc, --list_corpora List available corpora. Use -s to find corpora for a given language and use
both -s and -t to find corpora for a given language pair.
--local_db Search resources from the local database instead of the online OPUS-API.
--synthetic Use the synthetic corpus API instead of the default OPUS API.
-db DATABASE, --database DATABASE
Sqlite db file location
-dl DOWNLOAD_DIR, --download_dir DOWNLOAD_DIR
Expand All @@ -542,6 +559,12 @@ arguments:

Download files from OPUS

With `--synthetic`, queries are sent to the [synthetic corpus API](https://opus.nlpl.eu/synthetic-api).
`--directory` selects a collection name, `--release` selects a synthetic version, and `--source`/`--target`
map to the API's `src`/`tgt` filters. Only `xml` and `raw` preprocessing are supported in synthetic mode,
and it cannot be combined with `--local_db`. The default `latest` release resolves to the newest available
version for each collection and language pair.

**Examples:**

List available files in RF corpus for en-sv language pair:
Expand Down Expand Up @@ -592,6 +615,24 @@ Find all corpora that contain the language pair en-sv:
opus_get --list_corpora --source en --target sv
```

List synthetic collections:

```
opus_get --synthetic --list_corpora
```

List synthetic language-pair files for a collection:

```
opus_get --synthetic --directory Europarl --source bg --target mk --list_resources
```

Download the newest synthetic version of a language pair:

```
opus_get --synthetic --directory Europarl --source bg --target mk
```

---

## opus_langid
Expand Down
3 changes: 3 additions & 0 deletions opustools_pkg/bin/opus_cat
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ parser.add_argument('-rd', '--root_directory',
parser.add_argument('-dl', '--download_dir',
help='Set download directory (default=current directory)',
default='.')
parser.add_argument('--synthetic',
help='Use the synthetic corpus API when downloading files',
action='store_true')

args = parser.parse_args()

Expand Down
2 changes: 2 additions & 0 deletions opustools_pkg/bin/opus_get
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ parser.add_argument('-lc', '--list_corpora', help='List available corpora. Use -
action='store_true')
parser.add_argument('--local_db', help='Search resources from the local database instead of the online OPUS-API.',
action='store_true')
parser.add_argument('--synthetic', help='Use the synthetic corpus API instead of the default OPUS API.',
action='store_true')
parser.add_argument('-db', '--database', help='Sqlite db file location', default='~/.OpusTools/opusdata.db')
parser.add_argument('-dl', '--download_dir',
help='Set download directory (default=current directory)', default='.')
Expand Down
3 changes: 3 additions & 0 deletions opustools_pkg/bin/opus_read
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ parser.add_argument('--len_name',
parser.add_argument('-v', '--verbose',
help='Print progress messages when writing results to files',
action='store_true')
parser.add_argument('--synthetic',
help='Use the synthetic corpus API when downloading files',
action='store_true')

args = parser.parse_args()
OpusRead(**vars(args)).printPairs()
15 changes: 10 additions & 5 deletions opustools_pkg/opustools/opus_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ def __init__(self, directory=None, language=None, no_ids=False,
maximum=-2, preprocess='xml', plain=False, file_name=None, release='latest',
print_annotations=False, set_attribute=['pos', 'lem'],
change_annotation_delimiter='|',
root_directory='/projappl/nlpl/data/OPUS', download_dir='.' ):
root_directory='/projappl/nlpl/data/OPUS', download_dir='.',
synthetic=False):
"""Print the contents of a xml sentence file.

Keyword arguments:
Expand All @@ -102,6 +103,7 @@ def __init__(self, directory=None, language=None, no_ids=False,
root_directory -- Root directory for corpus files
(default /projappl/nlpl/data/OPUS)
download_dir -- Directory where files will be downloaded (default .)
synthetic -- Use the synthetic corpus API when downloading files.
"""

self.maximum = maximum
Expand All @@ -116,8 +118,10 @@ def __init__(self, directory=None, language=None, no_ids=False,
self.plain = plain

self.preprocess = preprocess
parser_pp = preprocess
if print_annotations:
self.synthetic = synthetic
self.sentence_preprocess = 'raw' if synthetic else preprocess
parser_pp = self.sentence_preprocess
if print_annotations and not synthetic:
parser_pp = 'parsed'

spar = SentenceParser(None, parser_pp,
Expand Down Expand Up @@ -145,7 +149,8 @@ def openFiles(self, localfile, defaultpath):
self.download_dir]
arguments={'directory': self.directory, 'source': self.language,
'target': '', 'preprocess': self.preprocess, 'list_resources': True,
'release': self.release, 'download_dir': self.download_dir}
'release': self.release, 'download_dir': self.download_dir,
'synthetic': self.synthetic}
og = OpusGet(**arguments)
og.get_files()
arguments['list_resources'] = False
Expand All @@ -165,7 +170,7 @@ def printFile(self, f, n):
maximum = self.maximum
stop = False
sentence = []
if self.preprocess == 'raw':
if self.sentence_preprocess == 'raw':
bp = BlockParser(f, data_tag='s', doc_size=0)
else:
bp = BlockParser(f, data_tag='w', doc_size=0)
Expand Down
12 changes: 9 additions & 3 deletions opustools_pkg/opustools/opus_file_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
class OpusFileHandler:

def __init__(self, download_dir, source_zip, target_zip, directory,
release, preprocess, fromto, suppress_prompts):
release, preprocess, fromto, suppress_prompts, synthetic=False):

self.directory = directory
self.release = release
self.preprocess = preprocess
self.fromto = fromto
self.synthetic = synthetic
self.sentence_preprocess = 'raw' if synthetic else preprocess

self.suppress_prompts = suppress_prompts

Expand All @@ -29,7 +31,8 @@ def download_files(self):
arguments = {'source': self.fromto[0],
'target': self.fromto[1], 'directory': self.directory,
'release': self.release, 'preprocess': self.preprocess,
'download_dir': self.download_dir, 'list_resources': True}
'download_dir': self.download_dir, 'list_resources': True,
'synthetic': self.synthetic}
og = OpusGet(**arguments)
og.get_files()
arguments['list_resources'] = False
Expand Down Expand Up @@ -98,6 +101,8 @@ def open_zipfiles(self):
self.src_zip, self.trg_zip = self.open_specific_zips(
self.src_zip_name, self.trg_zip_name)
elif os.path.isfile(local_src_name) and os.path.isfile(local_trg_name):
self.src_zip_name = local_src_name
self.trg_zip_name = local_trg_name
self.src_zip, self.trg_zip = self.open_specific_zips(
local_src_name, local_trg_name)
else:
Expand Down Expand Up @@ -126,7 +131,8 @@ def open_sentence_file(self, doc_name, direction):

#In OPUS, directory and preprocessing information need to be added and
#the ".gz" ending needs to be removed.
opus_doc_name = self.directory+'/'+self.preprocess+'/'+doc_name[:-3]
zip_preprocess = 'raw' if self.synthetic else self.preprocess
opus_doc_name = self.directory+'/'+zip_preprocess+'/'+doc_name[:-3]

try:
if direction == 'src':
Expand Down
Loading
Loading