From 904fbeab4123418a9d8dcb64b05825ff6f337887 Mon Sep 17 00:00:00 2001 From: Aleksander Dietrichson Date: Thu, 1 Jun 2023 18:02:01 +0000 Subject: [PATCH] Initial try [#84] --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/translate-document.R | 105 +++++++++++++++++++++++++++++++++++ man/gl_translate_document.Rd | 28 ++++++++++ 4 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 R/translate-document.R create mode 100644 man/gl_translate_document.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 60e7191..8934ed3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -24,7 +24,7 @@ Depends: R (>= 3.3) License: MIT + file LICENSE Encoding: UTF-8 LazyData: true -RoxygenNote: 7.1.0 +RoxygenNote: 7.2.3 VignetteBuilder: knitr Imports: assertthat, diff --git a/NAMESPACE b/NAMESPACE index 2c6b54e..0d6fb94 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -13,6 +13,7 @@ export(gl_talk_shiny) export(gl_talk_shinyUI) export(gl_translate) export(gl_translate_detect) +export(gl_translate_document) export(gl_translate_languages) import(assertthat) import(base64enc) diff --git a/R/translate-document.R b/R/translate-document.R new file mode 100644 index 0000000..23916dc --- /dev/null +++ b/R/translate-document.R @@ -0,0 +1,105 @@ +#' Translate document +#' +#' Translate a document via the Google Translate API +#' + +#' +#' @param d_path path of the document to be translated +#' @param out_path where to save the translated document +#' @param format currently only pdf-files are supported +#' +#' @return invisible() on success +#' @family translations +#' @import assertthat +#' @importFrom utils URLencode +#' @importFrom googleAuthR gar_api_generator +#' @importFrom tibble as_tibble +#' @importFrom stats setNames +#' @export +gl_translate_document <- function(d_path, + target = "es-ES", + out_path = "out.pdf", + format = c("pdf"), + source = 'en-UK', + model = c("nmt", "base"), + + location = "global"){ + + ## Checks + assert_that(is.character(d_path), + is.character(out_path), + is.string(target), + is.string(source)) + + format <- match.arg(format) + model <- match.arg(model) + + format <- paste0("application/",format) + + payload <- + list( + target_language_code = target, + source_language_code = source, + document_input_config = list( + mimeType = format, + content = base64encode(d_path) + ) + ) + + + project_id <- gar_token()$auth_token$secrets$project_id + LOCATION <- location + + my_URI <- paste0( + "https://translation.googleapis.com/v3beta1/projects/", + project_id, + "/locations/", + location,":translateDocument") + + + call_api <- gar_api_generator(my_URI, + "POST" + ) + + + + me <- tryCatch(call_api(the_body = payload)) + # me <- tryCatch(call_api(the_body = pars), + # error = function(ex){ + # if(grepl(catch_errors, + # ex$message)){ + # my_message(ex$message, level = 3) + # my_message("Attempting to split into several API calls", level = 3) + # # Reduce(rbind, lapply(t_string, gl_translate, + # # format = format, + # # target = target, + # # source = source, + # # model = model)) + # } else if(grepl("User Rate Limit Exceeded", + # ex$message)){ + # my_message("Characters per 100 seconds rate limit reached, waiting 10 seconds...", + # level = 3) + # Sys.sleep(10) + # ## try again + # gl_translate(t_string, + # format = format, + # target = target, + # source = source, + # model = model) + # } else { + # stop(ex$message, call. = FALSE) + # } + # }) + # + me + +} + +if(interactive()){ + library(googleAuthR) + gl_auth("_api-keys/google_api_key.json") -> my_test + library(dplyr) + library(assertthat) + gl_translate_document("./notes/test-doc.pdf") %>% print +} + diff --git a/man/gl_translate_document.Rd b/man/gl_translate_document.Rd new file mode 100644 index 0000000..6e79e11 --- /dev/null +++ b/man/gl_translate_document.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/translate-documents.R +\name{gl_translate_document} +\alias{gl_translate_document} +\title{Translate document} +\usage{ +gl_translate_document( + d_path, + target = "en", + format = c("pdf"), + source = "", + model = c("nmt", "base") +) +} +\arguments{ +\item{d_path}{path to the document} + +\item{target}{The target language} + +\item{format}{Whether the text is plain or HTML} + +\item{source}{Specify the language to translate from. Will detect it if left default} + +\item{model}{What translation model to use} +} +\description{ +Translate a document via the Google Translate API +}