シリコンバレーのマーケター日記

東京、シンガポールを経てシリコンバレーで働く、マーケター。英語、プログラミング、データ分析は次世代マーケターの必須スキルだと信じて進む。

Google Cloud Natural Language API(クラウド自然言語API)に触れる

さて、今週末はGoogle自然言語APIを触ってみました。すでに公開していたPrediction APIについては以下より。

 

Googleのホームページに載っていたquick startに従ってすすめていきます。curlを使っての手順になっています。要はterminal/ linuxコマンドを使った手順です。

1) Google Could SDKをインストールする。

以下のページが解りやすかったです。googleのサイトにzipも上がっていますが、ターミナルからの方が楽です。

Google Cloud SDKのインストールと認証の設定について - TASK NOTES

2) サービスアカウントを作成する

API Managerから、サービスアカウントを作成します。Google Could APIを使うときは必要になるようです。作成すると、json形式のaccount keyが生成されるので、大事に保管。

f:id:yuyutata:20160731235738p:plain

3) json形式で分析したい文章のfileをつくる

先日決算のあったFacebook COOのシェリルサンドバーグの決算に対する投稿を使ってみます。

{
"document":{
"type":"PLAIN_TEXT",
"content":"Today we shared our quarterly results and announced that there are now 60 million businesses using Facebook Pages each month. Facebook and Instagram have become the mobile presence for businesses around the world — and we are grateful that businesses large and small are using our products to connect to their customers."
},

4) 分析したいjsonファイルのあるディレクトリーまで移動する

コマンドは、”cd”で移動です。

5) アクセストークンの入手

$ gcloud auth activate-service-account --key-file=service-account-key-file //アカウントキーの場所をいれる

$ gcloud auth print-access-token
access_token //アクセストークンをget入手する。

6) 分析!

$ curl -s -k -H "Content-Type: application/json" \
    -H "Authorization: Bearer access_token" \
    https://language.googleapis.com/v1beta1/documents:analyzeEntities \
    -d @entity-request.json //分析したいjsonファイル名

7) 結果

{

  "documentSentiment": {

    "polarity": 0.3,

    "magnitude": 1.2

  },

  "language": "en"

}

  • Polarityとmagnitudeの2軸での評価のようです。
  • Polarity:方向性、極性という意味。+1 から -1の範囲をとる

Magnitude:強さで、0 から +無限大 をとる。文章が長いとMagnitudeが上がる傾向がある

ということらしい。

ということは、Sheryleの投稿は、positiveっちゃpositiveだけどけっこう冷静な感じ といったところでしょうか。まーそんなもんですね。

 

所感:

これをサービスに実装するとなると、json形式のファイルを一度生成した上でぶっ込んでいく感じでしょうか。

Facebookのページコメントを自動で分析する場合は、

  1. Facebookページコメントを引っ張る
  2. json形式ファイルを一個ずつ作成
  3. 一個ずつAPIにアクセス

という感じ?

あと現在はsentiment analysisは、英語サポートのみです。

 

以下、googleのページから引用

Interpreting Sentiment Analysis Values

A document with low magnitude generally indicates a low-emotion (neutral) document. A document with low polaritymay also indicate a neutral document, but can indicate a document of mixed emotions, with both high positive and negative values which cancel each out. Generally, the combination of polarity and magnitude provides you with the best evaluation of a document's overall sentiment.

The chart below shows some sample values and how to interpret them:

SentimentSample Values
Positive "polarity": 0.8, "magnitude": 3.0
Negative "polarity": -0.3, "magnitude": 4.0
Neutral "polarity": 1.0, "magnitude": 0.0
Mixed "polarity": 0.0, "magnitude": 4.0

When comparing documents, multiplying polarity and magnitude can provide a rough comparison of the sentiment, provided that you are analyzing documents of similar length.