Title: | Translate Text |
---|---|
Description: | Provide easy methods to translate pieces of text. Functions send requests to translation services online. |
Authors: | Tomer Iwan [aut, cre, cph] |
Maintainer: | Tomer Iwan <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.5.2 |
Built: | 2025-03-03 06:08:59 UTC |
Source: | https://github.com/tomeriko96/polyglotr |
This function translates a file into each target language using the polyglotr package's translate_file function, and saves the translated files.
batch_translate(input_file, source_language, target_languages)
batch_translate(input_file, source_language, target_languages)
input_file |
A character string indicating the path to the input file. |
source_language |
A character string indicating the source language. |
target_languages |
A character vector indicating the target languages. |
Nothing is returned.
## Not run: batch_translate("README.md", "nl", c("fr", "es", "de")) ## End(Not run)
## Not run: batch_translate("README.md", "nl", c("fr", "es", "de")) ## End(Not run)
This function generates a translation table by translating a list of words into multiple languages.
create_translation_table(words, languages)
create_translation_table(words, languages)
words |
A character vector containing the words to be translated. |
languages |
A character vector specifying the target languages for translation. |
A data frame representing the translation table with original words and translations in each language.
## Not run: words <- c("Hello", "Translate", "Table", "Script") languages <- c("es", "fr", "de", "nl") translations <- create_translation_table(words, languages) print(translations) ## End(Not run)
## Not run: words <- c("Hello", "Translate", "Table", "Script") languages <- c("es", "fr", "de", "nl") translations <- create_translation_table(words, languages) print(translations) ## End(Not run)
This function generates a transliteration table by transliterating a list of words into multiple languages.
create_transliteration_table(words, languages)
create_transliteration_table(words, languages)
words |
A character vector containing the words to be transliterated. |
languages |
A character vector specifying the target languages for transliteration. |
A data frame representing the transliteration table with original words and transliterations in each language.
## Not run: words <- c("Hello world", "Goodbye", "Thank you", "Please") languages <- c("ar", "he", "el", "ru", "fa") transliterations <- create_transliteration_table(words, languages) print(transliterations) ## End(Not run)
## Not run: words <- c("Hello world", "Goodbye", "Thank you", "Please") languages <- c("ar", "he", "el", "ru", "fa") transliterations <- create_transliteration_table(words, languages) print(transliterations) ## End(Not run)
This function fetches the supported languages from the Google Cloud Translate documentation page.
google_get_supported_languages()
google_get_supported_languages()
A data frame containing the supported languages and their corresponding ISO 639-1 codes.
This function checks if a given language code is in the google_supported_languages
dataset.
google_is_valid_language_code(language_code)
google_is_valid_language_code(language_code)
language_code |
The language code to check. |
A logical value indicating if the language code is valid.
## Not run: google_is_valid_language_code("en") # TRUE google_is_valid_language_code("fr") # TRUE google_is_valid_language_code("xx") # FALSE ## End(Not run)
## Not run: google_is_valid_language_code("en") # TRUE google_is_valid_language_code("fr") # TRUE google_is_valid_language_code("xx") # FALSE ## End(Not run)
This dataset contains the language names and iso codes of languages supported by Google Translate API.
google_supported_languages
google_supported_languages
A data frame with two variables: language_name and iso_code
Google Translate API
Translate text using google translate
google_translate(text, target_language = "en", source_language = "auto")
google_translate(text, target_language = "en", source_language = "auto")
text |
This is the text that you want to translate. |
target_language |
This is the language that you want to translate the text into. The default value for this argument is "en" for English. |
source_language |
This is the language of the text that you want to translate. The default value for this argument is "auto", which means that the function will try to automatically detect the language of the text. |
Translated text.
google_translate("I love languages", target_language = "es") text_to_translate <- c("the", "quick", "brown") google_translate(text_to_translate, "fr", "en")
google_translate("I love languages", target_language = "es") text_to_translate <- c("the", "quick", "brown") google_translate(text_to_translate, "fr", "en")
This function translates long text from one language to another using Google Translate. It splits the text into smaller chunks if necessary to handle large inputs.
google_translate_long_text( text, target_language = "en", source_language = "auto", chunk_size = 1000 )
google_translate_long_text( text, target_language = "en", source_language = "auto", chunk_size = 1000 )
text |
The long text to translate. Should be a single string. |
target_language |
The language to translate the text into. Default is "en" for English. |
source_language |
The language of the input text. Default is "auto" for automatic detection. |
chunk_size |
The maximum number of characters to send in a single translation request. Default is 1000. |
A single string containing the translated text.
## Not run: long_text <- paste(rep("This is a long text to translate.", 100), collapse = " ") google_translate_long_text( long_text, target_language = "de", source_language = "en", chunk_size = 500) ## End(Not run)
## Not run: long_text <- paste(rep("This is a long text to translate.", 100), collapse = " ") google_translate_long_text( long_text, target_language = "de", source_language = "en", chunk_size = 500) ## End(Not run)
Transliterate a single word or a sentence to the required language.
google_transliterate(text, language_tag = "el", num = 5)
google_transliterate(text, language_tag = "el", num = 5)
text |
The word or sentence to transliterate from Latin/Roman (English) script. |
language_tag |
The target language's ISO639 code. The default value for this argument is "el" for Greek. |
num |
The maximum number of suggestions to fetch. The default value for this argument is 5. |
Character vector of transliterated sentences or larger pieces of text.
## Not run: google_transliterate("Hello world", "fr", 10) google_transliterate("hello", "el", 10) ## End(Not run)
## Not run: google_transliterate("Hello world", "fr", 10) google_transliterate("hello", "el", 10) ## End(Not run)
This function detects the language of a given text using the Google Translate API.
language_detect(text)
language_detect(text)
text |
The text for which the language needs to be detected. |
A character string representing the detected language.
Retrieve external sources using Linguee Translation API
linguee_external_sources(query, src, dst, limit = 5)
linguee_external_sources(query, src, dst, limit = 5)
query |
The word or phrase for which you want to retrieve external sources. |
src |
The source language of the word or phrase. Accepts language codes such as "en", "es", "fr", etc. |
dst |
The target language for the external source retrieval. Accepts language codes such as "en", "es", "fr", etc. |
limit |
The maximum number of external sources to retrieve. Defaults to 5. |
A dataframe of external sources with columns: src, dst, src_url, dst_url.
linguee_word_translation, linguee_translation_examples
linguee_external_sources(query = "hello", src = "en", dst = "es")
linguee_external_sources(query = "hello", src = "en", dst = "es")
Provide translation examples using Linguee Translation API
linguee_translation_examples( query, src, dst, guess_direction = FALSE, follow_corrections = "always" )
linguee_translation_examples( query, src, dst, guess_direction = FALSE, follow_corrections = "always" )
query |
The word or phrase for which you want translation examples. |
src |
The source language of the word or phrase. Accepts language codes such as "en", "es", "fr", etc. |
dst |
The target language for the translation examples. Accepts language codes such as "en", "es", "fr", etc. |
guess_direction |
A boolean flag that determines whether the API should guess the translation direction. The default value is FALSE. |
follow_corrections |
Specifies how to treat responses with a "did you mean" link. Possible values are "always", "never", or "on_empty_translations". The default value is "always". |
A dataframe of translation examples with columns: source, target, pos.
linguee_word_translation
linguee_translation_examples(query = "hello", src = "en", dst = "es")
linguee_translation_examples(query = "hello", src = "en", dst = "es")
Translate word using Linguee Translation API
linguee_word_translation( word, target_language, source_language, guess_direction = FALSE, follow_corrections = "always" )
linguee_word_translation( word, target_language, source_language, guess_direction = FALSE, follow_corrections = "always" )
word |
This is the word that you want to translate. |
target_language |
This is the language that you want to translate the word into. |
source_language |
This is the language of the word that you want to translate. |
guess_direction |
Specifies whether the API should guess the translation direction when the source language is set to "auto". The default value is FALSE. |
follow_corrections |
Specifies whether the API should include translations that have been marked as corrections. The default value is "always" to include corrections. |
Translated word options.
linguee_word_translation("hello", target_language = "es", source_language = "en")
linguee_word_translation("hello", target_language = "es", source_language = "en")
Get the set of languages currently supported by the Microsoft Translator API
microsoft_supported_languages(scope = NULL)
microsoft_supported_languages(scope = NULL)
scope |
(optional) A comma-separated list of names defining the group of languages to return. Allowed group names are: translation, transliteration, and dictionary. If no scope is given, then all groups are returned. |
A list of supported languages for the specified groups.
## Not run: microsoft_supported_languages(scope = "translation,transliteration,dictionary") ## End(Not run)
## Not run: microsoft_supported_languages(scope = "translation,transliteration,dictionary") ## End(Not run)
Translate text using mymemory translate
mymemory_translate(text, target_language = "en", source_language = "auto")
mymemory_translate(text, target_language = "en", source_language = "auto")
text |
Text to translate. |
target_language |
Language to translate text to. |
source_language |
Language to translate text from |
Translated text.
mymemory_translate("Hello World", target_language = "es", source_language = "en")
mymemory_translate("Hello World", target_language = "es", source_language = "en")
Get the list of available dictionaries from PONS API
pons_dictionaries(language = "en")
pons_dictionaries(language = "en")
language |
The language of the output (ISO 639-1 - two-letter codes). Supported languages are de, el, en, es, fr, it, pl, pt, ru, sl, tr, zh. |
A list of available dictionaries in the specified language.
## Not run: pons_dictionaries(language = "es") ## End(Not run)
## Not run: pons_dictionaries(language = "es") ## End(Not run)
Translate text using PONS
pons_translate(text, target_language = "pt", source_language = "en")
pons_translate(text, target_language = "pt", source_language = "en")
text |
This is the text that you want to translate. Can be a single string or a vector of strings. |
target_language |
This is the language that you want to translate the text into. The default value for this argument is "pt" for Portuguese. |
source_language |
This is the language of the text that you want to translate. The default value for this argument is "en" for English. |
Translated text. If the input is a vector, it returns a character vector of translated strings.
## Not run: pons_translate("I love languages!", target_language = "pt", source_language = "en") text_to_translate <- c("The", "Greatest", "Language") pons_translate(text_to_translate, "pt", "en") ## End(Not run)
## Not run: pons_translate("I love languages!", target_language = "pt", source_language = "en") text_to_translate <- c("The", "Greatest", "Language") pons_translate(text_to_translate, "pt", "en") ## End(Not run)
Get the QCRI API key from the environment variable
qcri_api_key()
qcri_api_key()
The QCRI API key stored in the QCRI_API_KEY environment variable.
This function retrieves the supported domains from the QCRI Multiterm API.
qcri_get_domains(api_key = qcri_api_key())
qcri_get_domains(api_key = qcri_api_key())
api_key |
The API key associated with the user account being used. If not provided, the function will attempt to retrieve it from the QCRI_API_KEY environment variable. |
A list with keys:
success
: Boolean indicating whether the request succeeded.
domains
: Array of supported domains, such as news, tedtalks etc. Only present if success is true.
error
: Error message in case success is false.
## Not run: qcri_get_domains(api_key = "YourApiKey") qcri_get_domains() ## End(Not run)
## Not run: qcri_get_domains(api_key = "YourApiKey") qcri_get_domains() ## End(Not run)
This function retrieves the supported language pairs from the QCRI Multiterm API.
qcri_get_language_pairs(api_key = qcri_api_key())
qcri_get_language_pairs(api_key = qcri_api_key())
api_key |
The API key associated with the user account being used. If not provided, the function will attempt to retrieve it from the QCRI_API_KEY environment variable. You can register for an API key at https://mt.qcri.org/api/register |
Language pairs.
## Not run: qcri_get_language_pairs(api_key = "YourApiKey") qcri_get_language_pairs() ## End(Not run)
## Not run: qcri_get_language_pairs(api_key = "YourApiKey") qcri_get_language_pairs() ## End(Not run)
This function translates a text from the source language to the target language using the QCRI Multiterm API.
qcri_translate_text(text, langpair, domain, api_key = qcri_api_key())
qcri_translate_text(text, langpair, domain, api_key = qcri_api_key())
text |
The text to be translated. This must be URL encoded. |
langpair |
The source-target language pair, where source is language of the provided text and target is the language into which the text has to be translated. |
domain |
The domain over which the translation is tuned. |
api_key |
The API key associated with the user account being used. If not provided, the function will attempt to retrieve it from the QCRI_API_KEY environment variable. |
Translated text.
## Not run: qcri_translate_text(text = "Hello, world!", langpair = "en-ar", domain = "general", api_key = "YourApiKey") qcri_translate_text(text = "Hello, world!", langpair = "en-ar", domain = "general") ## End(Not run)
## Not run: qcri_translate_text(text = "Hello, world!", langpair = "en-ar", domain = "general", api_key = "YourApiKey") qcri_translate_text(text = "Hello, world!", langpair = "en-ar", domain = "general") ## End(Not run)
Translates the content of a file using Google Translate API.
translate_file( file_path, target_language = "en", source_language = "auto", overwrite = FALSE )
translate_file( file_path, target_language = "en", source_language = "auto", overwrite = FALSE )
file_path |
The path to the file to be translated. |
target_language |
The target language to translate the file content to. Default is "en". |
source_language |
The source language of the file content. Default is "auto". |
overwrite |
Logical indicating whether to overwrite the original file with the translated content. Default is FALSE. |
## Not run: translate_file("path/to/file.txt", target_language = "fr", source_language = "en", overwrite = TRUE) ## End(Not run)
## Not run: translate_file("path/to/file.txt", target_language = "fr", source_language = "en", overwrite = TRUE) ## End(Not run)
This function takes a string of text as input and translates it to Morse code using the FunTranslations API.
translate_to_morse(text, api_key = NULL)
translate_to_morse(text, api_key = NULL)
text |
A character string containing the text to be translated to Morse code. |
api_key |
(optional) Your FunTranslations API key, if you have a paid subscription. |
A list containing the translated Morse code text and other metadata.
This function takes an English text string as input and translates it to Morse code with an audio output using the FunTranslations API.
translate_to_morse_audio(text, api_key = NULL)
translate_to_morse_audio(text, api_key = NULL)
text |
A character string containing the English text to be translated. |
api_key |
(optional) Your FunTranslations API key, if you have a paid subscription. |
A list containing the translated Morse code text, the Morse code audio as a base64-encoded string, and other metadata.
This function sends a POST request to the Wikimedia Language ID API with the specified text, parses the JSON response, and returns the detected language.
wikimedia_detect_language(text)
wikimedia_detect_language(text)
text |
The text whose language is to be detected. |
The detected language.
# Detect the language of a text wikimedia_detect_language("Hallo, wereld")
# Detect the language of a text wikimedia_detect_language("Hallo, wereld")
This function sends a GET request to the Wikipedia API and returns the language names as a dataframe.
wikipedia_get_language_names()
wikipedia_get_language_names()
A dataframe of language names.
# Get language names wikipedia_get_language_names()
# Get language names wikipedia_get_language_names()
This function sends a POST request to the WMCloud translation API with the specified parameters, parses the JSON response, and returns the translated content.
wmcloud_translate( content, target_language = "en", source_language = "en", format = "text", model = "nllb200-600M" )
wmcloud_translate( content, target_language = "en", source_language = "en", format = "text", model = "nllb200-600M" )
content |
The content to translate. Can be plain text, a URL (for a webpage), a JSON string, or a Markdown string. |
target_language |
The target language for the translation (default is "en"). |
source_language |
The source language of the content (default is "en"). |
format |
The format of the content ("json", "markdown", "text", "webpage"). |
model |
The model to use for the translation (only "nllb200-600M" is currently known to work). |
The translated content.
## Not run: # Translate plain text wmcloud_translate("rijst", target_language = "es", source_language = "nl", format = "text") # Translate a webpage wmcloud_translate("https://en.m.wikivoyage.org/wiki/Goes", target_language = "es", source_language = "en", format = "webpage") # Translate JSON content wmcloud_translate('{ "id": 1, "title": "Chicken Biryani", "description": "Chicken Biryani is a savory chicken and rice dish", "ingredients": [ "Vegetable oil", "Garlic", "Ginger" ,"Rice"] } ', target_language = "es", source_language = "en", format = "json") # Translate Markdown content wmcloud_translate('# Heading This is a [link to Wikipedia](https://wikipedia.org) ', target_language = "es", source_language = "en", format = "markdown") ## End(Not run)
## Not run: # Translate plain text wmcloud_translate("rijst", target_language = "es", source_language = "nl", format = "text") # Translate a webpage wmcloud_translate("https://en.m.wikivoyage.org/wiki/Goes", target_language = "es", source_language = "en", format = "webpage") # Translate JSON content wmcloud_translate('{ "id": 1, "title": "Chicken Biryani", "description": "Chicken Biryani is a savory chicken and rice dish", "ingredients": [ "Vegetable oil", "Garlic", "Ginger" ,"Rice"] } ', target_language = "es", source_language = "en", format = "json") # Translate Markdown content wmcloud_translate('# Heading This is a [link to Wikipedia](https://wikipedia.org) ', target_language = "es", source_language = "en", format = "markdown") ## End(Not run)