R: google translate API (packages 'translate' & ''translateR')

我只是一个虾纸丫 提交于 2019-12-06 15:59:12

问题


I am trying to use the translate and translateR packages with R-Studio.

I have created both a 'server' and 'browser' API key. The browser API works fine when running the example:

https://www.googleapis.com/language/translate/v2?key=YOUR_API_KEY&q=hello%20world&source=en&target=de

However, when using either API key and either package with R-Studio (translate/translateR), I obtain an error message. With translate

> library(translate)
> set.key("mykey")
> translate('Hello, world!', 'en', 'de')
Error in function (type, msg, asError = TRUE)  : 
  SSL certificate problem: unable to get local issuer certificate

What might be the issue? Thanks for help!


回答1:


Seems the issue was related to the system. It works after I changed the https proxy.




回答2:


I also had some Problems with this and wrote a little function to retrieve the data from the API:

#' Translate with R
#'
#' Translate Keywords or/and text with the Google Translate API
#' The Functions allows to translate keywords or sentences using the Google Translate API.
#' To use this function you need to get a API-Key for the Google Translate API <https://cloud.google.com/translate/docs/?hl=en>.
#' @param text The keyword/sentence/text you want to translate
#' @param API_Key Your API Key. You get the API Key here: <https://cloud.google.com/translate/docs/?hl=en>
#' @param target The Language target your text translated to. For German 'de'. 
#' @param source The Language your given text/keyword is. For example 'en' - english 
#' translate()
#' @examples
#' \dontrun{
#' translate(text = "R is cool", API_Key = "XXXXXXXXXXX", target = "de", source = "en")
#' }


translate <- function(text,
                      API_Key,
                      target = "de",
                      source = "en") {
  b <- paste0(
    '{
    "q": [
    "',
    text,
    '"
    ],
    "target": "',
    target,
    '",
    "source": "',
    source,
    '",
    "format": "text"
}'
)
  url <-
    paste0("https://translation.googleapis.com/language/translate/v2?key=",
           API_Key)
  x <- httr::POST(url, body = b)
  x <- jsonlite::fromJSON(rawToChar(x$content))
  x <- x$data$translations
  return(x$translatedText[1])
  }

Updated Gist here: https://gist.github.com/dschmeh/8414b63c3ab816c44995cd6872165f0e



来源:https://stackoverflow.com/questions/38400582/r-google-translate-api-packages-translate-translater

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!