[PYTHON] What happens when Ikura speaks your words?

Introduction

There is a character called "Ikura-chan" among the characters in the anime "Sazae-san" that is broadcast on Fuji TV every Sunday at 6:30.

Ikura is about one and a half years old and can basically speak only the following three words. [^ 1] [^ 2]

--Hi --Churn --Baboo

Then, what words do Ikura say when Ikura speaks the words we usually speak? This time, as a theory, I will try to estimate based on the degree of similarity.

Implementation method

I used the Similarity Calculation API of COTOHA API. ..

Since this API calculates the degree of similarity using the semantic information of words contained in the text, it seems that it is possible to estimate the similarity between texts containing different words.

Therefore, it seems that Ikura can estimate what kind of meaning each word is used properly.

Click here for source code
Please use the CLIENT_ID and CLIENT_SECRET obtained by yourself.
import requests
import json
import numpy as np

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

def get_token(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 similarity(s1, s2, access_token):
    base_url = BASE_URL
    
    headers = {
        "Content-Type": "application/json",
        "charset": "UTF-8",
        "Authorization": "Bearer {}".format(access_token)
    }
    
    data = {
        "s1": s1,
        "s2": s2,
        "type": "kuzure"
    }
    
    r = requests.post(base_url + "v1/similarity",
                      headers=headers,
                      data=json.dumps(data))
    return r.json()["result"]["score"]

def said_ikurachan(sentence):
  access_token = get_token(CLIENT_ID, CLIENT_SECRET)
  ikurachan_words = ["Hi", "Churn", "Baboo"]
  similarity_arr = []
  for word in ikurachan_words:
    similarity_arr.append(similarity(sentence, word, access_token))
  return ikurachan_words[np.argmax(np.array(similarity_arr))]

Execution result

First of all, from the greeting system

said_ikurachan("Good morning", access_token)
# => 'Hi'

said_ikurachan("Hello", access_token)
# => 'Hi'

said_ikurachan("Good evening", access_token)
# => 'Hi'

The greeting looks good with "Hi".

Next, let's see what the quote looks like.

said_ikurachan("What is a genius?%Inspiration and 99%Is an effort", access_token)
# => 'Churn'

said_ikurachan("Stay hungry, stay foolish", access_token)
# => 'Churn'

said_ikurachan("The earth was blue", access_token)
# => 'Churn'

The quote may be "churn".

Finally, let's see what the dissatisfaction will be.

said_ikurachan("Why do i have to struggle for him")
# => 'Baboo'

said_ikurachan("Is it really meaningful to do this?")
# => 'Baboo'

said_ikurachan("Why do I have to meet this kind of thing?")
# => 'Baboo'

Dissatisfaction seems to be "Baboo".

in conclusion

Churn. Churn.

(This time, I estimated which word to say if the word we were saying was Ikura-chan, using the similarity calculation API of the COTOHA API. It's just one theory, so I don't know if this is true or not. ,I'm glad if you can use it as a reference.)

reference

Survive the SNS era with social filters

[^ 1]: From the section of Ikura Namino on the Official Site [^ 2]: It seems that there were times when opus numbers No. 2433 to No. 3991 spoke different words.

Recommended Posts

What happens when Ikura speaks your words?
[Question] What happens when I use% in python?
What happens when an amateur completes 100 knocks of language processing?