Google Cloud Platformではじめる機械学習と深層学習を読む

3-4 Cloud Natural Language API

3-4-2 エンティティ分析
from google.cloud import language

nl_client = language.Client()
# 解析する文章
text = u'六本木ヒルズにあるGoogleのオフィスには山手線駅名の会議室があります。'
document = nl_client.document_from_text(text)
print(type(document))
# エンティティ分析を実行
response = document.analyze_entities()

# 結果の表示
for entity in response.entities:
    print('=' * 20)
    print(u'   単語: {}'.format(entity.name))
    print(u'   種類: {}'.format(entity.entity_type))
    print(u'メタデータ: {}'.format(entity.metadata))
    print(u'  重要度: {}'.format(entity.salience))

f:id:bitop:20171226181323p:plain

3-4-3 感情分析

食べログの「くちこみ」テキストを感情分析させそのスコアと 評価点との相関関係をプロットした。 12人しか調べていないので正しいのか不明

# NL API クライアントライブラリをインポート
from google.cloud import language

# クライアントのインスタンスを作成
nl_client = language.Client()
# 解析する文章
data = [
    {
    "user":"user_name",
    "score":3.5,
    "text":"XXXXXXX"
    },
    ......
]

x = []
y = []
z = []
for d in data:
    document = nl_client.document_from_text(d["text"])
    response = document.analyze_sentiment()
    sentiment = response.sentiment
    x.append(d["score"])
    y.append(sentiment.score)
    z.append(sentiment.magnitude)
    print("score=%f magnitude=%f" % (sentiment.score,sentiment.magnitude))
    print("taberogu score %f" % d["score"])

import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.scatter(x,y)
w = [i*j for i,j in zip(y,z)]
ax2 = fig.add_subplot(212)
ax2.scatter(x,w)
plt.show()

f:id:bitop:20171227091552p:plain
食べログの評価値と口コミの感情ポジティブ値の散布図
ほぼ相関がありそうであるが感情ポジティブ値が低いのに
食べログの評価値が高い人もいてきれいな相関はなかった。
f:id:bitop:20171227092121p:plain
食べログの評価値と口コミの感情ポジティブ値×感情の強度を
掛け合わせた値の散布図もつくった。
f:id:bitop:20171227092147p:plain