[PYTHON] Is Vtuber Positive? Is it negative?

Introduction

Are there many positive people on Vtuber? Are there many negative people? This time, I analyzed Vtuber's tweets and measured their positiveness.

As a result, Sana Natori was found to be positive.

[('Neutral', 31), ('Positive', 12), ('Negative', 7)]
1.7142857142857142
A little positive

We also analyzed tweets from DWU and Machinery Tomoko.

What to use

Introduction of COTOHA API

This time, we will analyze emotions with COTOHA API. COTOHA API is an API developed by NTT Communications and uses the results of 40 years of research on Japanese language technology. You can easily perform annoying sentiment analysis just by sending a text. In addition to the sentiment analysis API used this time, there are also syntax analysis and keyword extraction APIs.

Flow to measure positiveness

  1. Get the twitter API key
  2. Get the API key for COTOHA API
  3. Get the target user's tweets with tweepy
  4. Cleanse your tweets
  5. Perform sentiment analysis with COTOHA API
  6. Determine the positiveness by the ratio of positive tweets and negative tweets

Judgment of positive and negative degrees

As a basic policy, we will first analyze about 50 emotions. Of the 50 tweets, if there are more positive tweets than negative tweets, it is considered positive. If the opposite is true, it will be judged as negative. After that, I tried to sort out pretty positive and a little positive by the sense value.

Implementation

This time, I implemented it with google colaboratory. Please see here for the detailed implementation. By rewriting the API key of twitter and the API key of COTOHA API, you can measure the positiveness of your favorite user.

BASE_URL = "https://api.ce-cotoha.com/api/dev/nlp/"
CLIENT_ID = "hogehoge"
CLIENT_SECRET = "hogehoge"

def auth(client_id, client_secret):
    token_url = "https://api.ce-cotoha.com/v1/oauth/accesstokens"
    
    headers = {
        "Content-Type": "application/json",
        "charset": "UTF-8"
    }

    data = {
        "grantType": "client_credentials",
        "clientId": client_id,
        "clientSecret": client_secret
    }
    r = requests.post(token_url,
                      headers=headers,
                      data=json.dumps(data))
    
    return r.json()["access_token"]

def sentiment(sentence, access_token):
    base_url = BASE_URL
    
    headers = {
        "Content-Type": "application/json",
        "charset": "UTF-8",
        "Authorization": "Bearer {}".format(access_token)
    }
    
    data = {
        "sentence": sentence,
        "type": "kuzure"
    }
    
    r = requests.post(base_url + "v1/sentiment",
                      headers=headers,
                      data=json.dumps(data))
    return r

def cleansing_tweet(tweet):
    clean_tweets = []
    for line in tweet:
        temp = line.text
        temp = re.sub('RT .*', '', temp)
        temp = re.sub('@.*', '', temp)
        temp = re.sub('http.*', '', temp)
        temp = re.sub('\n.*', '', temp)
        temp = re.sub('\u3000.*', '', temp)
        if len(temp) != 0:
            clean_tweets.append(temp)
        if len(clean_tweets) >= 50:
            break
    return clean_tweets

def check_positive(result):
    counter = Counter(result)
    print(counter.most_common())
    positive_negative = counter["Positive"]/counter["Negative"]
    print(positive_negative)
    if positive_negative > 1:
        if positive_negative > 4:
            print("Very positive")
        elif positive_negative > 2:
            print("Positive")
        elif positive_negative > 1:
            print("A little positive")
    elif positive_negative < 1:
        if positive_negative < 0.25:
            print("Very negative")
        elif positive_negative < 0.5:
            print("Is negative")
        elif positive_negative < 1:
            print("A little negative")
    elif counter["Positive"] == 0 and counter["Negative"] == 0:
        print("Losing emotions")

result

First of all, let's analyze the tweets of Sunshine Ikezaki to check if the positive judgment is made correctly.

Analysis results of Sunshine Ikezaki

[('Neutral', 32), ('Positive', 16), ('Negative', 2)]
8.0
Very positive

It seems that you can judge correctly. Let's analyze Vtuber.

Analysis result of Sana Natori

[('Neutral', 31), ('Positive', 12), ('Negative', 7)]
1.7142857142857142
A little positive

Analysis result of DWU

[('Neutral', 28), ('Negative', 15), ('Positive', 7)]
0.4666666666666667
Is negative

Analysis result of Machinery Tomoko

[('Neutral', 27), ('Positive', 16), ('Negative', 7)]
2.2857142857142856
Positive

Summary

This time, we acquired tweets, performed sentiment analysis, and judged the degree of positive and negative. As for the future outlook, it would be interesting to judge not only the emotions of the whole sentence but also the emotional words.

Reference link

Recommended Posts

Is Vtuber Positive? Is it negative?
Is it Google Colaboratory?
Negative / Positive Analysis 3 Twitter Negative / Positive Analysis (2)
What is Python? What is it used for?
Is it a character string operation?
Negative / Positive Analysis 1 Application of Text Analysis