-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c22f8f9
commit 24812d6
Showing
10 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.