Skip to content

Commit

Permalink
feat: add functions and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tin900 committed Sep 2, 2022
1 parent f82594e commit 793d6a2
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
^LICENSE\.md$
^.*\.Rproj$
^\.Rproj\.user$
^build_package\.R$
^.*.pdf$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ vignettes/*.pdf

# R Environment Variables
.Renviron
*.pdf
18 changes: 18 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", 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
23 changes: 2 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(google_translate)
export(mymemory_translate)
importFrom(magrittr,"%>%")
30 changes: 30 additions & 0 deletions R/google_translate.R
Original file line number Diff line number Diff line change
@@ -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)

}
31 changes: 31 additions & 0 deletions R/mymemory_translate.R
Original file line number Diff line number Diff line change
@@ -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)

}
14 changes: 14 additions & 0 deletions R/utils-pipe.R
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions build_package.R
Original file line number Diff line number Diff line change
@@ -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()
26 changes: 26 additions & 0 deletions man/google_translate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions man/mymemory_translate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/pipe.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions polyglotr.Rproj
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -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")
5 changes: 5 additions & 0 deletions tests/testthat/test-google_translate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test_that("translation works", {
expect_equal(google_translate("hello world",
target_language = "es"),
"Hola Mundo")
})
6 changes: 6 additions & 0 deletions tests/testthat/test-mymemory_translate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test_that("translation workds", {
expect_equal(mymemory_translate(text = "Hello World",
target_language = "es",
source_language = "en"),
"Hola Mundo")
})

0 comments on commit 793d6a2

Please sign in to comment.