AIに歌を聞かせた。そして彼は笑顔😃になった。(COTOHA-感情分析API)

# 概要
歌を聞かせた。
彼はその歌を聴き、
Positiveな感情になると笑い😃、
Negativeな感情になると怒り😠、
Neutralな感情になると。。。😐

([COTOHA](https://api.ce-cotoha.com/contents/index.html)の[感情分析API](https://api.ce-cotoha.com/contents/reference/apireference.html#sentiment)を使って、歌の歌詞からどのような感情をくみ取るのかを試した。[COTOHA API for Developers](https://api.ce-cotoha.com/contents/developers/index.html)に登録してすぐに、感情分析APIを試せるよう[**WEBアプリ**](https://cotoha-demo.now.sh/)も作った。)


# 結果
とりあえず歴代CDシングル売り上げ枚数ランキングを聞かせた。
「1位から順に！カウントアップ！👆」


## 1.（457.7万枚）およげ！たいやきくん

```:😠
"sentiment": "Negative"
"score": 0.5
```
<details><summary>Response</summary><div>

```json
{
  "result": {
    "sentiment": "Negative",
    "score": 0.5,
    "emotional_phrase": [
      {
        "form": "こころがはずむ",
        "emotion": "喜ぶ"
      },
      {
        "form": "たのしい",
        "emotion": "P"
      },
      {
        "form": "おもい",
        "emotion": "N"
      },
      {
        "form": "ひろいぜ",
        "emotion": "PN"
      },
      {
        "form": "いじめられる",
        "emotion": "N"
      },
      {
        "form": "もがいて",
        "emotion": "N"
      },
      {
        "form": "こげある",
        "emotion": "N"
      },
      {
        "form": "うまそう",
        "emotion": "P"
      }
    ]
  },
  "status": 0,
  "message": "OK"
}
```
</div></details>

## 2.（325.6万枚）女のみち

```:😠
"sentiment": "Negative"
"score": 0.2011977885862898
```
<details><summary>Response</summary><div>

```json
{
  "result": {
    "sentiment": "Negative",
    "score": 0.2011977885862898,
    "emotional_phrase": [
      {
        "form": "みちならば暗い",
        "emotion": "N"
      },
      {
        "form": "恋",
        "emotion": "好ましい,切ない"
      },
      {
        "form": "幸せ",
        "emotion": "P"
      },
      {
        "form": "つらい",
        "emotion": "N"
      },
      {
        "form": "泣",
        "emotion": "喜ぶ,悲しい,切ない"
      },
      {
        "form": "すがって泣いたうぶな",
        "emotion": "PN"
      },
      {
        "form": "いけない",
        "emotion": "N"
      },
      {
        "form": "みちならば",
        "emotion": "PN"
      },
      {
        "form": "捨てた",
        "emotion": "N"
      },
      {
        "form": "いじめる",
        "emotion": "N"
      }
    ]
  },
  "status": 0,
  "message": "OK"
}
```
</div></details>
## 3.（313.2万枚）世界に一つだけの花

```:😃
"sentiment": "Positive"
"score": 0.08995079776388203
```
<details><summary>Response</summary><div>

```json
{
  "result": {
    "sentiment": "Positive",
    "score": 0.08995079776388203,
    "emotional_phrase": [
      {
        "form": "うれしそうな",
        "emotion": "P"
      },
      {
        "form": "笑顔",
        "emotion": "安心"
      },
      {
        "form": "好み",
        "emotion": "好ましい"
      },
      {
        "form": "困った",
        "emotion": "N"
      },
      {
        "form": "迷",
        "emotion": "不安"
      },
      {
        "form": "笑い",
        "emotion": "喜ぶ"
      },
      {
        "form": "もともと特別な",
        "emotion": "PN"
      },
      {
        "form": "ひとそれぞれ",
        "emotion": "PN"
      },
      {
        "form": "きれいだ",
        "emotion": "P"
      },
      {
        "form": "争う",
        "emotion": "N"
      },
      {
        "form": "誇らしげ",
        "emotion": "P"
      },
      {
        "form": "咲かせる",
        "emotion": "P"
      },
      {
        "form": "一生懸命",
        "emotion": "PN"
      },
      {
        "form": "頑張って",
        "emotion": "P"
      },
      {
        "form": "仕方ないね",
        "emotion": "N"
      },
      {
        "form": "色とりどり",
        "emotion": "P"
      },
      {
        "form": "気づかない",
        "emotion": "N"
      },
      {
        "form": "小さい",
        "emotion": "PN"
      },
      {
        "form": "同じものはない",
        "emotion": "PN"
      }
    ]
  },
  "status": 0,
  "message": "OK"
}
```
</div></details>

## 4.（293.6万枚）TSUNAMI


```:😠
"sentiment": "Negative"
"score": 0.10121480950551423
```
<details><summary>Response</summary><div>

```json
{
  "result": {
    "sentiment": "Negative",
    "score": 0.10121480950551423,
    "emotional_phrase": [
      {
        "form": "愛",
        "emotion": "安心,好ましい"
      },
      {
        "form": "愛しい",
        "emotion": "P"
      },
      {
        "form": "怯えてる",
        "emotion": "不安"
      },
      {
        "form": "悲しみ",
        "emotion": "悲しい"
      },
      {
        "form": "恋",
        "emotion": "好ましい,切ない"
      },
      {
        "form": "好きな",
        "emotion": "P"
      },
      {
        "form": "戸惑う",
        "emotion": "N"
      },
      {
        "form": "泣いた",
        "emotion": "N"
      },
      {
        "form": "涙",
        "emotion": "喜ぶ,不安,悲しい,切ない"
      },
      {
        "form": "微笑",
        "emotion": "安心"
      },
      {
        "form": "弱気な",
        "emotion": "N"
      },
      {
        "form": "すがる",
        "emotion": "N"
      },
      {
        "form": "涙もろい",
        "emotion": "PN"
      },
      {
        "form": "清か",
        "emotion": "P"
      },
      {
        "form": "魔性",
        "emotion": "N"
      },
      {
        "form": "彷徨う",
        "emotion": "N"
      },
      {
        "form": "枯れる",
        "emotion": "N"
      },
      {
        "form": "素直に",
        "emotion": "P"
      },
      {
        "form": "侘しさ",
        "emotion": "PN"
      },
      {
        "form": "鏡のような",
        "emotion": "P"
      },
      {
        "form": "深い",
        "emotion": "PN"
      },
      {
        "form": "気付いてる",
        "emotion": "P"
      },
      {
        "form": "張り裂けそうな",
        "emotion": "N"
      }
    ]
  },
  "status": 0,
  "message": "OK"
}
```
</div></details>

## 5.（291.8万枚）だんご３兄弟


```:😠
"sentiment": "Negative"
"score": 0.06456371454543736
```
<details><summary>Response</summary><div>

```json
{
  "result": {
    "sentiment": "Negative",
    "score": 0.06456371454543736,
    "emotional_phrase": [
      {
        "form": "かたくな",
        "emotion": "不安"
      },
      {
        "form": "ささって",
        "emotion": "P"
      },
      {
        "form": "まれ",
        "emotion": "PN"
      },
      {
        "form": "たくさん",
        "emotion": "PN"
      },
      {
        "form": "こげ",
        "emotion": "N"
      },
      {
        "form": "かたくなりました",
        "emotion": "PN"
      }
    ]
  },
  "status": 0,
  "message": "OK"
}
```
</div></details>

## 6.（289.5万枚）君がいるだけで


```:😃
"sentiment": "Positive"
"score": 0.009547567550434202
```
<details><summary>Response</summary><div>

```json
{
  "result": {
    "sentiment": "Positive",
    "score": 0.009547567550434202,
    "emotional_phrase": [
      {
        "form": "憧れ",
        "emotion": "P"
      },
      {
        "form": "笑顔",
        "emotion": "安心"
      },
      {
        "form": "くやしい",
        "emotion": "N"
      },
      {
        "form": "何より大切な",
        "emotion": "P"
      },
      {
        "form": "涙",
        "emotion": "喜ぶ,切ない,不安,悲しい"
      },
      {
        "form": "儚い",
        "emotion": "PN"
      },
      {
        "form": "強く",
        "emotion": "PN"
      },
      {
        "form": "気付かせてくれたね",
        "emotion": "P"
      },
      {
        "form": "ありがちな",
        "emotion": "PN"
      },
      {
        "form": "つい引き込まれ",
        "emotion": "P"
      },
      {
        "form": "弱さ",
        "emotion": "N"
      },
      {
        "form": "忘れてた",
        "emotion": "PN"
      },
      {
        "form": "もっと素直になれなかった",
        "emotion": "N"
      },
      {
        "form": "わかって",
        "emotion": "P"
      }
    ]
  },
  "status": 0,
  "message": "OK"
}
```
</div></details>

## 7.（282.2万枚）SAY YES


```:😠
"sentiment": "Negative"
"score": 0.2490204168421342
```
<details><summary>Response</summary><div>

```json
{
  "result": {
    "sentiment": "Negative",
    "score": 0.2490204168421342,
    "emotional_phrase": [
      {
        "form": "愛",
        "emotion": "好ましい,安心"
      },
      {
        "form": "愛してる",
        "emotion": "P"
      },
      {
        "form": "恋",
        "emotion": "好ましい,切ない"
      },
      {
        "form": "寂しい",
        "emotion": "N"
      },
      {
        "form": "切なさ",
        "emotion": "PN"
      },
      {
        "form": "迷",
        "emotion": "不安"
      },
      {
        "form": "ワガママ",
        "emotion": "N"
      },
      {
        "form": "余計な",
        "emotion": "N"
      },
      {
        "form": "何げなく暮らさない",
        "emotion": "PN"
      },
      {
        "form": "あふれてる",
        "emotion": "N"
      },
      {
        "form": "勝てない",
        "emotion": "N"
      },
      {
        "form": "消えない",
        "emotion": "P"
      }
    ]
  },
  "status": 0,
  "message": "OK"
}
```
</div></details>
## 8.（276.6万枚）Tomorrow never knows


```:😃
"sentiment": "Positive"
"score": 0.007165278534051281
```
<details><summary>Response</summary><div>

```json
{
  "result": {
    "sentiment": "Positive",
    "score": 0.007165278534051281,
    "emotional_phrase": [
      {
        "form": "愛した",
        "emotion": "P"
      },
      {
        "form": "悲しい",
        "emotion": "N"
      },
      {
        "form": "孤独な",
        "emotion": "PN"
      },
      {
        "form": "寂しい",
        "emotion": "N"
      },
      {
        "form": "愛される喜び",
        "emotion": "P"
      },
      {
        "form": "消えた帰らぬ",
        "emotion": "P"
      },
      {
        "form": "すれ違う",
        "emotion": "N"
      },
      {
        "form": "無邪気に",
        "emotion": "P"
      },
      {
        "form": "裏切れる",
        "emotion": "N"
      },
      {
        "form": "欲しがっていた",
        "emotion": "P"
      },
      {
        "form": "分かり合えた",
        "emotion": "P"
      },
      {
        "form": "夢中で駆け抜ける",
        "emotion": "P"
      },
      {
        "form": "勝利",
        "emotion": "P"
      },
      {
        "form": "敗北もない",
        "emotion": "P"
      },
      {
        "form": "忘れてゆく",
        "emotion": "PN"
      },
      {
        "form": "避けて通れない",
        "emotion": "P"
      },
      {
        "form": "果てしない",
        "emotion": "P"
      },
      {
        "form": "優しさ",
        "emotion": "P"
      },
      {
        "form": "長い",
        "emotion": "PN"
      },
      {
        "form": "癒える",
        "emotion": "P"
      },
      {
        "form": "少し",
        "emotion": "PN"
      }
    ]
  },
  "status": 0,
  "message": "OK"
}
```
</div></details>
## 9.（258.8万枚）ラブ・ストーリーは突然に


```:😃
"sentiment": "Positive"
"score": 0.060429544970368884
```
<details><summary>Response</summary><div>

```json
{
  "result": {
    "sentiment": "Positive",
    "score": 0.060429544970368884,
    "emotional_phrase": [
      {
        "form": "もっと好き",
        "emotion": "P"
      },
      {
        "form": "もう心揺れたりしないで切ない",
        "emotion": "PN"
      },
      {
        "form": "揺れたり",
        "emotion": "興奮"
      },
      {
        "form": "いい",
        "emotion": "P"
      },
      {
        "form": "分からない",
        "emotion": "N"
      },
      {
        "form": "消えてゆく",
        "emotion": "N"
      },
      {
        "form": "あんまりすてきだ",
        "emotion": "P"
      },
      {
        "form": "甘く",
        "emotion": "PN"
      },
      {
        "form": "やわらかく",
        "emotion": "PN"
      },
      {
        "form": "心が動いた",
        "emotion": "P"
      },
      {
        "form": "忘れない",
        "emotion": "PN"
      },
      {
        "form": "心揺れたりしないで",
        "emotion": "PN"
      }
    ]
  },
  "status": 0,
  "message": "OK"
}
```
</div></details>
## 10.（248.9万枚）LOVE LOVE LOVE


```:😃
"sentiment": "Positive"
"score": 0.24542462771149284
```
<details><summary>Response</summary><div>

```json
{
  "result": {
    "sentiment": "Positive",
    "score": 0.24542462771149284,
    "emotional_phrase": [
      {
        "form": "愛",
        "emotion": "安心,好ましい"
      },
      {
        "form": "愛してる",
        "emotion": "P"
      },
      {
        "form": "すごく好きな",
        "emotion": "P"
      },
      {
        "form": "涙",
        "emotion": "喜ぶ,切ない,不安,悲しい"
      },
      {
        "form": "すっごく",
        "emotion": "PN"
      },
      {
        "form": "うまく",
        "emotion": "P"
      },
      {
        "form": "願う",
        "emotion": "P"
      },
      {
        "form": "少しずつ思い出になって",
        "emotion": "P"
      }
    ]
  },
  "status": 0,
  "message": "OK"
}
```
</div></details>


## 番外編（何となく気になった曲）
### 大きなのっぽの古時計

```:😃
"sentiment": "Positive"
"score": 0.30601098537139876
```
<details><summary>Response</summary><div>

```json
{
  "result": {
    "sentiment": "Positive",
    "score": 0.30601098537139876,
    "emotional_phrase": [
      {
        "form": "うれしい",
        "emotion": "P"
      },
      {
        "form": "かなしい",
        "emotion": "N"
      },
      {
        "form": "のっぽの",
        "emotion": "PN"
      },
      {
        "form": "ごじまんの",
        "emotion": "PN"
      },
      {
        "form": "きれいな",
        "emotion": "P"
      }
    ]
  },
  "status": 0,
  "message": "OK"
}
```
</div></details>

### 千の風になって

```:😃
"sentiment": "Positive"
"score": 0.14780720679775194,
```
<details><summary>Response</summary><div>

```json
{
  "result": {
    "sentiment": "Positive"
    "score": 0.14780720679775194,
    "emotional_phrase": [
      {
        "form": "泣かないでください",
        "emotion": "P"
      },
      {
        "form": "きらめく",
        "emotion": "P"
      },
      {
        "form": "見守る",
        "emotion": "P"
      }
    ]
  },
  "status": 0,
  "message": "OK"
}
```
</div></details>

### パプリカ

```:😃
"sentiment": "Positive"
"score": 0.01385186275421083
```
<details><summary>Response</summary><div>

```json
{
  "result": {
    "sentiment": "Positive",
    "score": 0.01385186275421083,
    "emotional_phrase": [
      {
        "form": "泣いてた",
        "emotion": "N"
      },
      {
        "form": "喜び",
        "emotion": "P"
      },
      {
        "form": "晴れた",
        "emotion": "P"
      },
      {
        "form": "燻り",
        "emotion": "N"
      },
      {
        "form": "慰める",
        "emotion": "P"
      },
      {
        "form": "まれ",
        "emotion": "PN"
      }
    ]
  },
  "status": 0,
  "message": "OK"
}
```
</div></details>

# WEBアプリ化
[COTOHA API for Developers](https://api.ce-cotoha.com/contents/developers/index.html)に登録した人限定ではあるが、同じ遊びが出来るようにした。（簡単に登録可）
以下URLで遊ぶには、登録後に取得できる、**Client ID**と**Client secret**が必要。
https://cotoha-demo.now.sh/

|![2020-02-26_17h32_04.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/184925/ee953aa6-0e92-71ee-e06d-76aec67b71c6.png)|
|:-:|
**Client ID**と**Client secret**は、画面リフレッシュのたびに入力しなおすのは面倒。なので、ブラウザのlocalstrageにて情報を保持している。

# 技術
アプリ化に伴い、以下の技術を使用。

- Nuxt.js【JavaScript framework】
- ant-design-vue【Css framework】
- Now【Deploy】
- Express【API】


<details><summary>server.js</summary><div>

```javascript:server.js
const express = require('express')
const request = require('request')
const app = express()

// server
const port = process.env.PORT || 5000
app.listen(port, err => {
    if (err) throw err
    console.log(`> Ready On Server http://localhost:${port}`)
})

// API
app.get('/getToken', function(req, res, next) {
    (() => {
        return new Promise(r => {
            if (req.query.ClientID && req.query.Clientsecret && req.query.AccessTokenPublishURL){
                const AccessTokenPublishURL = req.query.AccessTokenPublishURL
                const ClientID = req.query.ClientID
                const Clientsecret = req.query.Clientsecret
                const headers = {
                    'Content-Type': 'application/json'
                }
                const data = {
                    'grantType': 'client_credentials',
                    'clientId': ClientID,
                    'clientSecret': Clientsecret
                }
                const options = {
                    url: AccessTokenPublishURL,
                    method: 'POST',
                    headers: headers,
                    json: data
                }
                request(options, (error, response, body) => {
                    return r(body)
                })
            }else{
                r('Parameter is incorrect')
            }
        })
    })().then(data => {
        res.send(data)
    }).catch(error => {
        res.send(error)
      })
})

app.get('/cotohaApiSimilarity', function(req, res, next) {
    (() => {
        return new Promise(r => {
            if (req.query.sentence && req.query.token){
                const sentence = req.query.sentence
                const token = req.query.token
                const headers = {
                    'Content-Type': 'application/json;charset=UTF-8',
                    'Authorization': `Bearer ${token}`
                }
                const data = {
                    'sentence': sentence,
                }
                const options = {
                    url: 'https://api.ce-cotoha.com/api/dev/nlp/v1/sentiment',
                    method: 'POST',
                    headers: headers,
                    json: data
                }
                request(options, (error, response, body) => {
                    return r(body)
                })
            }else{
                r('Parameter is incorrect')
            }
        })
    })().then(data => {
        res.send(data)
    }).catch(error => {
        res.send(error)
      })
})
```
</div></details>


# あとがき
他のAPIと組み合わせれば、話す内容によって表情を変えるbotが簡単に出来そう。

あと要約(β)API・キーワード抽出API・言い淀み除去(β)・音声認識誤り検知(β)・音声認識API　ここら辺のを使って、音声データから議事録を作るWEBアプリ（WEBサービス）作りたい。。。時間が欲しい。。。

# おまけ
Q.　さて、これは誰が歌っているのさくらの結果でしょうか？？？↓
### さくら

```:😃
"sentiment": "Positive",
"score": 0.0010436678748683892,
```
<details><summary>Response</summary><div>

```json
{
  "result": {
    "sentiment": "Positive",
    "score": 0.0010436678748683892,
    "emotional_phrase": [
      {
        "form": "また気になる",
        "emotion": "PN"
      },
      {
        "form": "忘れた",
        "emotion": "PN"
      },
      {
        "form": "淡い",
        "emotion": "PN"
      },
      {
        "form": "意味なく",
        "emotion": "N"
      },
      {
        "form": "変わらない",
        "emotion": "PN"
      },
      {
        "form": "甦る",
        "emotion": "PN"
      },
      {
        "form": "長い",
        "emotion": "PN"
      },
      {
        "form": "たわいない",
        "emotion": "PN"
      },
      {
        "form": "気付けば",
        "emotion": "P"
      },
      {
        "form": "誘われ",
        "emotion": "P"
      },
      {
        "form": "すり抜けた",
        "emotion": "PN"
      },
      {
        "form": "分からなかった",
        "emotion": "N"
      },
      {
        "form": "若かった",
        "emotion": "P"
      },
      {
        "form": "香る",
        "emotion": "P"
      },
      {
        "form": "暖かい",
        "emotion": "P"
      },
      {
        "form": "こぼれる",
        "emotion": "N"
      },
      {
        "form": "消えてしまうよ",
        "emotion": "N"
      }
    ]
  },
  "status": 0,
  "message": "OK"
}
```
</div></details>