| Title: | Interface to 'TexTra' from R |
|---|---|
| Description: | A wrapper for the 'TexTra' API <https://mt-auto-minhon-mlt.ucri.jgn-x.jp/>, a web service for translating texts between different languages. 'TexTra' API account is required to use the service. |
| Authors: | Toshikazu Matsumura [aut, cre] (ORCID: <https://orcid.org/0000-0003-2306-6355>) |
| Maintainer: | Toshikazu Matsumura <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.9.0 |
| Built: | 2026-09-28 07:21:14 UTC |
| Source: | https://github.com/matutosi/textrar |
Return the base URL for the API
base_url()base_url()
A string containing the base URL.
base_url() is a low level building block that was never meant to be part
of the public interface. It is deprecated as of textrar 0.9.0 and will be
removed in a future release. If you rely on it, please open an issue at
https://github.com/matutosi/textrar/issues.
## Not run: base_url() ## End(Not run)## Not run: base_url() ## End(Not run)
This function extracts the translated text from the response.
returned by the post_request() function.
extract_result(res)extract_result(res)
res |
The response object returned by the |
A character string containing the translated text. An error is
raised when the API reports a failure. Note that the API answers with
HTTP 200 even when it fails, and reports the failure in
resultset$code, so the status code alone cannot be relied upon.
extract_result() is a low level building block that was never meant to
be part of the public interface. It is deprecated as of textrar 0.9.0 and
will be removed in a future release. Use textra() instead. If you rely
on it, please open an issue at
https://github.com/matutosi/textrar/issues.
## Not run: res <- post_request(params, "Hello world!") translated <- extract_result(res) ## End(Not run)## Not run: res <- post_request(params, "Hello world!") translated <- extract_result(res) ## End(Not run)
This function generates a list of parameters that can be used to make an API call.
gen_params( key = textra_env("TEXTRA_API_KEY"), secret = textra_env("TEXTRA_API_SECRET"), name = textra_env("TEXTRA_NAME"), api_name = "mt" )gen_params( key = textra_env("TEXTRA_API_KEY"), secret = textra_env("TEXTRA_API_SECRET"), name = textra_env("TEXTRA_NAME"), api_name = "mt" )
key |
The API key. Defaults to the environment variable
|
secret |
The API secret. Defaults to the environment variable
|
name |
The login ID of the 'TexTra' account. Defaults to the
environment variable |
api_name |
The name of the API to use. Defaults to "mt". |
Storing the credentials in the environment rather than in your scripts
keeps them out of version control and out of shared files. Add the
following lines to your user .Renviron file, which
usethis::edit_r_environ() opens, and restart R.
TEXTRA_API_KEY=your_api_key TEXTRA_API_SECRET=your_api_secret TEXTRA_NAME=your_login_id
A list of parameters.
## Not run: # Using the credentials stored in .Renviron (recommended). params <- gen_params() # Or passing them explicitly. key <- "abcdefghijklmnopqrstuvw01234567890abcdef1" # API key secret <- "xyzabcdefghijklmnopqrstuvw012345" # API secret name <- "login_ID" # login_ID params <- gen_params(key = key, secret = secret, name = name) ## End(Not run)## Not run: # Using the credentials stored in .Renviron (recommended). params <- gen_params() # Or passing them explicitly. key <- "abcdefghijklmnopqrstuvw01234567890abcdef1" # API key secret <- "xyzabcdefghijklmnopqrstuvw012345" # API secret name <- "login_ID" # login_ID params <- gen_params(key = key, secret = secret, name = name) ## End(Not run)
This function retrieves an access token from the API using the provided key and secret.
get_token( key = textra_env("TEXTRA_API_KEY"), secret = textra_env("TEXTRA_API_SECRET") )get_token( key = textra_env("TEXTRA_API_KEY"), secret = textra_env("TEXTRA_API_SECRET") )
key |
The API key. Defaults to the environment variable
|
secret |
The API secret. Defaults to the environment variable
|
A character string containing the access token. An error is raised when 'TexTra' rejects the credentials.
## Not run: # Using the credentials stored in .Renviron (recommended). token <- get_token() # Or passing them explicitly. key <- "abcdefghijklmnopqrstuvw01234567890abcdef1" # API key secret <- "xyzabcdefghijklmnopqrstuvw012345" # API secret token <- get_token(key = key, secret = secret) ## End(Not run)## Not run: # Using the credentials stored in .Renviron (recommended). token <- get_token() # Or passing them explicitly. key <- "abcdefghijklmnopqrstuvw01234567890abcdef1" # API key secret <- "xyzabcdefghijklmnopqrstuvw012345" # API secret token <- get_token(key = key, secret = secret) ## End(Not run)
This function sends a POST request to the API with the specified parameters and text.
post_request(params, text)post_request(params, text)
params |
A list of parameters to be passed to the API. |
text |
The text to be translated. |
The response from the API.
post_request() is a low level building block that was never meant to be
part of the public interface. It is deprecated as of textrar 0.9.0 and
will be removed in a future release. Use textra() instead. If you rely
on it to reach an endpoint that textra() does not cover, please open an
issue at https://github.com/matutosi/textrar/issues so that a supported
way of doing so can be provided.
## Not run: post_request(params, text = "Hello, world!") ## End(Not run)## Not run: post_request(params, text = "Hello, world!") ## End(Not run)
This function translates text with the 'TexTra' machine translation API.
textra(text, params, model = "transLM", from = "en", to = "ja")textra(text, params, model = "transLM", from = "en", to = "ja")
text |
The text to be translated. |
params |
A list of parameters to be passed to the API. |
model |
The model to be used for translation. |
from |
The source language. |
to |
The target language. |
The translated text.
## Not run: text <- "Hello world" key <- "abcdefghijklmnopqrstuvw01234567890abcdef1" # API key secret <- "xyzabcdefghijklmnopqrstuvw012345" # API secret name <- "login_ID" # login_ID params <- gen_params(key = key, secret = secret, name = name) translated <- textra(text, params, model = "transLM", from = "en", to = "ja") translated ## End(Not run)## Not run: text <- "Hello world" key <- "abcdefghijklmnopqrstuvw01234567890abcdef1" # API key secret <- "xyzabcdefghijklmnopqrstuvw012345" # API secret name <- "login_ID" # login_ID params <- gen_params(key = key, secret = secret, name = name) translated <- textra(text, params, model = "transLM", from = "en", to = "ja") translated ## End(Not run)