Articles using COTOHA API including Kotoha Api I saw a lot, so I decided to touch it while referring to it. You need an account to use the API, so if you are interested, please create one. (I was able to start using it in less time.) In this article, we will use this API to get the morphological analysis results.
import requests
import json
BASE_URL = "https://api.ce-cotoha.com/api/dev/nlp/"
CLIENT_ID = "CLIENT_ID" #Enter the issued ID
CLIENT_SECRET = "CLIENT_SECRET" #Enter the issued secret
#Get an access token
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"]
if __name__ == "__main__":
access_token = auth(CLIENT_ID, CLIENT_SECRET)
# print(access_token)
I'm wondering if you want to output the access token (commented out for the time being), but you can call the API by authenticating and getting the token.
import requests
import json
import sys
BASE_URL = "https://api.ce-cotoha.com/api/dev/nlp/"
CLIENT_ID = "CLIENT_ID" #Enter the issued ID
CLIENT_SECRET = "CLIENT_SECRET" #Enter the issued secret
#Get an access token
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"]
#Morphological analysis
def parse(sentence, access_token):
base_url = BASE_URL
headers = {
"Content-Type": "application/json",
"charset": "UTF-8",
"Authorization": "Bearer {}".format(access_token)
}
data = {
"sentence": sentence,
"type": "default"
}
r = requests.post(base_url + "v1/parse",
headers=headers,
data=json.dumps(data))
return r.json()
if __name__ == "__main__":
document = "test"
args = sys.argv
if len(args) >= 2:
document = str(args[1])
access_token = auth(CLIENT_ID, CLIENT_SECRET)
parse_document = parse(document, access_token)
print(parse_document)
This time I used the parsing API, but if it is available at here, you can use it immediately. I will.
python parse.py i am a bird
{'result': [{'chunk_info': {'id': 0, 'head': 1, 'dep': 'D', 'chunk_head': 0, 'chunk_func': 1, 'links': []}, 'tokens': [{'id': 0, 'form': 'I', 'kana': 'I', 'lemma': 'I', 'pos': 'noun', 'features': ['代noun'], 'dependency_labels': [{'token_id': 1, 'label': 'case'}], 'attributes': {}}, {'id': 1, 'form': 'Is', 'kana': 'C', 'lemma': 'Is', 'pos': 'Conjunctive particles', 'features': [], 'attributes': {}}]}, {'chunk_info': {'id': 1, 'head': -1, 'dep': 'O', 'chunk_head': 0, 'chunk_func': 1, 'links': [{'link': 0, 'label': 'aobject'}], 'predicate': []}, 'tokens': [{'id': 2, 'form': 'bird', 'kana': 'bird', 'lemma': 'bird', 'pos': 'noun', 'features': [], 'dependency_labels': [{'token_id': 0, 'label': 'nmod'}, {'token_id': 3, 'label': 'cop'}], 'attributes': {}}, {'id': 3, 'form': 'is', 'kana': 'death', 'lemma': 'is', 'pos': 'Judgment', 'features': ['stop'], 'attributes': {}}]}], 'status': 0, 'message': ''}
Obtaining an account was smooth, and it was convenient to use immediately. Morphological analysis is an area of personal interest that I have the opportunity to experience in actual work. Currently, I am registering for free, so it is for Developers, but I am also interested in the terminology dictionary and extensions of for Enterprise, so I would like to touch it if there is an opportunity. I'd like to do something with the COTOHA API, but I can't come up with any ideas because I don't have the creativity ... I would like to post something as soon as I can think of it.
Recommended Posts