From ab19e2cea3ca4f8675ec7b2762e29affdc6d582d Mon Sep 17 00:00:00 2001 From: Tomer Iwan Date: Thu, 17 Aug 2023 09:54:32 +0200 Subject: [PATCH] feat: get linguee api external sources --- NAMESPACE | 1 + R/linguee_external_sources.R | 42 +++++++++++++++++++++++++++++++++ man/linguee_external_sources.Rd | 32 +++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 R/linguee_external_sources.R create mode 100644 man/linguee_external_sources.Rd diff --git a/NAMESPACE b/NAMESPACE index febdc1f..e477e86 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/linguee_external_sources.R b/R/linguee_external_sources.R new file mode 100644 index 0000000..e7fbdac --- /dev/null +++ b/R/linguee_external_sources.R @@ -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) +} diff --git a/man/linguee_external_sources.Rd b/man/linguee_external_sources.Rd new file mode 100644 index 0000000..c4e38b1 --- /dev/null +++ b/man/linguee_external_sources.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/linguee_external_sources.R +\name{linguee_external_sources} +\alias{linguee_external_sources} +\title{Retrieve external sources using Linguee Translation API} +\usage{ +linguee_external_sources(query, src, dst, limit = 5) +} +\arguments{ +\item{query}{The word or phrase for which you want to retrieve external sources.} + +\item{src}{The source language of the word or phrase. Accepts language codes such as "en", "es", "fr", etc.} + +\item{dst}{The target language for the external source retrieval. Accepts language codes such as "en", "es", "fr", etc.} + +\item{limit}{The maximum number of external sources to retrieve. Defaults to 5.} +} +\value{ +A dataframe of external sources with columns: src, dst, src_url, dst_url. +} +\description{ +Retrieve external sources using Linguee Translation API +} +\examples{ +\donttest{ +linguee_external_sources(query = "hello", src = "en", dst = "es") +} + +} +\seealso{ +linguee_word_translation, linguee_translation_examples +}