From 904fbeab4123418a9d8dcb64b05825ff6f337887 Mon Sep 17 00:00:00 2001 From: Aleksander Dietrichson Date: Thu, 1 Jun 2023 18:02:01 +0000 Subject: [PATCH 01/12] 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 +} From 72c7c84380e559db3c5fafeea9ed3a0e050099dd Mon Sep 17 00:00:00 2001 From: Aleksander Dietrichson Date: Thu, 1 Jun 2023 18:42:55 +0000 Subject: [PATCH 02/12] Added support for translating documents --- NAMESPACE | 2 ++ R/translate-document.R | 49 +++++++---------------------------- man/gl_translate_languages.Rd | 1 + 3 files changed, 12 insertions(+), 40 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 0d6fb94..8050384 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -17,6 +17,8 @@ export(gl_translate_document) export(gl_translate_languages) import(assertthat) import(base64enc) +importFrom(base64enc,base64decode) +importFrom(base64enc,base64encode) importFrom(googleAuthR,gar_api_generator) importFrom(googleAuthR,gar_attach_auto_auth) importFrom(googleAuthR,gar_auth_service) diff --git a/R/translate-document.R b/R/translate-document.R index 23916dc..477f39e 100644 --- a/R/translate-document.R +++ b/R/translate-document.R @@ -5,12 +5,14 @@ #' #' @param d_path path of the document to be translated -#' @param out_path where to save the translated document +#' @param output_path where to save the translated document #' @param format currently only pdf-files are supported #' #' @return invisible() on success #' @family translations #' @import assertthat +#' @importFrom base64enc base64encode +#' @importFrom base64enc base64decode #' @importFrom utils URLencode #' @importFrom googleAuthR gar_api_generator #' @importFrom tibble as_tibble @@ -18,7 +20,7 @@ #' @export gl_translate_document <- function(d_path, target = "es-ES", - out_path = "out.pdf", + output_path = "out.pdf", format = c("pdf"), source = 'en-UK', model = c("nmt", "base"), @@ -27,7 +29,7 @@ gl_translate_document <- function(d_path, ## Checks assert_that(is.character(d_path), - is.character(out_path), + is.character(output_path), is.string(target), is.string(source)) @@ -61,45 +63,12 @@ gl_translate_document <- function(d_path, "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 + me$content$documentTranslation[[1]] %>% + base64decode() %>% + writeBin(output_path) + } diff --git a/man/gl_translate_languages.Rd b/man/gl_translate_languages.Rd index 00dd10b..0651593 100644 --- a/man/gl_translate_languages.Rd +++ b/man/gl_translate_languages.Rd @@ -36,6 +36,7 @@ gl_translate_languages("da") Other translations: \code{\link{gl_translate_detect}()}, +\code{\link{gl_translate_document}()}, \code{\link{gl_translate}()} } \concept{translations} From acbbcff310780208547676cbc005cbaf111f7373 Mon Sep 17 00:00:00 2001 From: Aleksander Dietrichson Date: Thu, 1 Jun 2023 18:46:07 +0000 Subject: [PATCH 03/12] added myself to contributors' list --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 8934ed3..fa25b9f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,8 @@ Package: googleLanguageR Title: Call Google's 'Natural Language' API, 'Cloud Translation' API, 'Cloud Speech' API and 'Cloud Text-to-Speech' API Version: 0.3.0.9000 -Authors@R: c(person("Mark", "Edmondson", email = "r@sunholo.com", role = c("aut", "cre")), +Authors@R: c(person("Aleksander", "Dietrichson",email = "dietrichson@gmail.com", role=c("ctb")), + person("Mark", "Edmondson", email = "r@sunholo.com", role = c("aut", "cre")), person("John", "Muschelli", email = "muschellij2@gmail.com", role = c("ctb")), person("Neal", "Richardson", email = "neal.p.richardson@gmail.com", role = "rev", comment = "Neal reviewed the package for ropensci, From 31d105e2525ca9a0bfca9850bbcb6a616a2274ad Mon Sep 17 00:00:00 2001 From: Aleksander Dietrichson Date: Thu, 1 Jun 2023 19:20:22 +0000 Subject: [PATCH 04/12] removed dplyr dependency --- R/translate-document.R | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/R/translate-document.R b/R/translate-document.R index 477f39e..ae5b81d 100644 --- a/R/translate-document.R +++ b/R/translate-document.R @@ -59,16 +59,14 @@ gl_translate_document <- function(d_path, location,":translateDocument") - call_api <- gar_api_generator(my_URI, - "POST" - ) + call_api <- gar_api_generator(my_URI, "POST" ) - me <- tryCatch(call_api(the_body = payload)) + me <- tryCatch(call_api(the_body = payload), error=function(e){print(e)}) - - me$content$documentTranslation[[1]] %>% - base64decode() %>% - writeBin(output_path) + writeBin( + base64decode( + me$content$documentTranslation[[1]] + ), output_path) } From ffd317e113474b79c00aa8e8158496efe66b1deb Mon Sep 17 00:00:00 2001 From: Aleksander Dietrichson Date: Thu, 1 Jun 2023 19:21:56 +0000 Subject: [PATCH 05/12] added test doc --- inst/test-doc.pdf | Bin 0 -> 5934 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 inst/test-doc.pdf diff --git a/inst/test-doc.pdf b/inst/test-doc.pdf new file mode 100644 index 0000000000000000000000000000000000000000..c14729dd3e01bdacaa3fb6ec0e701b98b4085987 GIT binary patch literal 5934 zcmb7|Wl$VilZJ5(8Z^OO!wl{&1A*WkWN;XqLBc?QFt`&Of(&j61Pu_}2?R-S4K87j z;I{YM-Kv|f?ybAu{_7v7`c%KC>*@10tCpfNFYuWV9_z-@&r3Wp1^|Pbl@p$%B%iXQ zGt}0TPuUp^wN-fSlUUn_3xu72b*R;)O=vP*tAK9yZ9u3E$9q&FI zX@jqZ7~pmYEtn;^V zBJ!8V$L7XY0+)VVcP{Qjn%Hiv8k#~Kx4{-^tN=B<@eGz&phm9#7I2|YG%0(K>hQ<8Ieys=nt31+GPa+43%T_|xU5yO*eu;TQN5Bc>&^ z7e;_UJKi@pbswb1`u6R$`RA^duk-1w0;~CD&_WujtCZu2d2&iOXU}H0J{k}Y5C-2! z;rf221~*Xdh4XnGjNk~ywe{@z^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F$G7T<5>98|ImBKl(?kV|F&T?^IPt3hzc$#RSU4&w6Ns2} zi=(4R~Vy()YR$WNr#cs;&| z`>o@3@%I^QQwnm=Y0kbgc<8(8Ql|6z+&N@qF1s+N#^EoPHV_7wM{gs7DUDdQ-^xNc z;J;+k!dVeeGpr>MCPjFh z*7EFZnG;{Dc}yf)#wGg6=4#eraceW9B#QsKdGJ8IsLFL@>6EZ{E}GW+u$HUHus5!N z5}BGjvSfpf8KwJbYz^dTHdEb74XV;MWNSl=6mKvx;9pdT)}qVHw6XTv(P=*{n@Y+&dvH8~RJMLA~`rng=UGRNOj zq8`i}Tva;H1LAo5fTG{1;P9R0)nE_|Mv+qK3zwDTReU- zK0{OBAF27x{((rMBEJKPb$Ey*n1Kvqg1Q$|i-agEe2zNCgDQ%IQTO4bf`}mBAOg$`=2HLw literal 0 HcmV?d00001 From a741ee5734ad0dcf6d47264bda2af73085a9254e Mon Sep 17 00:00:00 2001 From: Aleksander Dietrichson Date: Thu, 1 Jun 2023 19:23:04 +0000 Subject: [PATCH 06/12] added stop if output file exists --- R/translate-document.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/translate-document.R b/R/translate-document.R index ae5b81d..dca9000 100644 --- a/R/translate-document.R +++ b/R/translate-document.R @@ -38,6 +38,8 @@ gl_translate_document <- function(d_path, format <- paste0("application/",format) + if(file.exists(output_path)) stop("Output file already exists.") + payload <- list( target_language_code = target, From c01bf531546d7f616aa2a2305f621327267a3841 Mon Sep 17 00:00:00 2001 From: Aleksander Dietrichson Date: Thu, 1 Jun 2023 19:33:32 +0000 Subject: [PATCH 07/12] added example --- R/translate-document.R | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/R/translate-document.R b/R/translate-document.R index dca9000..a6c08a8 100644 --- a/R/translate-document.R +++ b/R/translate-document.R @@ -8,7 +8,7 @@ #' @param output_path where to save the translated document #' @param format currently only pdf-files are supported #' -#' @return invisible() on success +#' @return output filename #' @family translations #' @import assertthat #' @importFrom base64enc base64encode @@ -18,6 +18,13 @@ #' @importFrom tibble as_tibble #' @importFrom stats setNames #' @export +#' + #' @examples +#' +#' \dontrun{ +#' gl_translate_document(system.file(package = "googleLanguageR","test-doc.pdf"), "no") +#' +#' } gl_translate_document <- function(d_path, target = "es-ES", output_path = "out.pdf", @@ -70,5 +77,7 @@ gl_translate_document <- function(d_path, me$content$documentTranslation[[1]] ), output_path) + path.expand(output_path) + } From 5cbdc0bb16de9ddfaa684173ee13dfb8c8703f1e Mon Sep 17 00:00:00 2001 From: Aleksander Dietrichson Date: Thu, 1 Jun 2023 19:34:13 +0000 Subject: [PATCH 08/12] Documentation updated --- man/gl_translate_document.Rd | 37 +++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/man/gl_translate_document.Rd b/man/gl_translate_document.Rd index 6e79e11..8e07365 100644 --- a/man/gl_translate_document.Rd +++ b/man/gl_translate_document.Rd @@ -1,28 +1,43 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/translate-documents.R +% Please edit documentation in R/translate-document.R \name{gl_translate_document} \alias{gl_translate_document} \title{Translate document} \usage{ gl_translate_document( d_path, - target = "en", + target = "es-ES", + output_path = "out.pdf", format = c("pdf"), - source = "", - model = c("nmt", "base") + source = "en-UK", + model = c("nmt", "base"), + location = "global" ) } \arguments{ -\item{d_path}{path to the document} +\item{d_path}{path of the document to be translated} -\item{target}{The target language} +\item{output_path}{where to save the translated document} -\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} +\item{format}{currently only pdf-files are supported} +} +\value{ +invisible() on success } \description{ Translate a document via the Google Translate API } +\examples{ + +\dontrun{ +gl_translate_document(system.file(package = "googleLanguageR","test-doc.pdf"), "no") + +} +} +\seealso{ +Other translations: +\code{\link{gl_translate_detect}()}, +\code{\link{gl_translate_languages}()}, +\code{\link{gl_translate}()} +} +\concept{translations} From b64b5107ee935ad3862fbd5e8b8e21357879734d Mon Sep 17 00:00:00 2001 From: Aleksander Dietrichson Date: Thu, 1 Jun 2023 19:34:51 +0000 Subject: [PATCH 09/12] changed return value --- man/gl_translate_document.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/gl_translate_document.Rd b/man/gl_translate_document.Rd index 8e07365..46b6fa4 100644 --- a/man/gl_translate_document.Rd +++ b/man/gl_translate_document.Rd @@ -22,7 +22,7 @@ gl_translate_document( \item{format}{currently only pdf-files are supported} } \value{ -invisible() on success +output filename } \description{ Translate a document via the Google Translate API From b29726469b8441fd03fa1d3906a2c84ce18a63af Mon Sep 17 00:00:00 2001 From: Aleksander Dietrichson Date: Mon, 5 Jun 2023 22:11:37 +0000 Subject: [PATCH 10/12] added test --- inst/test-doc-no.pdf | Bin 0 -> 36019 bytes tests/testthat/test-translate-document.R | 13 +++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 inst/test-doc-no.pdf create mode 100644 tests/testthat/test-translate-document.R diff --git a/inst/test-doc-no.pdf b/inst/test-doc-no.pdf new file mode 100644 index 0000000000000000000000000000000000000000..8a458a84c8a8f389f0387e5f174292d26eafd909 GIT binary patch literal 36019 zcmeI5cTg117VlA%oUnlmC^;v| zl5>=t-l9M6_4m8?rFvEGuN{h+>C>lApZ@gB6jXhyn?*$m$_e7)$H!rLTh=myj{^d5 z1K`G1_&6dWKoui%m?MA(X;B3L)!m-LfRaWCBOAE6m>50|%+B)CGX9VyOc3e>MFX?9YbaKN~v0j1ZP^J1HXsj7>_2hnok?4dLeF0r5e2dD(%I zSJiL_He|AvPPXiSM&bP<$^#=u7!+=Y0LsAO<~A@jBRfYdivP!k~d)9=qLuHya9LfOF-=71a;+wY;V1Ccl^9T5(0Y!b-XgRvt=`t+#{%oaIv z0Qa9^t`hhiM&gPb0w4(CMb7`zhcHWXi(jQ*Oc4N19svjtYGY*X2mtc{uQGk`0Pd;_ z-~@9c>%cq!K^}epKbS}VXC2hi2F3#r_%*^SXC)(B*ngWcS>&^3X(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b9U3O#iPQ| zW-=mV?tee7tXISO`*CF@{!i-|8Xo#JWH0E%XyRxM&;Q;ARYDO!5kL_@5kL_@5kL_@ z5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@ z5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@ z5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@5kL_@ z5kL_@5kL_@5%~W@;PTslU*`U5UHN}43UA~HgCdu@2R?+^IKvQ@CPthO;5Mc}X*(0R zsimDca&dM$2|Gv2f0fmoj1g{6f34sS`n8VxRr|lKB!9Jt{r|j!|F_sztMUJi{nzUA zP#feD_=IlUg!`!}rBKIyXrZ{Ebc z88Pfzyu~XUnif-uEgpXwCzv#e_kCMT{PUA_{`=eUU(~FGYqX1WUv^=xr;!|z(={C~ z;Pcen(W-5-s!TjtqKzJV6;*Rs(qr$%W?ZR1EC9Pi>iai;mGnpPde@dN< ztx)kMIP;!{+TI`oAbzhr&ZMiBkm2hLF20k3iS4i~txavuQ4<@j`5~Eak!BS`6JK&I*{9O@PYXNS2cefAb!BlJAmE4ZOH93{%Hg8AU7nqI{xqj z3I3-IjNI1apMJa$rms55z41{%3wj z8*+P)zxM&=6$A!{{!zcIv1cE*g h``>%$U%lFn2qOo?uL%Yt_h%8n$6;ZSR)*r^{11xlKpg-8 literal 0 HcmV?d00001 diff --git a/tests/testthat/test-translate-document.R b/tests/testthat/test-translate-document.R new file mode 100644 index 0000000..90f63b0 --- /dev/null +++ b/tests/testthat/test-translate-document.R @@ -0,0 +1,13 @@ +source("prep_tests.R") +test_that("document tranlation works", { + skip_on_cran() + skip_on_travis() + + my_out <- tempfile(fileext = ".pdf") + + gl_translate_document(system.file(package = "googleLanguageR","test-doc.pdf"), target = "no",output_path = my_out) + + my_file1 <- readBin(my_out, "raw") + my_file2 <- readBin(system.file(package = "googleLanguageR","test-doc.pdf"), "raw") + expect_equal(my_file1, my_file2) +}) From afd3b2a14673403f376bc9c1e9d2dd5dd89d4169 Mon Sep 17 00:00:00 2001 From: Aleksander Dietrichson Date: Tue, 6 Jun 2023 11:06:02 +0000 Subject: [PATCH 11/12] added test based on pdftools --- tests/testthat/test-translate-document.R | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-translate-document.R b/tests/testthat/test-translate-document.R index 90f63b0..f5e2735 100644 --- a/tests/testthat/test-translate-document.R +++ b/tests/testthat/test-translate-document.R @@ -7,7 +7,11 @@ test_that("document tranlation works", { gl_translate_document(system.file(package = "googleLanguageR","test-doc.pdf"), target = "no",output_path = my_out) - my_file1 <- readBin(my_out, "raw") - my_file2 <- readBin(system.file(package = "googleLanguageR","test-doc.pdf"), "raw") - expect_equal(my_file1, my_file2) + my_pdf1 <-pdftools::pdf_data(my_out) + + my_pdf2 <- pdftools::pdf_data( + system.file(package = "googleLanguageR","test-doc-no.pdf") + ) + + expect_equal(my_pdf1[[1]]$text, my_pdf2[[1]]$text) }) From 80cac9456f0d7509fde04d30ba4305cbc35567d5 Mon Sep 17 00:00:00 2001 From: Aleksander Dietrichson Date: Tue, 6 Jun 2023 11:07:22 +0000 Subject: [PATCH 12/12] Added dependency --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index fa25b9f..9736c78 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -38,6 +38,7 @@ Imports: tibble, utils Suggests: + pdftools, cld2, testthat, knitr,