-
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
e9947ab
commit 5457e7a
Showing
4 changed files
with
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ Imports: | |
rlang, | ||
rvest, | ||
stringr, | ||
tibble, | ||
urltools | ||
Suggests: | ||
knitr, | ||
|
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,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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.