[PYTHON] I tried to touch the COTOHA API

Introduction

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.

First of all, authentication

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.

Morphological analysis (parsing)

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.

Run

python parse.py i am a bird

result

{'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': ''}

in conclusion

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

I tried to touch the COTOHA API
I tried to touch the API of ebay
I tried to touch Tesla's API
[First COTOHA API] I tried to summarize the old story
I tried to touch jupyter
I tried to touch the CSV file with Python
I tried to create Quip API
I tried the Naro novel API 2
I tried to touch Python (installation)
I tried the Naruro novel API
I tried to move the ball
I tried using the checkio API
I tried to estimate the interval.
I tried to touch the COTOHA API
Play with puns using the COTOHA API
I tried to get various information from the codeforces API
I tried to recognize the wake word
I tried to score the syntax that was too humorous and humorous using the COTOHA API.
I tried to summarize the graphical modeling.
I tried to estimate the pi stochastically
I tried to make a Web API
I tried to touch Python (basic syntax)
I tried using the BigQuery Storage API
I tried using the COTOHA API (there is code on GitHub)
I tried to debug.
I tried to paste
I tried hitting the Qiita API from go
I tried to optimize while drying the laundry
I tried to save the data with discord
I tried using the Google Cloud Vision API
I tried to correct the keystone of the image
I tried to touch Python's GUI library "PySimple GUI"
Qiita Job I tried to analyze the job offer
LeetCode I tried to summarize the simple ones
I tried to implement the traveling salesman problem
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
I tried to get the authentication code of Qiita API with Python.
I tried to extract and illustrate the stage of the story using COTOHA
I tried to get the movie information of TMDb API with Python
Using COTOHA, I tried to follow the emotional course of Run, Melos!
I tried to learn the sin function with chainer
I tried to learn PredNet
I tried to detect the iris from the camera image
I tried to summarize the basic form of GPLVM
I tried to predict the J-League match (data analysis)
I tried to organize SVM.
I tried to solve the soma cube with python
I tried to implement PCANet
I touched the Qiita API
I tried to approximate the sin function using chainer
I tried the changefinder library!
I tried to uncover our darkness with Chatwork API
I tried to put pytest into the actual battle
[Python] I tried to graph the top 10 eyeshadow rankings
I tried to reintroduce Linux
I tried using the API of the salmon data project
I tried to visualize the spacha information of VTuber
I tried to introduce Pylint
I tried to erase the negative part of Meros
I tried to solve the problem with Python Vol.1
I tried to summarize SparseMatrix