Skip to content

Commit

Permalink
feat: QCRI methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomeriko96 committed Jan 27, 2024
1 parent c22f8f9 commit 24812d6
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export(linguee_translation_examples)
export(linguee_word_translation)
export(mymemory_translate)
export(pons_translate)
export(qcri_get_domains)
export(qcri_get_language_pairs)
export(qcri_translate_text)
export(translate_file)
export(wikimedia_detect_language)
export(wikipedia_get_language_names)
Expand Down
10 changes: 10 additions & 0 deletions R/qcri_api_key.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#' Get the QCRI API key from the environment variable
#'
#' @return The QCRI API key stored in the QCRI_API_KEY environment variable.
qcri_api_key <- function() {
api_key <- Sys.getenv("QCRI_API_KEY")
if (api_key == "") {
stop("QCRI_API_KEY environment variable is not set.")
}
return(api_key)
}
44 changes: 44 additions & 0 deletions R/qcri_get_domains.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#' QCRI Get Domains
#'
#' This function retrieves the supported domains from the QCRI Multiterm API.
#'
#' @param 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.
#'
#' @return 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.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' qcri_get_domains(api_key = "YourApiKey")
#' qcri_get_domains()
#' }
qcri_get_domains <- function(api_key = qcri_api_key()) {
# Get the API key
if (is.null(api_key)) {
api_key <- qcri_api_key()
}

# Set up the URL parameters
url_params <- list(key = api_key)

# Make the request
response <- httr::GET(
url = "https://mt.qcri.org/api/v1/getDomains",
query = url_params
)

# Check the status code
if (httr::status_code(response) >= 400) {
stop("Request failed with status code ", httr::status_code(response))
}

# Parse the response
content <- httr::content(response, "parsed")

# Return the response
return(content)
}
36 changes: 36 additions & 0 deletions R/qcri_get_language_pairs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#' QCRI Get Language Pairs
#'
#' This function retrieves the supported language pairs from the QCRI Multiterm API.
#'
#' @param 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
#'
#' @return Language pairs.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' qcri_get_language_pairs(api_key = "YourApiKey")
#' qcri_get_language_pairs()
#' }
qcri_get_language_pairs <- function(api_key = qcri_api_key()) {
# Get the API key
if (is.null(api_key)) {
api_key <- qcri_api_key()
}

# Set up the URL parameters
url_params <- list(key = api_key)

# Make the request
response <- httr::GET(
url = "https://mt.qcri.org/api/v1/getLanguagePairs",
query = url_params
)

# Parse the response
content <- httr::content(response, "parsed")

# Return the response
return(content)
}
49 changes: 49 additions & 0 deletions R/qcri_translate_text.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#' QCRI Translate Text
#'
#' This function translates a text from the source language to the target language using the QCRI Multiterm API.
#'
#' @param text The text to be translated. This must be URL encoded.
#' @param 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.
#' @param domain The domain over which the translation is tuned.
#' @param 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.
#'
#' @return Translated text.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' 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")
#' }
qcri_translate_text <- function(text, langpair, domain, api_key = qcri_api_key()) {
# Get the API key
if (is.null(api_key)) {
api_key <- qcri_api_key()
}

# Set up the URL parameters
url_params <- list(
key = api_key,
langpair = langpair,
domain = domain,
text = text
)

# Make the request
response <- httr::GET(
url = "https://mt.qcri.org/api/v1/translate",
query = url_params
)

# Parse the response
content <- httr::content(response, "parsed")

# Return the response
return(content$translatedText)
}
8 changes: 8 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ reference:
contents:
- pons_translate

- title: QCRI Methods
desc: Methods using QCRI services
contents:
- qcri_api_key
- qcri_get_domains
- qcri_get_language_pairs
- qcri_translate_text

- title: Wikimedia Foundation Methods
desc: Methods using Wikimedia Foundation Translation services
contents:
Expand Down
14 changes: 14 additions & 0 deletions man/qcri_api_key.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions man/qcri_get_domains.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/qcri_get_language_pairs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions man/qcri_translate_text.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 24812d6

Please sign in to comment.