[PYTHON] How does the COTOHA API judge the feeling of "the moon is beautiful"?

Words are difficult

Isn't words difficult? If you receive it literally, you may get hurt, or you may pass through without noticing the hidden intention and be said to be natural. For example, even if you are suddenly told that the moon is beautiful, you may get a reply like a good audience, saying "Yes." It's a difficult task to understand the meaning of I love you.

This time, I would like to apply the difficult words to the sentiment analysis of COTOHA API to confirm "Can you find the true meaning?"

Preparation

What to use

Execution method

The COTOHA API itself is very easy to use once you register The code to be executed this time looks like this


import os
import urllib.request
import json


def get_sentiment(txt):
    token = get_cotoha_token()

    url = os.environ['COTOHA_URL'] + 'nlp/v1/sentiment'
    header_with_token = {
        "Content-Type": "application/json",
        "Authorization": "Bearer " + token
    }

    obj = {"sentence": txt}
    json_data = json.dumps(obj).encode("utf-8")

    request = urllib.request.Request(
        url, data=json_data, method="POST", headers=header_with_token
    )
    with urllib.request.urlopen(request) as response:
        response_body = response.read().decode("utf-8")
        result_objs = json.loads(response_body)
        res = result_objs['result']
        emotion = ''
        for emo in res['emotional_phrase']:
            emotion += emo['form'] + ': ' + emo['emotion'] + ', '
        print(txt, res['sentiment'], res['score'], emotion)

def get_cotoha_token():
    #COTOHA API authentication
    headers = {"Content-Type": "application/json"}
    auth_url = os.environ['COTOHA_TOKEN_URL']
    auth_obj = {
        "grantType": "client_credentials",
        "clientId": os.environ['COTOHA_ID'],
        "clientSecret": os.environ['COTOHA_SECRET']
    }
    auth_json = json.dumps(auth_obj).encode("utf-8")
    auth_request = urllib.request.Request(
        auth_url, data=auth_json, method="POST", headers=headers
    )

    with urllib.request.urlopen(auth_request) as response:
        auth_body = response.read().decode("utf-8")
        auth_res = json.loads(auth_body)
        token = auth_res['access_token']
        return token

if __name__ == '__main__':

    print('======Enter the text you want to analyze=====')
    txt = input('>  ')

    get_sentiment(txt)

This time it's simple, just grab a token and use it to request text for sentiment analysis. Let's actually try various texts

Sentiment analysis

The moon is beautiful

>The moon is beautiful
The moon is beautiful Positive 0.9111066300514463 It's beautiful: P

It seems that it was judged positive from the phrase It's beautiful By the way, the part of 0.9111066300514463 is called the sentiment score and it seems to be the reliability of the judgment. If I had no idea what the sentiment score was, I found out in the Slack community of the COTOHA API. Slack Community Convenient

By the way I understand "positive", but I didn't understand concrete feelings like in the reference: thinking: thinking: It would be perfect if it was labeled as "favorable"

Reflection Reflection Reflection ry

>I said that I am reflecting on it, but I am reflecting on it. However, I think this is my problem, but I can't see the color of reflection while saying that I am reflecting on it. I regret that this is my own problem.
I said that I am reflecting on it, but I am reflecting on it. However, I think this is my problem, but I can't see the color of reflection while saying that I am reflecting on it. I regret that this is my own problem. Neutral 0.3498954942284627 Reflection: PN

How did you feel about that line? I tried it from my curiosity, but it seems to be "Positive and Negative" because it is PN. I think it was a very complicated feeling I don't know

I'm not angry at all

It's a line that an insanely angry woman might say, but ...

>I'm not angry at all, so go to a drinking party
I'm not angry at all, so go to a drinking party Positive 0.48899415008698344 I'm not angry at all: P

Ah, this is bad I judge it as positive and participate in the drinking party If you go to a drinking party as it is, you will surely not be able to enter the house

I don't like you

Tsundere template Can COTOHA collect the flag? ??

>I don't really like you
I don't really like you, Negative 0.5125582125328727 I don't like it: N

Ohh... Sad flag crusher COTOHA seems to be better to capture a childhood friend character that is gentle and easy to understand

Ugu

Let's analyze Kanon's famous lines I still don't believe it's been over 20 years since Kanon! !!

>Forget about me
Forget about me Neutral 0.2972851940620987 Forget: PN

Isn't this bad? I think I think it's quite sharp to judge the meaning of the word "forget" as Positive and Negative.

result

I dared to analyze only difficult sentences, but I think it was quite interesting. I had the impression that words like "I'm not angry at all" would be judged in writing, but on the other hand, I personally feel convinced that "Forget" was judged as a PN. did In the case I tried this time, only the labels P and N were attached, but for example, if you throw the sentence in the reference,

>Enjoying the spring of life
Enjoying the spring of life Positive 0.19562121911742972 Song:relief,Rejoice

In this way, labels such as "safety" and "pleasant" are also attached. I hope that more labels will be attached around here

I think it's wonderful to be able to analyze the emotions of a sentence just by throwing it so easily. There are many other interesting features in the COTOHA API, so I would like to try them out again.


Recommended Posts

How does the COTOHA API judge the feeling of "the moon is beautiful"?
How to judge that the cross key is input in Python3
I tried using the COTOHA API (there is code on GitHub)
Is the probability of precipitation correct?
Science "Is Saito the representative of Saito?"
From the introduction of GoogleCloudPlatform Natural Language API to how to use it
[Java] [Linux] Investigating how the implementation of Java child processes on Linux is realized
The google search console sitemap api client is in webmasters instead of search console
Judge the authenticity of posted articles by machine learning (Google Prediction API).