diff --git a/.Rbuildignore b/.Rbuildignore new file mode 100644 index 0000000..b7be815 --- /dev/null +++ b/.Rbuildignore @@ -0,0 +1,5 @@ +^LICENSE\.md$ +^.*\.Rproj$ +^\.Rproj\.user$ +^build_package\.R$ +^.*.pdf$ diff --git a/.gitignore b/.gitignore index fae8299..f55b249 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ vignettes/*.pdf # R Environment Variables .Renviron +*.pdf diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..cec6ce6 --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,18 @@ +Package: polyglotr +Title: What the Package Does (One Line, Title Case) +Version: 0.0.0.9000 +Authors@R: + person("Tomer", "Iwan", , "t.iwan@vu.nl", role = c("aut", "cre")) +Description: What the package does (one paragraph). +License: MIT + file LICENSE +Encoding: UTF-8 +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.2.1 +Imports: + httr, + magrittr, + rvest, + stringr +Suggests: + testthat (>= 3.0.0) +Config/testthat/edition: 3 diff --git a/LICENSE b/LICENSE index b61a8ed..ca6303b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,2 @@ -MIT License - -Copyright (c) 2022 Tomeriko96 - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +YEAR: 2022 +COPYRIGHT HOLDER: polyglotr authors diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..e518af0 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# MIT License + +Copyright (c) 2022 polyglotr authors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000..89d72b1 --- /dev/null +++ b/NAMESPACE @@ -0,0 +1,6 @@ +# Generated by roxygen2: do not edit by hand + +export("%>%") +export(google_translate) +export(mymemory_translate) +importFrom(magrittr,"%>%") diff --git a/R/google_translate.R b/R/google_translate.R new file mode 100644 index 0000000..f1c7ab8 --- /dev/null +++ b/R/google_translate.R @@ -0,0 +1,30 @@ +#' Translate text using google translate +#' +#' @param text Text to translate. +#' @param target_language Language to translate text to. +#' @param source_language Language to translate text from +#' +#' @return Translated text. +#' @export +#' +#' @examples +#'\dontrun{ +#'google_translate("I love languages", target_language = "es") +#'} +google_translate <- function(text, target_language = "en", source_language = "auto") { + formatted_text <- stringr::str_replace_all(text, " ", "%20") + + formatted_link <- paste0("https://translate.google.com/m?tl=", + target_language, "&sl=", source_language, + "&q=", + formatted_text) + + response <- httr::GET(formatted_link) + + translation <- httr::content(response) %>% + rvest::html_nodes("div.result-container") %>% + rvest::html_text() + + return(translation) + +} diff --git a/R/mymemory_translate.R b/R/mymemory_translate.R new file mode 100644 index 0000000..0e436dd --- /dev/null +++ b/R/mymemory_translate.R @@ -0,0 +1,31 @@ +#' Translate text using mymemory translate +#' +#' @param text Text to translate. +#' @param target_language Language to translate text to. +#' @param source_language Language to translate text from +#' +#' @return Translated text. +#' @export +#' +#' @examples +#'\dontrun{ +#'mymemory_translate("Hello World", target_language = "es", source_language = "en") +#'} +mymemory_translate <- function(text, target_language = "en", source_language = "auto") { + formatted_text <- stringr::str_replace_all(text, " ", "%20") + + formatted_link <- paste0("https://api.mymemory.translated.net/get?q=", + formatted_text, + "&langpair=", + source_language, + "|", + target_language) + + response <- httr::GET(formatted_link) %>% + httr::content() + + translation <- response$responseData$translatedText + + return(translation) + +} diff --git a/R/utils-pipe.R b/R/utils-pipe.R new file mode 100644 index 0000000..fd0b1d1 --- /dev/null +++ b/R/utils-pipe.R @@ -0,0 +1,14 @@ +#' Pipe operator +#' +#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. +#' +#' @name %>% +#' @rdname pipe +#' @keywords internal +#' @export +#' @importFrom magrittr %>% +#' @usage lhs \%>\% rhs +#' @param lhs A value or the magrittr placeholder. +#' @param rhs A function call using the magrittr semantics. +#' @return The result of calling `rhs(lhs)`. +NULL diff --git a/build_package.R b/build_package.R new file mode 100644 index 0000000..c4d9ab2 --- /dev/null +++ b/build_package.R @@ -0,0 +1,37 @@ +## Set project name +package_name <- basename(rstudioapi::getActiveProject()) +## =============================================== ============================== +## Install and load the packages +build_packages <- c("devtools", + "usethis", + "renv") + +## install the packages that are not installed yet +lapply(build_packages[which(!build_packages %in% installed.packages())], + install.packages) + + +## load the packages +invisible(lapply(build_packages, + library, + character.only = TRUE)) + + +## =============================================== ============================== +## Build the package + + +## Check if the package is correct and can be built. +devtools::document() +devtools::check(manual = T) +devtools::build_manual(path = ".") + + +## Increment the version number and check in that change in Git +usethis::use_version() + +## Build the package, and release it to the correct folder +devtools::build(path = paste0("G:/DSZ/SA2016/Datasets/Packages/", package_name, "/")) + +## Make use of the development version again +usethis::use_dev_version() diff --git a/man/google_translate.Rd b/man/google_translate.Rd new file mode 100644 index 0000000..150239b --- /dev/null +++ b/man/google_translate.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/google_translate.R +\name{google_translate} +\alias{google_translate} +\title{Translate text using google translate} +\usage{ +google_translate(text, target_language = "en", source_language = "auto") +} +\arguments{ +\item{text}{Text to translate.} + +\item{target_language}{Language to translate text to.} + +\item{source_language}{Language to translate text from} +} +\value{ +Translated text. +} +\description{ +Translate text using google translate +} +\examples{ +\dontrun{ +google_translate("I love languages", target_language = "es") +} +} diff --git a/man/mymemory_translate.Rd b/man/mymemory_translate.Rd new file mode 100644 index 0000000..6fa3817 --- /dev/null +++ b/man/mymemory_translate.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/mymemory_translate.R +\name{mymemory_translate} +\alias{mymemory_translate} +\title{Translate text using mymemory translate} +\usage{ +mymemory_translate(text, target_language = "en", source_language = "auto") +} +\arguments{ +\item{text}{Text to translate.} + +\item{target_language}{Language to translate text to.} + +\item{source_language}{Language to translate text from} +} +\value{ +Translated text. +} +\description{ +Translate text using mymemory translate +} +\examples{ +\dontrun{ +mymemory_translate("Hello World", target_language = "es", source_language = "en") +} +} diff --git a/man/pipe.Rd b/man/pipe.Rd new file mode 100644 index 0000000..a648c29 --- /dev/null +++ b/man/pipe.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils-pipe.R +\name{\%>\%} +\alias{\%>\%} +\title{Pipe operator} +\usage{ +lhs \%>\% rhs +} +\arguments{ +\item{lhs}{A value or the magrittr placeholder.} + +\item{rhs}{A function call using the magrittr semantics.} +} +\value{ +The result of calling \code{rhs(lhs)}. +} +\description{ +See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. +} +\keyword{internal} diff --git a/polyglotr.Rproj b/polyglotr.Rproj new file mode 100644 index 0000000..497f8bf --- /dev/null +++ b/polyglotr.Rproj @@ -0,0 +1,20 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +AutoAppendNewline: Yes +StripTrailingWhitespace: Yes + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..6690d30 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,12 @@ +# This file is part of the standard setup for testthat. +# It is recommended that you do not modify it. +# +# Where should you do additional test configuration? +# Learn more about the roles of various files in: +# * https://r-pkgs.org/tests.html +# * https://testthat.r-lib.org/reference/test_package.html#special-files + +library(testthat) +library(polyglotr) + +test_check("polyglotr") diff --git a/tests/testthat/test-google_translate.R b/tests/testthat/test-google_translate.R new file mode 100644 index 0000000..55e7084 --- /dev/null +++ b/tests/testthat/test-google_translate.R @@ -0,0 +1,5 @@ +test_that("translation works", { + expect_equal(google_translate("hello world", + target_language = "es"), + "Hola Mundo") +}) diff --git a/tests/testthat/test-mymemory_translate.R b/tests/testthat/test-mymemory_translate.R new file mode 100644 index 0000000..49fedd6 --- /dev/null +++ b/tests/testthat/test-mymemory_translate.R @@ -0,0 +1,6 @@ +test_that("translation workds", { + expect_equal(mymemory_translate(text = "Hello World", + target_language = "es", + source_language = "en"), + "Hola Mundo") +})