Google Cloud Natural Language APIの日本語の感情分析(analyzeSentiment)をRから使ってみる

最近、Google Cloud Natural Language APIの感情分析が日本語に対応したらしい。
今回はちょっと使ってみたのでそのコードを載せるだけである。
Rでやってみるが、どの言語でもできると思う。
Google Cloud PlatformのAPI_KEYを持ってない人は登録する必要があります。
以下、コードです。

getResult <- function(res){
  API_KEY <- "**********************"
  u <- paste0("https://language.googleapis.com/v1/documents:analyzeSentiment?key=", API_KEY)
  body <-list(document=list(type = "PLAIN_TEXT",
                            content = res,language="ja"))
  
  response <- POST(url = u,
              encode = "json",
              body = body,
              content_type_json())
  return(response)
}
library("httr")
res<-"最悪だ"
res1<-getResult(res)
content(res1)$documentSentiment$score

実行結果は以下のように帰ってきます。

> res1
Response [https://language.googleapis.com/v1/documents:analyzeSentiment?key=***************************]
  Date: 2017-05-19 12:30
  Status: 200
  Content-Type: application/json; charset=UTF-8
  Size: 287 B
{
  "documentSentiment": {
    "magnitude": 0.7,
    "score": -0.7
  },
  "language": "ja",
  "sentences": [
    {
      "text": {
        "content": "最悪だ",
...

content(res1)$documentSentiment$scoreでscoreだけ取れます。
ちなみに、テキストを"最高だ"にするとscore=0.7になりました。はい。