LoginSignup
4
2

More than 1 year has passed since last update.

LIFE WILL Emodiversity APIのemotion_analyzeを使ってみたメモ

Last updated at Posted at 2021-10-28

これはなに

LIFULLさんから出ているテキストから感情を分析するAPIを利用するにあたってのメモです。

emotion_mapについてのメモはこちら

どんなものかのデモはこちらで体験できます。
https://lab.lifull.com/lifewill/emodiversity/

tokenの申請は以下のページを見てください。
https://lab.lifull.com/lifewill/emodiversity/api-docs/index.html

今回利用するemotion_analyzeのドキュメントはこちら
https://lab.lifull.com/lifewill/emodiversity/api-docs/reference/emotion_analyze.html

APIの呼び方

Node.js

const request = require("request");

request.post({
    url: "https://lab.lifull.com/lifewill/api/v1/emotion-analyze",
    headers: {
        "token": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    },
    form: { text: "あいうえお" },
    json: true
}, function (error, response, body) {
    if (!error) {
        console.log(body);
    }
});

Postman

スクショの方がわかりやすいのでそれを。
image.png
image.png

※refererはドキュメント的に必須ですが2021年10月現在は無くても動作するようです。

Browser

Headerが以下になっているので、他のドメインでは利用できません。
'access-control-allow-origin': 'https://lab.lifull.com/lifewill'
tokenが丸見えになるのがなんですが利用可能です。

const obj = { text: "こんにちは" };
const body = Object.keys(obj).reduce((o, key) => (o.set(key, obj[key]), o), new FormData());
const headers = {
 'Token': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
};
let res = await fetch('https://lab.lifull.com/lifewill/api/v1/emotion-analyze', { 'POST', headers, body });
let json = await res.json();

取得できるデータ

実際のサンプル

{
  analyze_id: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
  emo_graph: [
    0.130787371925722,
    0.0526486850710477,
    1,
    -0.304385738916744,
    -0.00231078697556408,
    0.368675558374126,
    0.115828692601915,
    0.0510236033215083,
    -0.192191680374165,
    -1,
    -0.117620246140431,
    -0.510279632969341
  ],
  emo_max: 'キュンキュン',
  emo_min: 'ツンツン',
  recommend: [
    { jis_code: '62022', name: '米沢市' },
    { jis_code: '272272', name: '神戸市' },
    { jis_code: '82309', name: '神栖市' },
    { jis_code: '183229', name: 'おおい町' },
    { jis_code: '45012', name: '涌谷町' },
    { jis_code: '294268', name: '新宮市' },
    { jis_code: '442071', name: '門川町' },
    { jis_code: '43010', name: '蔵王町' },
    { jis_code: '102083', name: '富岡市' },
    { jis_code: '204099', name: '大鹿村' }
  ],
  result: 'success'
}

※同じtextを送っても値が変わる事があるようなので要注意です。2021年10月現在で同じ単語でも2種類の値が2回毎に送られてくる状態です。

データの変換

  • emo_graphの配列を取得しやすいオブジェクトにする
const emoList = ["ドキドキ", "ワクワク", "キュンキュン", "ニコニコ", "ホノボノ", "ユルユル", "ウトウト", "シクシク", "モヤモヤ", "ツンツン", "ピリピリ", "ソワソワ"];
const emoGraphs = {};
for (let i in body.emo_graph) {
    emoGraph[emoList[i]] = body.emo_graph[i];
}
console.log(emoGraph);

- 色の分布とマッチングさせる

値のイメージは以下の図から抽出
image.png
https://lifull.com/news/16373/

const emoColors = {
"ドキドキ":"#c81c31",
"ワクワク":"#fbda00",
"キュンキュン":"#f555cc",
"ニコニコ":"#efa773",
"ホノボノ":"#c4d56a",
"ユルユル":"#87cca8",
"ウトウト":"#a9a698",
"シクシク":"#96a2a5",
"モヤモヤ":"#a59aa9",
"ツンツン":"#28416d",
"ピリピリ":"#7d0000",
"ソワソワ":"#79017a"
}
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2