COTOHA API is a Japanese natural language processing API. It has various functions and can be used for 1000 / day even with a free account. It is a very fun API.
By the way, do you all know Isono-kun? Yes, it's Katsuo Isono, a national anime.
This Isono-kun is called something in the play, but when it is inconvenient, he does not hear it or reacts so-called "human". This time, I will try various things with COTOHA API to realize this.
For details on how to use the COTOHA API, please refer to the [article] I wrote earlier (https://qiita.com/mosamosa/items/e63f6e582a206659dc2b) and various other people who have written articles. ..
I:Hey Isono. Let's play baseball.
Isono:Read?
I:Isono-kun
Isono:Read?
I:Isono is no good
Isono:...
I:Isono-kun is cool
Isono:Read?
I:Speaking of Isono, Iso Nori
Isono:...
The number of samples is small, but what about? Isn't it like Isono?
In the COTOHA API, create a wrapper function to display whether it recognizes that "Isono was called". If you think you have been called, "Did you call?" If not, "..." is displayed.
cotoha_exp = [
"Isono",
"Isono-kun",
]
def call_cotoha(res_func,texts,cotoha_token,cotoha_exp):
"""
res_func:A function that determines if it has been called. Receives a statement and returns true false.
texts:Sentence array to be judged
cotoha_token:Access token
cotoha_exp:For determining Isono and its name.
"""
for t in texts:
does_ans = res_func(t,cotoha_token,cotoha_exp)
if does_ans:
answer = "Read?"
else:
answer = "..."
print("I:{} \n Isono:{}\n".format(t,answer))
The simplest way for Isono-kun to detect his call is to morphologically analyze the text and react if it contains Isono or something similar.
Simply put, if Isono is included in the sentence, it will react.
The parsing documentation is here [https://api.ce-cotoha.com/contents/reference/apireference.html#parsing_response_morpheme)
def parser_base(text,cotoha_token,cotoha_exp):
header = {
"Content-Type":"application/json",
"Authorization":"Bearer "+cotoha_token
}
datas = {
"sentence":text
}
r = requests.post(api_base+"nlp/v1/parse",headers=header,data=json.dumps(datas))
parsed = json.loads(r.text)
for res in parsed["result"]:
for tok in res["tokens"]:
for exp in cotoha_exp:
if exp in tok["form"]:
return True
return False
call_cotoha(parser_base,texts,cotoha_token,cotoha_exp)
I:Hey Isono. Let's play baseball.
Isono:Read?
I:Isono-kun
Isono:Read?
I:Isono is no good
Isono:Read?
I:Isono-kun is cool
Isono:Read?
I:Speaking of Isono, Iso Nori
Isono:Read?
Of course, it's natural, but it reacts to everything. I feel a little over-conscious.
Even if you don't bother to morphologically analyze and see all the tokens, the COTOHA API has a function called named entity recognition, which makes it easy to extract personal names, place names, numerical expressions, etc. If you use this, you can do something similar to 1. in a slightly stylish way.
def ne_base(text,cotoha_token,cotoha_exp):
header = {
"Content-Type":"application/json",
"Authorization":"Bearer "+cotoha_token
}
datas = {
"sentence":text
}
r = requests.post(api_base+"nlp/v1/ne",headers=header,data=json.dumps(datas))
parsed = json.loads(r.text)
for res in parsed["result"]:
for exp in cotoha_exp:
if exp in res["form"]:
return True
return False
I:Hey Isono. Let's play baseball.
Isono:Read?
I:Isono-kun
Isono:Read?
I:Isono is no good
Isono:Read?
I:Isono-kun is cool
Isono:Read?
I:Speaking of Isono, Iso Nori
Isono:Read?
After all it is excessive self-consciousness. Well, it's about age. The reputation of the people around me is anxious.
Unlike named entity extraction, the COTOHA API has a function called keyword extraction, and I would like to make Isono react when it becomes a keyword in a conversation. If you feel that you are the subject, you should react.
def key_base(text,cotoha_token,cotoha_exp):
header = {
"Content-Type":"application/json",
"Authorization":"Bearer "+cotoha_token
}
datas = {
"document":text
}
r = requests.post(api_base+"nlp/v1/keyword",headers=header,data=json.dumps(datas))
parsed = json.loads(r.text)
for res in parsed["result"]:
for exp in cotoha_exp:
if exp in res["form"]:
return True
return False
I:Hey Isono. Let's play baseball.
Isono:...
I:Isono-kun
Isono:...
I:Isono is no good
Isono:...
I:Isono-kun is cool
Isono:...
I:Speaking of Isono, Iso Nori
Isono:...
It doesn't react at all. Personal names do not seem to be keywords. This is Isono-kun's reaction when he did something wrong.
Will it respond if I call it even with this?
I:Isono-kun, Isono-kun, Isono-kun, Isono-kun, Isono-kun
Isono:...
It's no good ... Isono-kun seems to be dead ...
The COTOHA API has a function to judge the similarity between two sentences, so I would like to use this. Specifically, if the similarity with the sentence "Call Isono" is high, I will judge that Isono is being called.
def sim_base(text,cotoha_token,cotoha_exp):
header = {
"Content-Type":"application/json",
"Authorization":"Bearer "+cotoha_token
}
datas = {
"s1":text,
"s2":"Call Isono"
}
r = requests.post(api_base+"nlp/v1/similarity",headers=header,data=json.dumps(datas))
parsed = json.loads(r.text)
if parsed["result"]["score"] > 0.5:
return True
else:
return False
I:Hey Isono. Let's play baseball.
Isono:Read?
I:Isono-kun
Isono:Read?
I:Isono is no good
Isono:...
I:Isono-kun is cool
Isono:...
I:Speaking of Isono, Iso Nori
Isono:...
Oh, isn't it pretty good? Only the first and second call sentences responded. There is no reaction method that is quite practical for wakeups such as chatbots.
The COTOHA API can determine what type of statement a sentence is. (Descriptions / questions / instructions can be taken in more detail. For details, see Documentation.)
I will try to use this to react only when a sentence is interrogative. (I wish I had a sentence type called a call, but I couldn't find it ...)
Of course, if it is an interrogative form, it will not work, so if it is an interrogative form, morphological analysis, which is considered to be the most sensitive, is performed to determine whether COTOHA is included.
def parser_sent_type_base(text,cotoha_token,cotoha_exp):
header = {
"Content-Type":"application/json",
"Authorization":"Bearer "+cotoha_token
}
datas = {
"sentence":text
}
r = requests.post(api_base+"nlp/v1/sentence_type",headers=header,data=json.dumps(datas))
parsed = json.loads(r.text)
if parsed["result"]["modality"] == "interrogative":
return parser_base(text,cotoha_token,cotoha_exp)
else:
return False
I:Hey Isono. Let's play baseball.
Isono:...
I:Isono-kun
Isono:...
I:Isono is no good
Isono:...
I:Isono-kun is cool
Isono:...
I:Speaking of Isono, Iso Nori
Isono:...
Hmm ... subtle ... it looks like no good.
The COTOHA API has a function called sentiment analysis that allows you to determine whether a sentence is positive or negative. So ** When the sentence is positive and contains Isono → When Isono is likely to be praised **
I just want to make it react. Isono-kun is convenient. Let's all get in the mood for Isono.
def senti_parser_base(text,cotoha_token,cotoha_exp):
header = {
"Content-Type":"application/json",
"Authorization":"Bearer "+cotoha_token
}
datas = {
"sentence":text
}
r = requests.post(api_base+"nlp/v1/sentiment",headers=header,data=json.dumps(datas))
parsed = json.loads(r.text)
if parsed["result"]["sentiment"] == "Positive":
return parser_base(text,cotoha_token,cotoha_exp)
else:
return False
I:Hey Isono. Let's play baseball.
Isono:...
I:Isono-kun
Isono:...
I:Isono is no good
Isono:...
I:Isono-kun is cool
Isono:Read?
I:Speaking of Isono, Iso Nori
Isono:...
It seems that the characteristics of Isono can be reproduced quite well.
From the results of the experiments so far
I feel that Isono can be reproduced by making it react when either of them is satisfied. I will try it.
def ISONO(text,cotoha_token,cotoha_exp):
flag1 = sim_base(text,cotoha_token,cotoha_exp)
flag2 = senti_parser_base(text,cotoha_token,cotoha_exp)
return (flag1 or flag2)
I:Hey Isono. Let's play baseball.
Isono:Read?
I:Isono-kun
Isono:Read?
I:Isono is no good
Isono:...
I:Isono-kun is cool
Isono:Read?
I:Speaking of Isono, Iso Nori
Isono:...
Artificial intelligence ** ISONO ** Bomb here (exaggeration)
I've tried various things using as many APIs as possible, but if you want to try some features that aren't available yet (summary, anaphora resolution, etc.), please register here.
I intended to write it completely, but I thought that it could be used for wakeups such as chatbots.
By the way, I feel that I can create a negative personality if I try to react only to sentences that are likely to be denied. I feel like I can reproduce it if I have a certain personality ...
** Swastika application swastika ** seems to be worthwhile.
Recommended Posts