iMind Developers Blog

iMind開発者ブログ

IBM CloudのPersonality Insightsを使う

概要

Personality Insightsはツイートなどのテキストから書いた人の分析をします。

具体的にはビッグファイブ(知的好奇心、誠実性、外向性、協調性、感情起伏)の分析や、ミュージカル映画を好む、楽器の演奏経験がある等の分析結果が取れる。

価格

価格はこちら https://www.ibm.com/cloud/watson-personality-insights/pricing

1000回までならタダなので、まずはその枠の範囲で試してみます。

アカウントの作成

下記から Get started free を選択し、IBM Cloudのアカウントを作成します。

https://www.ibm.com/watson/services/personality-insights/

トークンの取得

下記のサイトを参考にIBM Cloudのアカウントを作成し、リソース・リストからpersonality-insightsのAPI鍵を生成します。

https://cloud.ibm.com/docs/services/watson?topic=watson-iam&locale=ja

作成すると下記のようなページからAPI鍵をコピーできる。

f:id:imind:20191229020223p:plain

curlでのリクエスト

curlでのリクエスト例。

profile.txt というファイルを作ってそこに分析したい文章を打ち込む。

curlで実行。

API_KEY="取得したAPIキーをここに記述"

curl -X POST -u "apikey:$API_KEY" \
    --header "Content-Type: text/plain;charset=utf-8" \
    --header "Content-Language: ja" \
    --header "Accept: application/json" \
    --data-binary @profile.txt \
    "https://gateway.watsonplatform.net/personality-insights/api/v3/profile?version=2017-10-13"

上記は日本語を実行する場合の例としてheaderにContent-Language: jaを設定している。これを入れないと英語として解析されてエラーになる。

実行結果がこちら。

{
  "word_count": 717,
  "processed_language": "ja",
  "personality": [
    {
      "trait_id": "big5_openness",
      "name": "Openness",
      "category": "personality",
      "percentile": 0.9338527467010143,
      "significant": true,
      "children": [
        {
          "trait_id": "facet_adventurousness",
          "name": "Adventurousness",
          "category": "personality",
          "percentile": 0.45835004591302886,
          "significant": true
        },
        {
          "trait_id": "facet_artistic_interests",
          "name": "Artistic interests",
          "category": "personality",
          "percentile": 0.7427029500150462,
          "significant": true
        },
        {
          "trait_id": "facet_emotionality",
          "name": "Emotionality",
          "category": "personality",
          "percentile": 0.8283521636576548,
          "significant": true
        },
        {
          "trait_id": "facet_imagination",
          "name": "Imagination",
          "category": "personality",
          "percentile": 0.7825356332489908,
          "significant": true
        },
        {
          "trait_id": "facet_intellect",
          "name": "Intellect",
          "category": "personality",
          "percentile": 0.959826933376321,
          "significant": true
        },
        {
          "trait_id": "facet_liberalism",
          "name": "Authority-challenging",
          "category": "personality",
          "percentile": 0.7128506811703423,
          "significant": true
        }
      ]
    },
    {
      "trait_id": "big5_conscientiousness",
      "name": "Conscientiousness",
      "category": "personality",
      "percentile": 0.5569023066666839,
      "significant": true,
      "children": [
        {
          "trait_id": "facet_achievement_striving",
          "name": "Achievement striving",
          "category": "personality",
          "percentile": 0.3357051113027134,
          "significant": true
        },

以下略

それぞれのキーの意味は下記参照。

https://cloud.ibm.com/docs/services/personality-insights?topic=personality-insights-output

結果を日本語で表示

Accept-Languageをjaにすると戻り値のjsonのキーが日本語になる。

curl -X POST -u "apikey:$API_KEY" \
    --header "Content-Type: text/plain;charset=utf-8" \
    --header "Content-Language: ja" \
    --header "Accept: application/json" \
    --header "Accept-Language: ja" \
    --data-binary @profile.txt \
    "https://gateway.watsonplatform.net/personality-insights/api/v3/profile?version=2017-10-13&consumption_preferences=true"
{
  "word_count": 717,
  "processed_language": "ja",
  "personality": [
    {
      "trait_id": "big5_openness",
      "name": "知的好奇心",
      "category": "personality",
      "percentile": 0.9338527467010143,
      "significant": true,
      "children": [
        {
          "trait_id": "facet_adventurousness",
          "name": "大胆性",
          "category": "personality",
          "percentile": 0.45835004591302886,
          "significant": true
        },
        {
          "trait_id": "facet_artistic_interests",
          "name": "芸術的関心度",
          "category": "personality",
          "percentile": 0.7427029500150462,
          "significant": true
        },

以下略

消費嗜好性の取得

パラメータでconsumption_preferencesをtrueにすると、消費嗜好性についても取得ができるらしい。

クエリパラメータに追加して実行する。

curl -X POST -u "apikey:$API_KEY" --header "Content-Type: text/plain;charset=utf-8" --header "Content-Language: ja" --header "Accept: application/json" --data-binary @profile.txt "https://gateway.watsonplatform.net/personality-insights/api/v3/profile?version=2017-10-13&consumption_preferences=true"

戻りのjsonに先ほどはいなかったconsumption_preferencesというキーが登場する。

  "consumption_preferences": [
    {
      "consumption_preference_category_id": "consumption_preferences_shopping",
      "name": "Purchasing Preferences",
      "consumption_preferences": [
        {
          "consumption_preference_id": "consumption_preferences_automobile_ownership_cost",
          "name": "Likely to be sensitive to ownership cost when buying automobiles",
          "score": 0
        },
        {
          "consumption_preference_id": "consumption_preferences_automobile_safety",
          "name": "Likely to prefer safety when buying automobiles",
          "score": 1
        },
        {
          "consumption_preference_id": "consumption_preferences_clothes_quality",
          "name": "Likely to prefer quality when buying clothes",
          "score": 0
        },

以下略

車を買いたいとか衝動買いの可能性等、いろいろな分析結果が得られる。

https://cloud.ibm.com/docs/services/personality-insights?topic=personality-insights-preferences

Pythonからの実行

Watson Developer Cloud Python SDK というのがあるので使ってみる。

https://github.com/watson-developer-cloud/python-sdk

pipでインストール。

pip install --upgrade ibm-watson

テストデータとして、トランプ大統領のTwitterから適当にテキストを抜粋する。

content = '''
new Federal Judges have been confirmed under the Trump Administration, including two great new United States Supreme Court Justices. We are shattering every record! Read all about this in “The Long Game,” a great new book by @senatemajldr
 Mitch McConnell. Amazing story!

The ONLY reason we were able to get our great USMCA Trade Deal approved was because the Do Nothing Democrats wanted to show that they could approve  something productive in light of the fact that all they even think about is impeachment. She knows nothing about the USMCA Deal!

“The Dems are complicating matters again. Keeping Mueller out of it was the focus. Keeping it crisp & simple was the key, and now all of a sudden they are saying maybe we’ll go back and visit the Mueller probe, which is absolutely unbelievable, and shows they don’t care about....

....the American public’s tone deafness - & it should be intolerable, because the American people have had it with this.” @kilmeade
 @foxandfriends
  The Radical Left, Do Nothing Democrats have gone CRAZY. They want to make it as hard as possible for me to properly run our Country!

“The Trump Administration, Trump policies, have put our economy on a trajectory it has never been on before.” Griff Jenkins, @foxandfriends
'''

API_KEYに取得したAPIキーを入力して、下記のコードを実行する。

from ibm_watson import IAMTokenManager
from ibm_watson.personality_insights_v3 import PersonalityInsightsV3, ProfileEnums
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

API_KEY="取得したAPIキーをここに記述"

authenticator = IAMAuthenticator(API_KEY)
service = PersonalityInsightsV3(version='2017-10-13', authenticator=authenticator)

profile = service.profile(
    content,
    accept = ProfileEnums.Accept.APPLICATION_JSON.value,
    content_type=ProfileEnums.ContentType.TEXT_PLAIN.value)

result = profile.get_result()

これでresultにcurlで叩いた時にjsonで取得された値と同じものがdictとして入る。

Accept-Lanuage, Content-Language, consumption_preferences なども指定できる。

profile = service.profile(
    content,
    accept=ProfileEnums.Accept.APPLICATION_JSON.value,
    content_type=ProfileEnums.ContentType.TEXT_PLAIN.value,
    content_language=ProfileEnums.ContentLanguage.JA.value,
    accept_language=ProfileEnums.AcceptLanguage.JA.value,
    consumption_preferences=True)

改定履歴

Author: Masato Watanabe, Date: 2020-01-05, 記事投稿