For more information here (I'm very happy to have a star!)
If you want to use it quickly, please refer to the code below.
$ pip install cotoha_at_python
from cotohacall.main import cotoha_call
import os
os.environ['CLIENT_ID'] = 'Your ID'
os.environ['CLIENT_SECRET'] = 'Ypur Secret'
sentence = 'An API service that realizes highly accurate natural language analysis of Japanese. You can easily use the natural language analysis technology that makes use of the research results of the NTT Group for more than 40 years with the COTOHA API.'
cotoha_call('keyword', sentence)
# Return
"""
[{'form': 'High precision', 'score': 20.0},
{'form': 'Realization', 'score': 16.8278},
{'form': 'Easy', 'score': 10.8133},
{'form': 'reaserch result', 'score': 10.0},
{'form': 'cotoha api', 'score': 10.0}]
"""
Already a super-easy explanation about how to use COTOHA (I tried using the COTOHA API rumored to be easy to handle natural language processing in Python and "Mentos The result of having COTOHA summarize "Memories of Go". With COTOHA's fastest tutorial)
[Qiita x COTOHA API present plan] I wrote it myself. ・ Because it is troublesome to build an environment, use Google Colab! ・ You can use it with just one copy without thinking about anything!
I wanted something like that, so I published it as an article.
If you read this article, you will be able to analyze it instantly with just one copy ...! (Should).
python
cotoha_call("ne", "I want to eat Black Thunder")
If you enter The analysis result of "** Black Thunder wants to eat **" (this is named entity recognition) is returned.
===> Named entity recognition ===> {'message': '', 'result': [{'begin_pos': 0, 'class': 'ART', 'end_pos': 8, 'extended_class': 'Product_Other', 'form':'Black Thunder', 'source': 'basic', 'std_form':'Black Thunder'}], 'status': 0}
The "ne" can be changed to (it should cover all apis available in developers)
API name | input |
---|---|
Parsing | parse |
Named entity recognition | ne |
Keyword extraction | keyword |
Resolution analysis | coref |
Similarity calculation | simi |
Sentence type judgment | sen_type |
User attribute estimation(β) | user_at |
Removal of stagnation(β) | filter |
Speech recognition error detection(β) | detect |
Sentiment analysis | senti |
wrap up(β) | summary |
When I try everything using the for statement, it looks like this →
python
for api_type in ["ne", "parse", "coref", "keyword", "simi", "sen_type", "user_at", "filter", "detect", "senti", "summary"]:
cotoha_call(api_type, "I want to eat Black Thunder.")
print("\n") #Line breaks to make the results easier to see
print("Analysis finished!")
-Register as a user from COTOHA API and get client_id and client_secret (user registration was pretty easy to understand).
・ If you are in the "Python is what?" State but want to try it, this is a super-easy explanation (I tried using the COTOHA API, which is rumored to be able to handle natural language processing easily. Please read / gossy5454 / items / 83072418fb0c5f3e269f)).
Rewrite client ID and client secret after copying
# -*- coding:utf-8 -*-
#Reference: https://qiita.com/gossy5454/items/83072418fb0c5f3e269f#python%E3%81%A7%E4%BD%BF%E3%81%A3%E3%81%A6%E3%81%BF%E3%81%9F
import os
import urllib.request
import json
import configparser
import codecs
import sys
client_id = "Client ID"
client_secret = "Client secret"
developer_api_base_url = "https://api.ce-cotoha.com/api/dev/nlp/"
access_token_publish_url = "https://api.ce-cotoha.com/v1/oauth/accesstokens"
api_name_show_switch = 1 #Set to 0 if you do not want to display the api name in the output result
def cotoha_call(api_type, sentence_1, sentence_2 = "I want to eat white thunder", sent_len = 1, ):
#Get access token
def getAccessToken():
#Access token acquisition URL specification
url = access_token_publish_url
#Header specification
headers={
"Content-Type": "application/json;charset=UTF-8"
}
#Request body specification
data = {
"grantType": "client_credentials",
"clientId": client_id,
"clientSecret": client_secret
}
#Encode request body specification to JSON
data = json.dumps(data).encode()
#Request generation
req = urllib.request.Request(url, data, headers)
#Send a request and receive a response
res = urllib.request.urlopen(req)
#Get response body
res_body = res.read()
#Decode the response body from JSON
res_body = json.loads(res_body)
#Get an access token from the response body
access_token = res_body["access_token"]
return access_token
#API URL specification
if api_type == "parse":
api_name = "Parsing"
base_url_footer = "v1/" + api_type
request_body_type = 1
elif api_type == "ne":
api_name = "Named entity recognition"
base_url_footer = "v1/" + api_type
request_body_type = 1
elif api_type == "keyword":
api_name = "Keyword extraction"
base_url_footer = "v1/" + api_type
request_body_type = 2
elif api_type == "coref":
api_name = "Resolution analysis"
base_url_footer = "v1/coreference"
request_body_type = 2
elif api_type == "simi":
api_name = "Similarity calculation"
base_url_footer = "v1/similarity"
request_body_type = 3
elif api_type == "sen_type":
api_name = "Sentence type judgment"
base_url_footer = "v1/sentence_type"
request_body_type = 1
elif api_type == "user_at":
api_name = "User attribute estimation (β)"
base_url_footer = "beta/user_attribute"
request_body_type = 2
elif api_type == "filter":
api_name = "Stagnation removal (β)"
base_url_footer = "beta/remove_filler"
request_body_type = 4
elif api_type == "detect":
api_name = "Speech recognition error detection (β)"
base_url_footer = "beta/detect_misrecognition"
request_body_type = 1
elif api_type == "senti":
api_name = "Sentiment analysis"
base_url_footer = "v1/sentiment"
request_body_type = 1
elif api_type == "summary":
api_name = "Summary (β)"
base_url_footer = "beta/summary"
request_body_type = 5
else :
print("Api Type Error.")
sys.exit()
if api_name_show_switch == 1:
print("===>\n" + api_name + "\n===>")
url = developer_api_base_url + base_url_footer
#Header specification
headers={
"Authorization": "Bearer " + getAccessToken(), #access_token,
"Content-Type": "application/json;charset=UTF-8",
}
#Request body specification
if request_body_type == 1:
data = {
"sentence": sentence_1
}
elif request_body_type == 2:
data = {
"document": sentence_1
}
elif request_body_type == 3:
data = {
"s1": sentence_1,
"s2": sentence_2
}
elif request_body_type == 4:
data = {
"text": sentence_1
}
elif request_body_type == 5:
data = {
"document": sentence_1,
"sent_len": sent_len
}
#Encode request body specification to JSON
data = json.dumps(data).encode()
#Request generation
req = urllib.request.Request(url, data, headers)
#Send a request and receive a response
try:
res = urllib.request.urlopen(req)
#What to do if an error occurs in the request
except urllib.request.HTTPError as e:
#If the status code is 401 Unauthorized, get the access token again and request again
if e.code == 401:
access_token = getAccessToken()
headers["Authorization"] = "Bearer " + access_token
req = urllib.request.Request(url, data, headers)
res = urllib.request.urlopen(req)
#Show cause for errors other than 401
else:
print ("<Error> " + e.reason)
#sys.exit()
#Get response body
res_body = res.read()
#Decode the response body from JSON
res_body = json.loads(res_body)
#Get analysis result from response body
return res_body
later
python
cotoha_call("input", "The text you want to analyze")
The analysis starts at!
Those who participate in the project Let's do our best!
Recommended Posts