Skip to content

Commit

Permalink
feat: linguee translate words
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomeriko96 committed Jun 22, 2023
1 parent 9960229 commit 6f610f2
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export("%>%")
export(create_translation_table)
export(google_translate)
export(language_detect)
export(linguee_word_translation)
export(mymemory_translate)
export(translate_file)
importFrom(magrittr,"%>%")
Expand Down
46 changes: 46 additions & 0 deletions R/linguee_word_translation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' Translate word using Linguee Translation API
#'
#' @param word This is the word that you want to translate.
#' @param target_language This is the language that you want to translate the word into.
#' @param source_language This is the language of the word that you want to translate.
#' @param guess_direction Specifies whether the API should guess the translation direction when the source language is set to "auto".
#' The default value is FALSE.
#' @param follow_corrections Specifies whether the API should include translations that have been marked as corrections.
#' The default value is "always" to include corrections.
#'
#' @return Translated word options.
#' @export
#'
#' @examples
#'\donttest{
#'linguee_word_translation("hello", target_language = "es", source_language = "en")
#'}
linguee_word_translation <- function(word, target_language, source_language, guess_direction = FALSE, follow_corrections = "always") {
api_root <- "https://linguee-api.fly.dev/api/v2"
endpoint <- paste0(api_root, "/translations")

params <- list(
query = word,
src = source_language,
dst = target_language,
guess_direction = tolower(guess_direction),
follow_corrections = follow_corrections
)

response <- httr::GET(url = endpoint, query = params)
if (response$status_code != 200) {
stop("Error: API request failed with status code ", response$status_code)
}

translation_data <- httr::content(response, "parsed")

translated_options <- character()

for (lemma in translation_data) {
for (translation in lemma$translations) {
translated_options <- append(translated_options, translation$text)
}
}

return(translated_options)
}
38 changes: 38 additions & 0 deletions man/linguee_word_translation.Rd

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

0 comments on commit 6f610f2

Please sign in to comment.