| Title: | Local Machine-Translation with 'EasyNMT' from 'R' |
|---|---|
| Description: | 'R' wrapper around the 'Python' library 'EasyNMT', enabling easy-to-use, local, reproducible machine translation. Supports translation between multiple language pairs. The package takes care of preprocessing and language detection using 'fastText' from 'R'. 'Python' backend and installation of 'PyTorch' is handled by 'reticulate' and 'uv'. |
| Authors: | Daniel Thiele [aut, cre] (ORCID: <https://orcid.org/0000-0003-0324-4943>) |
| Maintainer: | Daniel Thiele <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.0.5 |
| Built: | 2026-07-17 06:32:29 UTC |
| Source: | https://github.com/thieled/easiernmt |
Displays diagnostic information about the easynmt Python backend setup.
check_backend()check_backend()
Invisibly returns a list with backend configuration details.
## Not run: easynmt::check_backend() ## End(Not run)## Not run: easynmt::check_backend() ## End(Not run)
Cleans and optionally tokenizes raw text data. Handles emoji replacement, removal of unsupported characters, normalization, sentence tokenization, and chunking into word-limited segments. Returns either a standardized data.table or a character vector.
clean_text( x, text_col = "text", id_col = NULL, lang_guess_col = NULL, replace_emojis = TRUE, replace_alphaless = TRUE, max_char = 5000, tokenize_sentences = TRUE, max_words = 50, clean_characters = TRUE, return_string = FALSE, verbose = TRUE )clean_text( x, text_col = "text", id_col = NULL, lang_guess_col = NULL, replace_emojis = TRUE, replace_alphaless = TRUE, max_char = 5000, tokenize_sentences = TRUE, max_words = 50, clean_characters = TRUE, return_string = FALSE, verbose = TRUE )
x |
Character vector or data.frame containing texts to clean. |
text_col |
Character, name of the text column if |
id_col |
Character, optional column name in the input data.frame to
preserve as |
lang_guess_col |
Character, optional column name in the input data.frame
to preserve as |
replace_emojis |
Logical, whether to replace emojis with placeholder names. Default = TRUE. |
replace_alphaless |
Logical, whether to replace strings that contain no alphabetic characters with empty strings. Default = TRUE. |
max_char |
Integer, maximum number of characters per text. Texts longer than this are truncated. Default = 5000. |
tokenize_sentences |
Logical, whether to split texts into sentences and chunks. Default = TRUE. |
max_words |
Integer, maximum number of words per sentence or chunk. Long sentences are split into chunks of this size. Default = 50. |
clean_characters |
Logical, whether to apply character normalization (regex replacements, squishing, punctuation handling). Default = TRUE. |
return_string |
Logical, if TRUE, return only the cleaned character
vector ( |
verbose |
Logical, whether to print progress messages. Default = TRUE. |
Either:
A data.table with columns:
sen_id: Unique identifier for each sentence or chunk.
doc_idx: Row index of the original document in the input.
sen_idx: Sentence/chunk index within each document.
id: Optional user-provided identifier.
text_orig: Original text.
text_clean: Cleaned and normalized text.
lang_guess: Optional language guess column, if present.
Or a character vector of cleaned texts if return_string = TRUE.
Identifies the language of each text in a character vector or data.frame
using fastText's pretrained language identification model. Calls clean_text()
internally to normalize and optionally tokenize the text before prediction.
detect_languages( x, text_col = "text", id_col = NULL, lang_guess_col = NULL, model_path = NULL, prob_threshold = 0.25, und_label = "und", max_char = 5000, tokenize_sentences = FALSE, threads = parallel::detectCores(), verbose = TRUE, max_words = 50, ... )detect_languages( x, text_col = "text", id_col = NULL, lang_guess_col = NULL, model_path = NULL, prob_threshold = 0.25, und_label = "und", max_char = 5000, tokenize_sentences = FALSE, threads = parallel::detectCores(), verbose = TRUE, max_words = 50, ... )
x |
Character vector or data.frame containing texts to clean. |
text_col |
Character, name of the text column if |
id_col |
Character, optional column name in the input data.frame to
preserve as |
lang_guess_col |
Character, optional column name in the input data.frame
to preserve as |
model_path |
Character, path to the fastText |
prob_threshold |
Numeric, threshold below which the detected language is replaced
by |
und_label |
Character, label for undefined language if confidence is below
|
max_char |
Integer, maximum number of characters per text. Texts longer than this are truncated. Default = 5000. |
tokenize_sentences |
Logical, whether to split texts into sentences and chunks. Default = TRUE. |
threads |
Integer, number of threads for fastText. Default = |
verbose |
Logical, whether to print progress messages. Default = TRUE. |
max_words |
Integer, maximum number of words per sentence or chunk. Long sentences are split into chunks of this size. Default = 50. |
... |
Additional parameters passed on to |
A data.table with standardized columns:
sen_id, doc_idx, sen_idx, optional id, text_orig,
text_clean, optional lang_guess, lang, and lang_prob.
Declares all required Python packages via py_require,
which provisions them using uv into an ephemeral virtual environment.
PyTorch is installed with UV_TORCH_BACKEND=auto, which lets uv
auto-detect NVIDIA (CUDA), AMD (ROCm), and Intel GPUs and pick the correct
wheels — no manual driver querying needed. Pass gpu = FALSE to force
CPU-only onnxruntime.
The function is called automatically at the start of each easynmt function and is a no-op after the first successful call within a session.
initialize_easynmt(gpu = check_gpu(), uv_cache_dir = NULL, models_dir = NULL)initialize_easynmt(gpu = check_gpu(), uv_cache_dir = NULL, models_dir = NULL)
gpu |
Logical. Whether to install |
uv_cache_dir |
Character (optional). Directory used by uv to install python libraries. |
models_dir |
Character (optional). Directory used to cache huggingface models. |
Non-torch packages are declared with py_require() so that reticulate /
uv can resolve them together. PyTorch is handled by setting
UV_TORCH_BACKEND=auto, which instructs uv to query for CUDA, ROCm,
and Intel GPU drivers and select the matching PyTorch index automatically.
onnxruntime-gpu only supports CUDA, so the gpu parameter
reflects NVIDIA presence only; AMD and Intel users get CPU onnxruntime while
still getting GPU-accelerated PyTorch.
fasttext workaround: EasyNMT declares fasttext as a hard
Requires-Dist dependency (not optional). The canonical fasttext
package has no prebuilt wheels for Windows and fails to compile from C++
source on MSVC. The workaround uses UV_OVERRIDE with a direct-URL
specifier that points fasttext at the fasttext-predict wheel —
a prediction-only fork that ships prebuilt binary wheels for all platforms
and Python 3.9–3.13, and exposes the same import fasttext namespace.
The override file contains fasttext @ https://... with the PyPI URL
for the matching wheel, selected at runtime by platform and Python version.
Language detection is handled in R, so the prediction-only scope of
fasttext-predict is not a limitation.
Invisibly returns TRUE.
Wrapper around detect_languages() that detects languages,
reprocesses low-confidence cases, and splits the data into homogeneous
language groups for translation. Tokenization can be performed before or
after language detection.
preprocess( x, text_col = "text", id_col = NULL, lang_guess_col = NULL, targ_lang, model_path = NULL, prob_threshold = 0.25, und_label = "und", max_char = 5000, chunk_size = NULL, tokenize_sentences = TRUE, tokenize_when = "after", threads = parallel::detectCores(), verbose = TRUE, max_words = max_words, ... )preprocess( x, text_col = "text", id_col = NULL, lang_guess_col = NULL, targ_lang, model_path = NULL, prob_threshold = 0.25, und_label = "und", max_char = 5000, chunk_size = NULL, tokenize_sentences = TRUE, tokenize_when = "after", threads = parallel::detectCores(), verbose = TRUE, max_words = max_words, ... )
x |
Character vector or data.frame containing texts to clean. |
text_col |
Character, name of the text column if |
id_col |
Character, optional column name in the input data.frame to
preserve as |
lang_guess_col |
Character, optional column name in the input data.frame
to preserve as |
targ_lang |
Character, fallback language code when detection is uncertain. |
model_path |
Character, path to the fastText |
prob_threshold |
Numeric, threshold below which the detected language is replaced
by |
und_label |
Character, label for undefined language if confidence is below
|
max_char |
Integer, maximum number of characters per text. Texts longer than this are truncated. Default = 5000. |
chunk_size |
Integer, optional. If provided, split each homogeneous
language data.table into chunks of at most |
tokenize_sentences |
Logical, whether to split texts into sentences and chunks. Default = TRUE. |
tokenize_when |
Character, determines when to tokenize: |
threads |
Integer, number of threads for fastText. Default = |
verbose |
Logical, whether to print progress messages. Default = TRUE. |
max_words |
Integer, maximum number of words per sentence or chunk. Long sentences are split into chunks of this size. Default = 50. |
... |
Additional parameters passed on to |
A named list of data.tables, each containing texts of one homogeneous language or language+part chunk.
Replaces all emojis in a character vector with their textual names (in :name: style).
replace_emoji_with_name(text_vec)replace_emoji_with_name(text_vec)
text_vec |
Character vector of texts. |
Character vector with emojis replaced by names.
Detects languages, reprocesses uncertain cases, optionally
splits long texts into sentences, splits the input into homogeneous
language groups, and calls the Python translator
(easynmt_translate()) on each group.
translate( x, text_col = "text", id_col = NULL, lang_guess_col = NULL, targ_lang, model_path = NULL, prob_threshold = 0.25, und_label = "und", max_char = 5000, threads = parallel::detectCores(), target_lang, model = "opus-mt", seed = 42L, batch_size = 20L, max_length_tl = 512L, beam_size = 1L, deterministic = TRUE, verbose = TRUE, check_translation = FALSE, n_retries = 3L, check_threshold = 0.6, return_string = FALSE, save_dir = NULL, tokenize_sentences = TRUE, uv_cache_dir = NULL, models_dir = NULL, ... )translate( x, text_col = "text", id_col = NULL, lang_guess_col = NULL, targ_lang, model_path = NULL, prob_threshold = 0.25, und_label = "und", max_char = 5000, threads = parallel::detectCores(), target_lang, model = "opus-mt", seed = 42L, batch_size = 20L, max_length_tl = 512L, beam_size = 1L, deterministic = TRUE, verbose = TRUE, check_translation = FALSE, n_retries = 3L, check_threshold = 0.6, return_string = FALSE, save_dir = NULL, tokenize_sentences = TRUE, uv_cache_dir = NULL, models_dir = NULL, ... )
x |
Character vector or data.frame containing texts to clean. |
text_col |
Character, name of the text column if |
id_col |
Character, optional column name in the input data.frame to
preserve as |
lang_guess_col |
Character, optional column name in the input data.frame
to preserve as |
targ_lang |
Character, fallback language code when detection is uncertain. |
model_path |
Character, path to the fastText |
prob_threshold |
Numeric, threshold below which the detected language is replaced
by |
und_label |
Character, label for undefined language if confidence is below
|
max_char |
Integer, maximum number of characters per text. Texts longer than this are truncated. Default = 5000. |
threads |
Integer, number of threads for fastText. Default = |
target_lang |
Character, target language for translation. |
model |
Character, translation model (default "opus-mt"). |
seed |
Integer, random seed (default 42). |
batch_size |
Integer, batch size for translation (default 20L). |
max_length_tl |
Integer, maximum token length (default 512L). |
beam_size |
Integer, beam size for translation (default 1L). |
deterministic |
Logical, enforce deterministic cudnn ops (default TRUE). |
verbose |
Logical, whether to print progress messages. Default = TRUE. |
check_translation |
Logical, perform retry check (default FALSE). |
n_retries |
Integer, number of retries if check fails (default 3L). |
check_threshold |
Numeric, threshold for ratio of unique tokens (target / source) below which retries are attempted (default 0.6). |
return_string |
Logical, if TRUE, return only the translated character vector. Default = FALSE. |
save_dir |
Optional character path. If provided, saves each processed
subset as an |
tokenize_sentences |
Logical, if TRUE, split long texts into sentences during preprocessing and reassemble them after translation. Default = TRUE. |
uv_cache_dir |
Character (optional). Directory used by uv to install python libraries, passed on to initialize_easynmt. |
models_dir |
Character (optional). Directory used to cache huggingface models, passed on to initialize_easynmt. |
... |
Additional parameters passed on to |
A data.table with original data plus translation results merged in.