-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from dietrichson/master
- Loading branch information
Showing
8 changed files
with
151 additions
and
2 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 |
---|---|---|
|
@@ -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 = "[email protected]", role = c("aut", "cre")), | ||
Authors@R: c(person("Aleksander", "Dietrichson",email = "[email protected]", role=c("ctb")), | ||
person("Mark", "Edmondson", email = "[email protected]", role = c("aut", "cre")), | ||
person("John", "Muschelli", email = "[email protected]", role = c("ctb")), | ||
person("Neal", "Richardson", email = "[email protected]", role = "rev", | ||
comment = "Neal reviewed the package for ropensci, | ||
|
@@ -24,7 +25,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, | ||
|
@@ -37,6 +38,7 @@ Imports: | |
tibble, | ||
utils | ||
Suggests: | ||
pdftools, | ||
cld2, | ||
testthat, | ||
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,83 @@ | ||
#' Translate document | ||
#' | ||
#' Translate a document via the Google Translate API | ||
#' | ||
|
||
#' | ||
#' @param d_path path of the document to be translated | ||
#' @param output_path where to save the translated document | ||
#' @param format currently only pdf-files are supported | ||
#' | ||
#' @return output filename | ||
#' @family translations | ||
#' @import assertthat | ||
#' @importFrom base64enc base64encode | ||
#' @importFrom base64enc base64decode | ||
#' @importFrom utils URLencode | ||
#' @importFrom googleAuthR gar_api_generator | ||
#' @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", | ||
format = c("pdf"), | ||
source = 'en-UK', | ||
model = c("nmt", "base"), | ||
|
||
location = "global"){ | ||
|
||
## Checks | ||
assert_that(is.character(d_path), | ||
is.character(output_path), | ||
is.string(target), | ||
is.string(source)) | ||
|
||
format <- match.arg(format) | ||
model <- match.arg(model) | ||
|
||
format <- paste0("application/",format) | ||
|
||
if(file.exists(output_path)) stop("Output file already exists.") | ||
|
||
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), error=function(e){print(e)}) | ||
|
||
writeBin( | ||
base64decode( | ||
me$content$documentTranslation[[1]] | ||
), output_path) | ||
|
||
path.expand(output_path) | ||
|
||
} | ||
|
Binary file not shown.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,17 @@ | ||
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_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) | ||
}) |