Skip to content

Commit

Permalink
baidu_translate
Browse files Browse the repository at this point in the history
  • Loading branch information
GuangchuangYu committed Dec 11, 2023
1 parent 946215d commit 73259d6
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 7 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Makefile
\.github
6 changes: 5 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: yulab.utils
Title: Supporting Functions for Packages Maintained by 'YuLab-SMU'
Version: 0.1.0.002
Version: 0.1.1
Authors@R: c(person("Guangchuang", "Yu", email = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6485-8781")))
Description: Miscellaneous functions commonly used by 'YuLab-SMU'.
Imports:
Expand All @@ -12,6 +12,10 @@ Imports:
stats,
tools,
utils
Suggests:
httr,
jsonlite,
openssl
ByteCompile: true
URL: https://yulab-smu.top/
License: Artistic-2.0
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ S3method(print,exec)
export(Biocpkg)
export(CRANpkg)
export(Githubpkg)
export(baidu_translate)
export(check_pkg)
export(combinations)
export(en2cn)
export(exec)
export(get_dependencies)
export(get_fun_from_pkg)
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

# yulab.utils 0.1.0.002
# yulab.utils 0.1.1

+ `baidu_translate()` to translate sentences (2023-12-11, Mon)
+ use `normalizePath()` in `o()` to convert file paths to canonical form (2023-10-06, Fri, #4)
+ change the default parameter, `ref = "master"` to `ref = "HEAD"` in the `install_zip_gh()` function to use the default branch of the GitHub repo (2023-10-02, Mon)

Expand Down
46 changes: 46 additions & 0 deletions R/baidu-translate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' @rdname baidu-translate
#' @export
en2cn <- function (x) {
baidu_translate(x, from = 'en', to = 'zh')
}


#' Translate query sentence
#'
#' This function use the Baidu fanyi API to translate query sentence
#' @title baidu_translate
#' @rdname baidu-translate
#' @param x query sentence
#' @param from source language, i.e., the language to be translated
#' @param to target language, i.e., the language to be translated into
#' @return the translated sentence
#' @author Guangchuang Yu
#' @export
baidu_translate <- function(x, from = 'en', to = 'zh') {
vapply(x, .baidu_translate, from = from, to = to, FUN.VALUE = character(1))
}

.baidu_translate <- function(x, from = 'en', to = 'zh') {
url <- httr::modify_url("http://api.fanyi.baidu.com/api/trans/vip/translate",
query = baidu_translate_query(x, from = from, to = to)
)
url <- url(url, encoding = "utf-8")
res <- jsonlite::fromJSON(url)

return(res$trans_result$dst)
}

baidu_translate_query <- function(x, from, to) {
salt <- sample.int(1e+05, 1)
.info <- getOption('.baidu_translate')
sign <- sprintf("%s%s%s%s", .info$appid, x, salt, .info$key)
.sign <- openssl::md5(sign)

.query <- list(q = x, from = from, to = to,
appid = .info$appid,
salt = salt,
sign = .sign
)
return(.query)
}

8 changes: 4 additions & 4 deletions R/pkg-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,28 +200,28 @@ get_fun_from_pkg <- function(pkg, fun) {
##' @return md text string
##' @export
##' @author Guangchuang Yu
CRANpkg <- function (pkg) {
CRANpkg <- function(pkg) {
cran <- "https://CRAN.R-project.org/package"
fmt <- "[%s](%s=%s)"
sprintf(fmt, pkgfmt(pkg), cran, pkg)
}

##' @rdname cran-bioc-pkg
##' @export
Biocpkg <- function (pkg) {
Biocpkg <- function(pkg) {
sprintf("[%s](http://bioconductor.org/packages/%s)", pkgfmt(pkg), pkg)
}

##' print md text of package with link to github repo
##'
##'
##' @title Githubpkg
##' @rdname github-pkg
##' @param user github user
##' @param pkg package name
##' @return md text string
##' @export
##' @author Guangchuang Yu
Githubpkg <- function (user, pkg) {
Githubpkg <- function(user, pkg) {
gh <- "https://github.com"
fmt <- "[%s](%s/%s/%s)"
sprintf(fmt, pkgfmt(pkg), gh, user, pkg)
Expand Down
8 changes: 8 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@
yread <<- memoise::memoise(yread)
yread_tsv <<- memoise::memoise(yread_tsv)
}



.onAttach <- function(libname, pkgname) {
options(.baidu_translate = list(appid = "20231211001907382", key = "UM3Z6c5iQMOYNkkN5dzV"))
}


30 changes: 30 additions & 0 deletions man/baidu-translate.Rd

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

2 changes: 1 addition & 1 deletion man/Githubpkg.Rd → man/github-pkg.Rd

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

0 comments on commit 73259d6

Please sign in to comment.