0
0

More than 3 years have passed since last update.

COTOHA API で品詞分類

Last updated at Posted at 2020-02-20

COTOHA API Portal の使用例です。

次の記事に触発されて書きました。
【Qiita x COTOHA APIプレゼント企画】COTOHA APIで、テキスト解析をしてみよう!

cotoha_api.py は次のページの python3 の CotohaApi をそのまま使っています。

自然言語処理を簡単に扱えると噂のCOTOHA APIをPythonで使ってみた

cotoha01.py
#! /usr/bin/python
# -*- coding:utf-8 -*-
#
# cotoha01.py
#
# ----------------------------------------------------------------------
import sys
import os
import json
from dotenv import load_dotenv

from cotoha_api import CotohaApi
# ----------------------------------------------------------------------
if __name__ == '__main__':

    dotenv_path = '.env'
    load_dotenv(dotenv_path)
    CLIENT_ID = os.environ.get("CLIENT_ID")
    CLIENT_SECRET = os.environ.get("CLIENT_SECRET")
    DEVELOPER_API_BASE_URL = os.environ.get("DEVELOPER_API_BASE_URL")
    ACCESS_TOKEN_PUBLISH_URL = os.environ.get("ACCESS_TOKEN_PUBLISH_URL")
# ----------------------------------------------------------------------
    # COTOHA APIインスタンス生成
    cotoha_api = CotohaApi(CLIENT_ID, CLIENT_SECRET, DEVELOPER_API_BASE_URL, ACCESS_TOKEN_PUBLISH_URL)

    # 解析対象文
    sentence = "特急はくたか"

    # 構文解析API実行
    result = cotoha_api.parse(sentence)

#   sys.stderr.write("len = %d\n" % len(result["result"]))

#   for unit in result["result"]:
#       llx = len(unit['tokens'])
#       sys.stderr.write("llx(tokens) = %d\n" % llx)

    for unit in result["result"]:
        for token in unit['tokens']:
            print(token['form'],token['pos'])
# ----------------------------------------------------------------------
.env
LIENT_ID = AAAAAAAAAAAAAAAAAAAAAAAAAA
CLIENT_SECRET = aaaaaaaaaaaaaaaaaaaaaaaaaa
DEVELOPER_API_BASE_URL = https://api.ce-cotoha.com/api/dev/nlp/
ACCESS_TOKEN_PUBLISH_URL = https://api.ce-cotoha.com/v1/oauth/accesstokens

実行結果

$ ./cotoha01.py 
特急 名詞
は 動詞語幹
く 動詞接尾辞
たか 名詞

Mecab を使っての実行例はこちら

ubuntu 18.10 に mecab をインストール

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0