Skip to content

Commit

Permalink
feat: get linguee api external sources
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomeriko96 committed Aug 17, 2023
1 parent 5457e7a commit ab19e2c
Show file tree
Hide file tree
Showing 3 changed files with 75 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_external_sources)
export(linguee_translation_examples)
export(linguee_word_translation)
export(mymemory_translate)
Expand Down
42 changes: 42 additions & 0 deletions R/linguee_external_sources.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#' Retrieve external sources using Linguee Translation API
#'
#' @param query The word or phrase for which you want to retrieve external sources.
#' @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 external source retrieval. Accepts language codes such as "en", "es", "fr", etc.
#' @param limit The maximum number of external sources to retrieve. Defaults to 5.
#'
#' @return A dataframe of external sources with columns: src, dst, src_url, dst_url.
#'
#' @examples
#' \donttest{
#' linguee_external_sources(query = "hello", src = "en", dst = "es")
#' }
#'
#' @seealso linguee_word_translation, linguee_translation_examples
#'
#'
#' @export
linguee_external_sources <- function(query, src, dst, limit = 5) {
api_root <- "https://linguee-api.fly.dev/api/v2/external_sources"
endpoint <- api_root

params <- list(
query = query,
src = src,
dst = dst,
limit = limit
)

response <- httr::GET(url = endpoint, query = params)

if (response$status_code != 200) {
stop("Error: API request failed with status code ", response$status_code)
}

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

# Combine the individual lists into a single dataframe
sources_df <- dplyr::bind_rows(external_sources)

return(sources_df)
}
32 changes: 32 additions & 0 deletions man/linguee_external_sources.Rd

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

0 comments on commit ab19e2c

Please sign in to comment.