[PYTHON] I want to analyze the emotions of people who want to meet and tremble

Thing you want to do

~~ Understand the emotions of people who tremble because they want to meet ~~

Sentiment analysis from the lyrics Is it a bright song? Is it a sad song? I want to judge.

It would be great if we could perform ambiguous searches such as bright songs and sad songs on music distribution services. This year is a difficult year and it's not good, so I'm doing my best listening to bright songs

Try to analyze the lyrics

Analysis tool

COTOHA API Python 3.8.1

Analysis target

A song that trembles because I want to meet

The following has already been categorized in RecoChoku, so I selected a song from them as my hobby.

Bright song

Sad song

code

Does the cactus stop dancing in sentiment analysis? I was allowed to reference.

Implementation
top/
 ├ file/
 │ ├ input.txt   (Lyrics to analyze)
 │ └ output.csv (Output result)
 ├ 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__)) + "/"

    # config.Get ini value
    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)

       # file/infile.Get analysis target sentence from 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') #Line feed code (\n) is specified

    sentence = song_data.readline()
    while sentence:
        print(sentence.strip())
        #API execution
        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()
    

result of analysis

Kana Nishino "I want to meet you"

Lyrics (beginning)
I want to see you, I want to see you, I tremble
Feel as far as you think
Tell me again, even if it's a lie
Like that day"I love you"What ...
…
It wasn't as negative as I expected

Greeeen "Kiseki"

Lyrics (beginning)
Tomorrow, I can love you more than today
I still like it so much, but I can't put it into words
The days you gave me piled up and passed away The days when two people walked "trajectory"
…

Pretty positive song

Sambomaster "The world calls it love"

Lyrics
If there is a faint light in your tears
In front of you
I'm talking about warming up
…
If anything, there are many negative lyrics

SPYAIR 「BEAUTIFUL DAYS」

Lyrics
I can change it more positively Let's believe that way
If someone laughs at you, I won't laugh
A new start Anyone can shine
With anxiety & expectations Oh Try yourself
…
I thought it was a pretty positive song for me ## Ken Hirai "Non-Fiction"
Lyrics
Many of the dreams I have drawn do not come true
I hate myself when I envy good people
Some nights are likely to be crushed by a light sleep
…

Well negative

back number "Happy ending"

Lyrics
Goodbye stuck in the back of my throat
I said thank you like coughing
The next word is even if you look for a pocket somewhere
…
Well-balanced

Summary

~~ I can't understand the emotions of people who tremble because they want to meet ~~

This sentiment analysis was done line by line, I think that the results are biased due to the order of the words. I think that more accurate results will be obtained if the granularity of the lyrics to be analyzed is finer, so I will try it if I have time.

Recommended Posts

I want to analyze the emotions of people who want to meet and tremble
I want to know the features of Python and pip
I tried to verify and analyze the acceleration of Python by Cython
I want to customize the appearance of zabbix
I want to visualize where and how many people are in the factory
I want to grep the execution result of strace
I want to increase the security of ssh connections
I want to use only the normalization process of SudachiPy
I want to judge the authenticity of the elements of numpy array
I want to map the EDINET code and securities number
Keras I want to get the output of any layer !!
I want to know the legend of the IT technology world
I want to find the intersection of a Bezier curve and a straight line (Bezier Clipping method)
I want to get the name of the function / method being executed
I want to record the execution time and keep a log.
I want to manually assign the training parameters of the [Pytorch] model
I want to read the html version of "OpenCV-Python Tutorials" OpenCV 3.1 version
[Introduction to Python] I compared the naming conventions of C # and Python.
I want to use both key and value of Python iterator
I summarized how to change the boot parameters of GRUB and GRUB2
I want to connect remotely to another computer, and the nautilus command
I want to check the position of my face with OpenCV!
I want to know the population of each country in the world.
I want to clear up the question of the "__init__" method and the "self" argument of a Python class.
I want to extract the tag information (title and artist) of a music file (flac, wav).
I want to pin Spyder to the taskbar
I want to output to the console coolly
I want to handle the rhyme part1
I want to handle the rhyme part3
I want to analyze logs with Python
I want to display the progress bar
I want to handle the rhyme part2
I want to handle the rhyme part5
I want to handle the rhyme part4
[Note] I want to completely preprocess the data of the Titanic issue-Age version-
I tried to visualize the age group and rate distribution of Atcoder
I don't want to admit it ... The dynamical representation of Neural Networks
What I did to keep track of the humidity and temperature of the archive
(Python Selenium) I want to check the settings of the download destination of WebDriver
[Python] I tried to analyze the pitcher who achieved no hit no run
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to sort a list in the order of other lists
(Python) I tried to analyze 1 million hands ~ I tried to estimate the number of AA ~
I tried to extract and illustrate the stage of the story using COTOHA
I tried to analyze the negativeness of Nono Morikubo. [Compare with Posipa]
I want to use the Qore SDK to predict the success of NBA players
I want to leave an arbitrary command in the command history of Shell
I want to stop the automatic deletion of the tmp area with RHEL7
Python: I want to measure the processing time of a function neatly
I want to revive the legendary Nintendo combination by making full use of AI and HR Tech!
I tried to notify the update of "Hamelin" using "Beautiful Soup" and "IFTTT"
I want to get the path of the directory where the running file is stored.
I want to visualize the transfer status of the 2020 J League, what should I do?
I tried web scraping to analyze the lyrics.
I want to analyze songs with Spotify API 2
The story of IPv6 address that I want to keep at a minimum
I want to use Python in the environment of pyenv + pipenv on Windows 10
I want to handle the rhyme part7 (BOW)
I want to make a music player and file music at the same time
I tried to touch the API of ebay
I want to use PyTorch to generate something like the lyrics of Japari Park