【问题标题】:R: google translate API (packages 'translate' & ''translateR')R:谷歌翻译 API(包“翻译”和“翻译R”)
【发布时间】:2016-11-18 22:16:16
【问题描述】:

我正在尝试将 translatetranslateR 包与 R-Studio 一起使用。

我已经创建了“服务器”和“浏览器”API 密钥。运行示例时浏览器 API 工作正常:

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

但是,当使用 API 密钥和 R-Studio (translate/translateR) 包时,我收到一条错误消息。与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

可能是什么问题?感谢您的帮助!

【问题讨论】:

  • 服务器和浏览器 API 帮不了你,你需要谷歌的翻译 API。 cloud.google.com/translate/docs
  • 对不起,你能澄清一下吗?我已经启用了 Google 的翻译 API——这就是我最初获得“服务器”和“浏览器”API 密钥的方式。据我了解,R 包 translatetranslateR 应该使用这些键。我错过了什么?
  • 您的 API 请求是否要求拥有一个 Google 帐户,并在文件中记录帐单信息?如果没有,那么你得到了错误的 API。
  • 是的,我确实包含了帐单信息。同样,基于浏览器的翻译与我的 API 密钥配合得很好。问题在于 R 包的功能。

标签: r google-translate translate


【解决方案1】:

似乎问题与系统有关。我更改了 https 代理后它就可以工作了。

【讨论】:

    【解决方案2】:

    我也遇到了一些问题,并编写了一个小函数来从 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])
      }
    

    此处更新要点:https://gist.github.com/dschmeh/8414b63c3ab816c44995cd6872165f0e

    【讨论】:

      猜你喜欢
      • 2011-12-26
      • 1970-01-01
      • 2015-05-19
      • 2010-10-10
      • 2011-06-06
      • 2020-12-19
      • 2019-05-20
      相关资源
      最近更新 更多