iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🎵

Analyzing the Emotions of People Who Are Shivering with Longing

に公開

Objective

Understand the emotions of someone who is "trembling because they want to see someone so much"

I want to perform sentiment analysis on lyrics to determine if a song is upbeat or sad.

It would be great to have ambiguous searches on music streaming services, such as searching for "upbeat songs" or "sad songs," right? Since I'm having an unlucky year and nothing good is happening, I've been listening to upbeat songs to keep going.

Let's Analyze the Lyrics

Analysis Tool

COTOHA API
Python 3.8.1

Targets for Analysis

The song for "trembling because I want to see you"

  • Kana Nishino, "Aitakute Aitakute" (I want to see you, I want to see you)

The following were already categorized on Recochoku, so I selected songs from those lists based on my own taste.

Upbeat Songs

  • Greeeen, "Kiseki" (Miracle)
  • Sambomaster, "Sekai wa Sore o Ai to Yobu ndaze" (The World Calls That Love)
  • SPYAIR, "BEAUTIFUL DAYS" *Not on Recochoku, but a song the author likes

Sad Songs

  • Ken Hirai, "Nonfiction"
  • back number, "Happy End"

Code

Will the cactus stop dancing due to sentiment analysis?
I used this as a reference.

<details><summary>Implementation</summary><div>

top/
 ├ file/
 │ ├ input.txt   (lyrics to analyze)
 │ └ output.csv (output results)
 ├ config.ini
  └ cotoha_sentiment.py
cotoha_sentiment.py
# -*- coding:utf-8 -*-

import os
import urllib.request
import json
import configparser
import codecs
import csv


class CotohaApi:
    def __init__(self, client_id, client_secret, developer_api_base_url, access_token_publish_url):
        self.client_id = client_id
        self.client_secret = client_secret
        self.developer_api_base_url = developer_api_base_url
        self.access_token_publish_url = access_token_publish_url
        self.getAccessToken()

    def getAccessToken(self):
        url = self.access_token_publish_url
        headers={
            "Content-Type": "application/json;charset=UTF-8"
        }

        data = {
            "grantType": "client_credentials",
            "clientId": self.client_id,
            "clientSecret": self.client_secret
        }
        data = json.dumps(data).encode()
        req = urllib.request.Request(url, data, headers)
        res = urllib.request.urlopen(req)
        res_body = res.read()
        res_body = json.loads(res_body)
        self.access_token = res_body["access_token"]

    # Sentiment Analysis API
    def sentiment(self, sentence):
        url = self.developer_api_base_url + "nlp/v1/sentiment"
        headers={
            "Authorization": "Bearer " + self.access_token,
            "Content-Type": "application/json;charset=UTF-8",
        }
        data = {
            "sentence": sentence
        }
        data = json.dumps(data).encode()
        req = urllib.request.Request(url, data, headers)
        try:
            res = urllib.request.urlopen(req)
        except urllib.request.HTTPError as e:
            print ("<Error> " + e.reason)

        res_body = res.read()
        res_body = json.loads(res_body)
        return res_body

if __name__ == '__main__':
    APP_ROOT = os.path.dirname(os.path.abspath( __file__)) + "/"

    # Get values from config.ini
    config = configparser.ConfigParser()
    config.read(APP_ROOT + "config.ini")
    CLIENT_ID = config.get("COTOHA API", "Developer Client id")
    CLIENT_SECRET = config.get("COTOHA API", "Developer Client secret")
    DEVELOPER_API_BASE_URL = config.get("COTOHA API", "Developer API Base URL")
    ACCESS_TOKEN_PUBLISH_URL = config.get("COTOHA API", "Access Token Publish URL")

    cotoha_api = CotohaApi(CLIENT_ID, CLIENT_SECRET, DEVELOPER_API_BASE_URL, ACCESS_TOKEN_PUBLISH_URL)

       # Obtain sentences to analyze from file/infile.txt
    checkpath = 'file/input.txt'
    song_data = open(checkpath, "r", encoding='utf-8')

    output_file = open('file/output.csv', 'w')
    writer = csv.writer(output_file, lineterminator='\n') # Specify newline code (\n)

    sentence = song_data.readline()
    while sentence:
        print(sentence.strip())
        # Execute API
        result = cotoha_api.sentiment(sentence)

        score = result["result"]["score"]
        print(score)
        sentiment = result["result"]["sentiment"]
        print(sentiment)

        one_row = [score,sentiment]
        writer.writerow(one_row)  # Pass list

        sentence = song_data.readline()

    song_data.close()
    output_file.close()

</div></details>

Analysis Results

Kana Nishino, "Aitakute Aitakute" (I want to see you, I want to see you)

<details><summary>Lyrics (Beginning)</summary><div>

会いたくて 会いたくて 震える
君想うほど遠く感じて
もう一度聞かせて嘘でも
あの日のように"好きだよ"って…

</div></details>
<img width="600" alt="" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/351187/a6ef832b-eb79-20a1-9b81-8f6271569f0a.png">
<img width="600" alt="" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/351187/1cdecff3-0e23-2122-baea-b46383cbcc75.png">
It was not as negative as I thought.

Greeeen, "Kiseki" (Miracle)

<details><summary>Lyrics (Beginning)</summary><div>

明日、今日よりも好きになれる 溢れる想いが止まらない
今もこんなに好きでいるのに 言葉に出来ない
君のくれた日々が積み重なり 過ぎ去った日々2人歩いた『軌跡』

</div></details>
<img width="600" alt="" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/351187/7c6749f1-9ba8-d65f-cf68-b2b9eb8eb915.png">
<img width="600" alt="" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/351187/e85a2aa4-4efa-01cb-ba7f-637d6a67ea1b.png">

A quite positive song.

Sambomaster, "Sekai wa Sore o Ai to Yobu ndaze" (The World Calls That Love)

<details><summary>Lyrics</summary><div>

涙の中にかすかな灯りがともったら
君の目の前で
あたためてた事話すのさ

</div></details>
<img width="600" alt="" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/351187/dc1c63a6-2685-d33f-bf31-ab9adc811f0f.png">
<img width="600" alt="" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/351187/b0c4ae23-c58a-9849-73ee-ce25d4d0c66e.png">
There are more negative lyrics than positive ones, if anything.

SPYAIR, "BEAUTIFUL DAYS"

<details><summary>Lyrics</summary><div>

プラスにもっと変えていける そうやって信じていこう
誰かが君を笑っても 俺は笑ったりしないよ
新たなスタート どんな君も輝いていけるさ
不安&期待で Oh Try yourself

</div></details>
<img width="600" alt="" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/351187/b70da2ee-2397-2dad-0fab-9ee397dd2e05.png">
<img width="600" alt="" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/351187/5f296bc6-085f-589c-1719-bd5f7e42c1a7.png">
I thought this was a very positive song to me, but...

Ken Hirai, "Nonfiction"

<details><summary>Lyrics</summary><div>

描いた夢は叶わないことの方が多い
優れた人を羨んでは自分が嫌になる
浅い眠りに押しつぶされそうな夜もある

</div></details>
<img width="600" alt="" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/351187/649f1fcb-365f-480a-ee61-5089e4006f8f.png">
<img width="600" alt="" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/351187/60c3d6a0-4ba1-c40c-99d9-3242ce070f9c.png">

Somewhat negative.

back number, "Happy End"

<details><summary>Lyrics</summary><div>

さよならが喉の奥につっかえてしまって
咳をするみたいにありがとうって言ったの
次の言葉はどこかとポケットを探しても

</div></details>
<img width="600" alt="" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/351187/75248021-6bf2-08ad-38da-286f93214799.png">
<img width="600" alt="" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/351187/2ea04655-ab07-b282-418e-6f7c5e0050c1.png">
It has a good balance.

Conclusion

I cannot understand the emotions of someone who is "trembling because they want to see someone so much"

In this sentiment analysis, I processed the lyrics line by line, but I think the results are biased due to factors such as word order. If I refine the granularity of the lyrics to be analyzed, I believe I can get more accurate results, so I will try that when I have some time.

Discussion