This vignette introduces the Apertium translation functionality in
the polyglotr
package. Apertium is an open-source machine
translation platform that provides free translation services without
requiring API keys or authentication.
Apertium is a free, open-source machine translation platform that:
https://apertium.org/apy/
The apertium_translate()
function provides a simple
interface to translate text using the Apertium API:
# Basic translation from English to Spanish
result <- apertium_translate(
text = "Hello, world!",
target_language = "es",
source_language = "en"
)
print(result)
# Translation from Spanish to English
result <- apertium_translate(
text = "Hola, mundo!",
target_language = "en",
source_language = "es"
)
print(result)
Before translating, you can check which language pairs are supported
using the apertium_get_language_pairs()
function:
You can translate multiple pieces of text by using vectorization or loops:
# Vector of texts to translate
texts <- c(
"Good morning",
"How are you?",
"Thank you very much",
"See you later"
)
# Translate each text
translations <- sapply(texts, function(text) {
apertium_translate(
text = text,
target_language = "es",
source_language = "en"
)
})
# Display results
data.frame(
English = texts,
Spanish = translations,
stringsAsFactors = FALSE
)
The function allows you to specify a custom host if you’re using a different Apertium instance:
The function includes basic error handling for common issues:
Apertium offers several advantages within the polyglotr ecosystem:
# Compare translations across services
text <- "The weather is beautiful today"
# Apertium translation
apertium_result <- apertium_translate(text, "es", "en")
# MyMemory translation (also free, but different approach)
mymemory_result <- mymemory_translate(text, "es", "en")
data.frame(
Service = c("Apertium", "MyMemory"),
Translation = c(apertium_result, mymemory_result),
stringsAsFactors = FALSE
)
When using Apertium translation:
# Example of robust translation function
safe_apertium_translate <- function(text, target_lang, source_lang, max_retries = 3) {
for (attempt in 1:max_retries) {
tryCatch({
result <- apertium_translate(text, target_lang, source_lang)
return(result)
}, error = function(e) {
if (attempt == max_retries) {
stop("Translation failed after ", max_retries, " attempts: ", e$message)
}
Sys.sleep(1) # Wait before retry
})
}
}
# Usage
result <- safe_apertium_translate("Hello world", "es", "en")
print(result)
The apertium_translate()
function provides a simple,
free, and open-source solution for machine translation within the
polyglotr package. It’s particularly useful for users who want to avoid
API keys and usage limits while still accessing quality machine
translation services.
For more information about Apertium, visit https://apertium.org/ and explore their documentation at https://apertium.org/apy/.