R/ggtrans.R

#' 谷歌翻译
#' @description A R wrap function ggtrans of python package googletrans to translate between languages.
#' @param string string to translate
#' @param src the source language
#' @param dest the destination language
#' @return translations
#' @export
#' @examples
#' ggtrans("hello")
#' ggtrans('我们爱世界','zh-CN','en')
#'#data=readLines("ch18-final.txt")
#'#data[1:10] %>% map(~paste(.x,gmtools::ggtrans(.x)[2],"\n\n")) %>% map(~cat(.x, file="ggtrans-ch18.txt",append=TRUE))

ggtrans <- function(string,src="en",dest="zh-CN"){
	#path <- file.path(system.file(package = "gmdata"),'ggtrans.py')
	path <- file.path(system.file(package = "gmtools"),'ggtrans.py')
	cmd <- paste0('/anaconda3/bin/python ', path,' "' ,string,'" "',src,'" "',dest,'"')
	trans <- system(command=cmd, intern = TRUE)[1]
	res = c(string,trans,"谷歌翻译")
	return(res)
}
Gabegit/gmtools documentation built on May 6, 2019, 5:32 p.m.