Skip to content

API Reference

Welcome to the API reference for EsTranslator. This section provides detailed information about the classes, methods, and attributes available in the EsTranslator library.

es_translator.EsTranslator

es_translator.EsTranslator(options)

Parameters:

Name Type Description Default
options Dict[str, Any]

A dictionary of options needed to set up the translator.

required
Source code in es_translator/es_translator.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def __init__(self, options: Dict[str, Any]):
    """Initialize the Elasticsearch translator.

    Args:
        options (Dict[str, Any]): A dictionary of options needed to set up the translator.
    """
    self.url = options['url']
    self.index = options['index']
    self.source_language = options['source_language']
    self.target_language = options['target_language']
    self.intermediary_language = options['intermediary_language']
    self.source_field = options['source_field']
    self.target_field = options['target_field']
    self.query_string = options['query_string']
    self.data_dir = options['data_dir']
    self.scan_scroll = options['scan_scroll']
    self.dry_run = options.get('dry_run', False)
    self.pool_size = options['pool_size']
    self.pool_timeout = options['pool_timeout']
    self.throttle = options['throttle']
    self.progressbar = options.get('progressbar', False)
    self.interpreter_name = options['interpreter']
    self.max_content_length = options.get('max_content_length', -1)
    self.plan = options.get('plan', False)

no_progressbar property

no_progressbar: bool

Check if the progressbar option is set to False.

Returns:

Name Type Description
bool bool

True if the progressbar option is False, else False.

search_source property

search_source: List[str]

Gets the list of fields to use in the search.

Returns:

Type Description
List[str]

List[str]: list of fields to use in the search.

stdout_loglevel property

stdout_loglevel: int

Gets the log level of stdout.

Returns:

Name Type Description
int int

The log level of stdout.

configure_search()

Configures the search object.

Returns:

Name Type Description
Search Search

A configured search object.

Source code in es_translator/es_translator.py
120
121
122
123
124
125
126
127
128
129
def configure_search(self) -> Search:
    """Configures the search object.

    Returns:
        Search: A configured search object.
    """
    search = self.search()
    search = search.source(self.search_source)
    search = search.params(scroll=self.scan_scroll, size=self.pool_size)
    return search

create_translation_queue

create_translation_queue()

Creates a queue that can translate documents in parallel.

Returns:

Name Type Description
JoinableQueue JoinableQueue

A queue for parallel document translation.

Source code in es_translator/es_translator.py
143
144
145
146
147
148
149
def create_translation_queue(self) -> JoinableQueue:
    """Creates a queue that can translate documents in parallel.

    Returns:
        JoinableQueue: A queue for parallel document translation.
    """
    return JoinableQueue(self.pool_size)

init_interpreter

init_interpreter()

Initializes the interpreter.

Returns:

Name Type Description
Any Any

The initialized interpreter.

Source code in es_translator/es_translator.py
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
def init_interpreter(self) -> Any:
    """Initializes the interpreter.

    Returns:
        Any: The initialized interpreter.
    """
    pack_dir = path.join(self.data_dir, 'packs', self.interpreter_name)
    interpreters = (Apertium, Argos,)
    Interpreter = next(
        i for i in interpreters if i.name.lower() == self.interpreter_name.lower())
    return Interpreter(
        self.source_language,
        self.target_language,
        self.intermediary_language,
        pack_dir)

instantiate_interpreter

instantiate_interpreter()

Instantiates the interpreter.

Returns:

Name Type Description
Any Any

An instance of the interpreter.

Source code in es_translator/es_translator.py
109
110
111
112
113
114
115
116
117
118
def instantiate_interpreter(self) -> Any:
    """Instantiates the interpreter.

    Returns:
        Any: An instance of the interpreter.
    """
    if not hasattr(self, 'interpreter'):
        with self.print_done(f'Instantiating {self.interpreter_name} interpreter'):
            self.interpreter = self.init_interpreter()
    return self.interpreter

print_done

print_done(string)

Prints a string and yields.

Parameters:

Name Type Description Default
string str

The string to be printed.

required

Returns:

Name Type Description
ContextManager ContextManager

A context manager.

Source code in es_translator/es_translator.py
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
@contextmanager
def print_done(self, string: str) -> ContextManager:
    """Prints a string and yields.

    Args:
        string (str): The string to be printed.

    Returns:
        ContextManager: A context manager.
    """
    logger.info(string)
    if self.stdout_loglevel > 20:
        string = '\r%s...' % string
        self.print_flush(string)
        try:
            yield
            print('{0} \033[92mdone\033[0m'.format(string))
        except (FatalTranslationException, ElasticsearchException, Full) as error:
            logger.error(error, exc_info=True)
            print('{0} \033[91merror\033[0m'.format(string))
            sys.exit(1)
    else:
        yield

print_flush

print_flush(string)

Prints and flushes a string to stdout.

Parameters:

Name Type Description Default
string str

The string to be printed.

required
Source code in es_translator/es_translator.py
255
256
257
258
259
260
261
262
def print_flush(self, string: str) -> None:
    """Prints and flushes a string to stdout.

    Args:
        string (str): The string to be printed.
    """
    sys.stdout.write('\r{0}'.format(string))
    sys.stdout.flush()

process_document

process_document(translation_queue, hit, progress, task, shared_fatal_error)

Processes a document.

Parameters:

Name Type Description Default
translation_queue JoinableQueue

A queue for parallel document translation.

required
hit Any

The document to be translated.

required
index int

The index of the document.

required
progress Progress

A progress object.

required
task Any

The current task.

required
shared_fatal_error Manager

A shared manager for fatal errors.

required
Source code in es_translator/es_translator.py
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
def process_document(
        self,
        translation_queue: JoinableQueue,
        hit: Any,
        progress: Progress,
        task: Any,
        shared_fatal_error: Manager) -> None:
    """Processes a document.

    Args:
        translation_queue (JoinableQueue): A queue for parallel document translation.
        hit (Any): The document to be translated.
        index (int): The index of the document.
        progress (Progress): A progress object.
        task (Any): The current task.
        shared_fatal_error (Manager): A shared manager for fatal errors.
    """
    translation_queue.put((self, hit), True, self.pool_timeout)
    progress.advance(task)
    if shared_fatal_error.value:
        raise FatalTranslationException(shared_fatal_error.value)

search

search()

Executes a search query.

Returns:

Name Type Description
Search Search

The search result.

Source code in es_translator/es_translator.py
227
228
229
230
231
232
233
234
235
236
237
def search(self) -> Search:
    """Executes a search query.

    Returns:
        Search: The search result.
    """
    using = self.create_client()
    search = Search(index=self.index, using=using)
    if self.query_string:
        search = search.query("query_string", query=self.query_string)
    return search

start

start()

Starts or plans the translation process.

Source code in es_translator/es_translator.py
57
58
59
60
61
62
def start(self) -> None:
    """Starts or plans the translation process."""
    if self.plan:
        self.start_later()
    else:
        self.start_now()

start_later

start_later()

Plan the translation process.

Source code in es_translator/es_translator.py
76
77
78
79
80
81
82
83
84
85
def start_later(self):
    """Plan the translation process."""
    self.instantiate_interpreter()
    total = self.search().count()
    desc = f"Planning translation for {total} document{'s'[:total^1]}"
    with self.print_done(desc):
        search = self.configure_search()
        for hit in search.scan():
            logger.info(f'Planned translation for doc {hit.meta.id}')
            translate_document_task.delay(self.options, hit.meta.to_dict())

start_now

start_now()

Starts the translation process.

Source code in es_translator/es_translator.py
64
65
66
67
68
69
70
71
72
73
74
def start_now(self) -> None:
    """Starts the translation process."""
    self.instantiate_interpreter()
    total = self.search().count()
    desc = 'Translating %s document(s)' % total
    with self.print_done(desc):
        search = self.configure_search()
        translation_queue = self.create_translation_queue()
        with self.with_shared_fatal_error() as shared_fatal_error:
            self.translate_documents_in_pool(
                search, translation_queue, shared_fatal_error, total)

translate_documents_in_pool

translate_documents_in_pool(search, translation_queue, shared_fatal_error, total)

Translates documents.

Parameters:

Name Type Description Default
search Search

A search object.

required
translation_queue JoinableQueue

A queue for parallel document translation.

required
shared_fatal_error Manager

A shared manager for fatal errors.

required
total int

The total number of documents.

required
Source code in es_translator/es_translator.py
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
def translate_documents_in_pool(
        self,
        search: Search,
        translation_queue: JoinableQueue,
        shared_fatal_error: Manager,
        total: int) -> None:
    """Translates documents.

    Args:
        search (Search): A search object.
        translation_queue (JoinableQueue): A queue for parallel document translation.
        shared_fatal_error (Manager): A shared manager for fatal errors.
        total (int): The total number of documents.
    """
    with Pool(self.pool_size, translation_worker, (translation_queue, shared_fatal_error)):
        with Progress(disable=self.no_progressbar, transient=True) as progress:
            task = progress.add_task(
                f"Translating {total} document{'s'[:total^1]}", total=total)
            for hit in search.scan():
                self.process_document(
                    translation_queue, hit, progress, task, shared_fatal_error)
            translation_queue.join()

with_shared_fatal_error

with_shared_fatal_error()

Creates a context manager for managing shared fatal errors.

Returns:

Name Type Description
ContextManager ContextManager

A context manager.

Source code in es_translator/es_translator.py
151
152
153
154
155
156
157
158
159
@contextmanager
def with_shared_fatal_error(self) -> ContextManager:
    """Creates a context manager for managing shared fatal errors.

    Returns:
        ContextManager: A context manager.
    """
    with Manager() as manager:
        yield manager.Value('b', None)

es_translator.interpreters.Argos

es_translator.interpreters.Argos(source=None, target=None, intermediary=None, pack_dir=None)

Bases: AbstractInterpreter

Argos is a class that extends the AbstractInterpreter.

This class is responsible for handling translation tasks, specifically using the Argos interpreter.

Attributes:

Name Type Description
name str

The name of the interpreter.

The function also checks if the necessary language pair is available, and downloads it if necessary.

Parameters:

Name Type Description Default
source str

The source language code.

None
target str

The target language code.

None
intermediary str

The intermediary language code. Defaults to None.

None
pack_dir str

The directory of language packs. This option will be ignored. Defaults to None.

None

Raises:

Type Description
Exception

If the necessary language pair is not available and cannot be downloaded.

Source code in es_translator/interpreters/argos/argos.py
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
def __init__(
        self,
        source=None,
        target=None,
        intermediary=None,
        pack_dir=None):
    """
    Initializes the Argos interpreter with source and target language codes.

    The function also checks if the necessary language pair is available, and downloads it if necessary.

    Args:
        source (str): The source language code.
        target (str): The target language code.
        intermediary (str, optional): The intermediary language code. Defaults to None.
        pack_dir (str, optional): The directory of language packs. This option will be ignored. Defaults to None.

    Raises:
        Exception: If the necessary language pair is not available and cannot be downloaded.
    """
    super().__init__(source, target)
    # Raise an exception if an intermediary language is provded
    if intermediary is not None:
        logger.warn(
            'Argos interpreter doesnt support intermediary language')
    if pack_dir is not None:
        logger.warn(
            'Argos interpreter doesnt support custom pack directory')
    # Raise an exception if the language pair is unkown
    if not self.is_pair_available and self.has_pair:
        try:
            self.download_necessary_languages()
        except ArgosPairNotAvailable:
            raise Exception('The pair %s is not available' % self.pair)
    else:
        logger.info('Existing package(s) found for pair %s' % self.pair)

is_pair_available property

is_pair_available

Checks if the necessary language pair is available in the installed packages.

Returns:

Name Type Description
bool

True if the language pair is available, False otherwise.

local_languages property

local_languages

Gets the codes for the installed languages.

This method retrieves the installed languages and returns their codes as a list. If an AttributeError is encountered, it returns an empty list.

Returns:

Type Description

list of str: The codes for the installed languages.

translation property

translation

Returns a Translation object for the source and target languages.

Raises:

Type Description
Exception

If either the source or target language is not installed.

Returns:

Name Type Description
Translation

The Translation object for the source and target languages.

download_and_install_package

download_and_install_package(package)

Downloads and installs a language package.

This method locks the download using a file lock based on the package's source and target language codes. If the package is not installed, it is downloaded and installed.

Parameters:

Name Type Description Default
package Package

The package to download and install.

required

Raises:

Type Description
Timeout

If a lock on the download path cannot be acquired within the timeout duration.

Source code in es_translator/interpreters/argos/argos.py
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
def download_and_install_package(self, package):
    """
    Downloads and installs a language package.

    This method locks the download using a file lock based on the package's source and target language codes.
    If the package is not installed, it is downloaded and installed.

    Args:
        package (Package): The package to download and install.

    Raises:
        Timeout: If a lock on the download path cannot be acquired within the timeout duration.
    """
    try:
        temp_dir = Path(tempfile.gettempdir())
        lock_path = temp_dir / f'{package.from_code}_{package.to_code}.lock'

        with FileLock(lock_path, timeout=600).acquire(timeout=600):
            if self.is_package_installed(package):
                return
            download_path = package.download()
            logger.info('Installing Argos package %s', package)
            return argospackage.install_from_path(download_path)
    except Timeout as exc:
        raise ArgosPackageDownloadLockTimeout(
            f'Another instance of the program is downloading the package {package}. Please try again later.') from exc

download_necessary_languages

download_necessary_languages()

Downloads necessary language packages if they are not installed already.

This method performs the following steps: 1. Updates the package index. 2. Finds the necessary package. 3. Downloads and installs the package.

Raises:

Type Description
ArgosPairNotAvailable

If the necessary language package could not be found.

Timeout

If a lock on the download path cannot be acquired within the timeout duration.

Source code in es_translator/interpreters/argos/argos.py
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
def download_necessary_languages(self):
    """
    Downloads necessary language packages if they are not installed already.

    This method performs the following steps:
    1. Updates the package index.
    2. Finds the necessary package.
    3. Downloads and installs the package.

    Raises:
        ArgosPairNotAvailable: If the necessary language package could not be found.
        Timeout: If a lock on the download path cannot be acquired within the timeout duration.
    """
    self.update_package_index()
    necessary_package = self.find_necessary_package()
    self.download_and_install_package(necessary_package)

find_necessary_package

find_necessary_package()

Finds the necessary language package.

This method loops over the available packages to find the necessary package based on the source and target language codes. If the package cannot be found, it raises an ArgosPairNotAvailable exception.

Returns:

Name Type Description
Package

The necessary language package.

Raises:

Type Description
ArgosPairNotAvailable

If the necessary language package could not be found.

Source code in es_translator/interpreters/argos/argos.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def find_necessary_package(self):
    """
    Finds the necessary language package.

    This method loops over the available packages to find the necessary package based on the source and target language codes.
    If the package cannot be found, it raises an ArgosPairNotAvailable exception.

    Returns:
        Package: The necessary language package.

    Raises:
        ArgosPairNotAvailable: If the necessary language package could not be found.
    """
    for package in argospackage.get_available_packages():
        if package.from_code == self.source_alpha_2 and package.to_code == self.target_alpha_2:
            return package
    raise ArgosPairNotAvailable

is_package_installed

is_package_installed(package)

Checks if a package is installed.

Parameters:

Name Type Description Default
package Package

The package to check.

required

Returns:

Name Type Description
bool

True if the package is installed, False otherwise.

Source code in es_translator/interpreters/argos/argos.py
126
127
128
129
130
131
132
133
134
135
136
def is_package_installed(self, package):
    """
    Checks if a package is installed.

    Args:
        package (Package): The package to check.

    Returns:
        bool: True if the package is installed, False otherwise.
    """
    return package in argospackage.get_installed_packages()

translate

translate(text_input)

Translates the input text from the source language to the target language.

Parameters:

Name Type Description Default
text_input str

The input text in the source language.

required

Returns:

Name Type Description
str

The translated text in the target language.

Source code in es_translator/interpreters/argos/argos.py
204
205
206
207
208
209
210
211
212
213
214
def translate(self, text_input):
    """
    Translates the input text from the source language to the target language.

    Args:
        text_input (str): The input text in the source language.

    Returns:
        str: The translated text in the target language.
    """
    return self.translation.translate(text_input)

update_package_index

update_package_index()

Updates the package index.

Source code in es_translator/interpreters/argos/argos.py
102
103
104
105
106
def update_package_index(self):
    """
    Updates the package index.
    """
    argospackage.update_package_index()