Skip to content

Commit

Permalink
feat: get linguee api examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomeriko96 committed Aug 17, 2023
1 parent e9947ab commit 5457e7a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Imports:
rlang,
rvest,
stringr,
tibble,
urltools
Suggests:
knitr,
Expand Down
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_translation_examples)
export(linguee_word_translation)
export(mymemory_translate)
export(translate_file)
Expand Down
51 changes: 51 additions & 0 deletions R/linguee_translation_examples.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#' Provide translation examples using Linguee Translation API
#'
#' @param query The word or phrase for which you want translation examples.
#' @param src The source language of the word or phrase. Accepts language codes such as "en", "es", "fr", etc.
#' @param dst The target language for the translation examples. Accepts language codes such as "en", "es", "fr", etc.
#' @param guess_direction A boolean flag that determines whether the API should guess the translation direction.
#' The default value is FALSE.
#' @param 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".
#'
#' @return A dataframe of translation examples with columns: source, target, pos.
#'
#' @examples
#' \donttest{
#' linguee_translation_examples(query = "hello", src = "en", dst = "es")
#' }
#'
#' @seealso linguee_word_translation
#'
#' @export
linguee_translation_examples <- function(query, src, dst, guess_direction = FALSE, follow_corrections = "always") {
api_root <- "https://linguee-api.fly.dev/api/v2/examples"
endpoint <- api_root

params <- list(
query = query,
src = src,
dst = dst,
guess_direction = 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_examples <- httr::content(response, "parsed")

# Extract the relevant information from the translation examples
examples_df <- purrr::map_dfr(translation_examples, function(example) {
tibble::tibble(
source = example$text,
target = example$translations[[1]]$text,
pos = example$translations[[1]]$pos
)
})

return(examples_df)
}
43 changes: 43 additions & 0 deletions man/linguee_translation_examples.Rd

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

0 comments on commit 5457e7a

Please sign in to comment.