R:Google翻译API("translate"和"translateR"包) [英] R: google translate API (packages 'translate' & ''translateR')

查看:501
本文介绍了R:Google翻译API("translate"和"translateR"包)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在R-Studio中使用translatetranslateR软件包.

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

我已经创建了服务器"和浏览器" API密钥.运行示例时,浏览器API可以正常工作:

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

但是,当在R-Studio(translate/translateR)中使用API​​密钥和软件包时,会出现错误消息.使用translate

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!

推荐答案

我对此也遇到了一些问题,并编写了一个小函数来从API检索数据:

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])
  }

此处更新了Gist: https://gist.github.com/dschmeh/8414b63c3ab816c44995cd6872165f0e

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

这篇关于R:Google翻译API("translate"和"translateR"包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆