自然言語処理のCOTOHA APIをPHPで使ってみた

COTOHA API Portal:
https://api.ce-cotoha.com/contents/

APIを使う前に、解析デモを使ってみます。

解析デモではJSON形式では見れませんが、視覚的に分かりやすくなっています。

COTOHAに登録

項目多いですが、登録を済ませます。無料版のほうで。

ログイン出来たらこの画面を開いておきます。

PHPでAPIを叩く

POSTリクエストでしか動きませんので、少し大変です。GETだったらすごい簡単なんですが。

まずトークンを取得し、そのトークンを用いてAPIを利用します。

情報が少ないので、苦労してしまいました。コードはこちらです。

<?php
// 出力用文字コードセット
header("Content-Type:text/html; charset=UTF-8");

// crientId, crientSecletは自分のプロフィール参照
// getTokenUrl, parseUrlは大体固定
$crientId = "*********************************";
$crientSeclet = "**************";
$getTokenUrl = "https://api.ce-cotoha.com/v1/oauth/accesstokens";
$parseUrl = "https://api.ce-cotoha.com/api/dev/nlp/v1/parse";

// ====== トークン取得 ======

// ヘッダ
$getTokenHttpHeader = [
    "Content-Type: application/json; charser=UTF-8"
];

// ボディ
$getTokenHttpBody = [
    "grantType" => "client_credentials",
    "clientId" => $crientId,
    "clientSecret" => $crientSeclet
];

// HTTPリクエスト組み立て
$getTokenContext = stream_context_create([
    "http" => [
        "method" => "POST",
        "header" => implode(PHP_EOL, $getTokenHttpHeader),
        "content"=>  json_encode($getTokenHttpBody),
        "ignore_errors" => true
    ]
]);

// リクエスト送信
$getTokenResponse = file_get_contents($getTokenUrl, false, $getTokenContext);

// レスポンスをデコード(JSON -> 連想配列)
$getTokenResult = json_decode($getTokenResponse, true);

// お目当てのトークン
$token = $getTokenResult["access_token"];

// ====== 解析スタート ======

// ヘッダ
$parseHeader = [
    "Authorization: Bearer " . $token,
    "Content-Type: application/json; charser=UTF-8"
];

// ボディ(sentenceに文章を入れます)
$parseBody = [
    "sentence" => "cnnを用いた姿勢特徴抽出と行動遷移を考慮した人物行動認識。"
];

// HTTPリクエスト組み立て
$parseContext = stream_context_create([
    "http" => [
        "method" => "POST",
        "header" => implode(PHP_EOL, $parseHeader),
        "content"=> json_encode($parseBody),
        "ignore_errors" => true
    ]
]);

// 解析リクエスト
$parseResponse = file_get_contents($parseUrl, false, $parseContext);

// 解析した結果をデコード
$parseResult = json_decode($parseResponse, true);

// 出力
print "<pre>";
print_r($parseResult);
print "</pre>";

そして結果がこちら。

Array
(
    [result] => Array
        (
            [0] => Array
                (
                    [chunk_info] => Array
                        (
                            [id] => 0
                            [head] => 1
                            [dep] => D
                            [chunk_head] => 0
                            [chunk_func] => 1
                            [links] => Array
                                (
                                )

                        )

                    [tokens] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 0
                                    [form] => cnn
                                    [kana] => シーエヌエヌ
                                    [lemma] => cnn
                                    [pos] => 名詞
                                    [features] => Array
                                        (
                                            [0] => Undef
                                        )

                                    [dependency_labels] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [token_id] => 1
                                                    [label] => case
                                                )

                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 1
                                    [form] => を
                                    [kana] => ヲ
                                    [lemma] => を
                                    [pos] => 格助詞
                                    [features] => Array
                                        (
                                            [0] => 連用
                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                        )

                )

            [1] => Array
                (
                    [chunk_info] => Array
                        (
                            [id] => 1
                            [head] => 2
                            [dep] => D
                            [chunk_head] => 0
                            [chunk_func] => 1
                            [links] => Array
                                (
                                    [0] => Array
                                        (
                                            [link] => 0
                                            [label] => object
                                        )

                                )

                            [predicate] => Array
                                (
                                    [0] => past
                                )

                        )

                    [tokens] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 2
                                    [form] => 用い
                                    [kana] => モチイ
                                    [lemma] => 用いる
                                    [pos] => 動詞語幹
                                    [features] => Array
                                        (
                                            [0] => A
                                        )

                                    [dependency_labels] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [token_id] => 0
                                                    [label] => dobj
                                                )

                                            [1] => Array
                                                (
                                                    [token_id] => 3
                                                    [label] => aux
                                                )

                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 3
                                    [form] => た
                                    [kana] => タ
                                    [lemma] => た
                                    [pos] => 動詞接尾辞
                                    [features] => Array
                                        (
                                            [0] => 連体
                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                        )

                )

            [2] => Array
                (
                    [chunk_info] => Array
                        (
                            [id] => 2
                            [head] => 3
                            [dep] => P
                            [chunk_head] => 2
                            [chunk_func] => 3
                            [links] => Array
                                (
                                    [0] => Array
                                        (
                                            [link] => 1
                                            [label] => adjectivals
                                        )

                                )

                        )

                    [tokens] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 4
                                    [form] => 姿勢
                                    [kana] => シセイ
                                    [lemma] => 姿勢
                                    [pos] => 名詞
                                    [features] => Array
                                        (
                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 5
                                    [form] => 特徴
                                    [kana] => トクチョウ
                                    [lemma] => 特徴
                                    [pos] => 名詞
                                    [features] => Array
                                        (
                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                            [2] => Array
                                (
                                    [id] => 6
                                    [form] => 抽出
                                    [kana] => チュウシュツ
                                    [lemma] => 抽出
                                    [pos] => 名詞
                                    [features] => Array
                                        (
                                            [0] => 動作
                                        )

                                    [dependency_labels] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [token_id] => 2
                                                    [label] => acl
                                                )

                                            [1] => Array
                                                (
                                                    [token_id] => 5
                                                    [label] => compound
                                                )

                                            [2] => Array
                                                (
                                                    [token_id] => 4
                                                    [label] => compound
                                                )

                                            [3] => Array
                                                (
                                                    [token_id] => 7
                                                    [label] => cc
                                                )

                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                            [3] => Array
                                (
                                    [id] => 7
                                    [form] => と
                                    [kana] => ト
                                    [lemma] => と
                                    [pos] => 格助詞
                                    [features] => Array
                                        (
                                            [0] => 連用
                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                        )

                )

            [3] => Array
                (
                    [chunk_info] => Array
                        (
                            [id] => 3
                            [head] => 4
                            [dep] => D
                            [chunk_head] => 1
                            [chunk_func] => 2
                            [links] => Array
                                (
                                    [0] => Array
                                        (
                                            [link] => 2
                                            [label] => other
                                        )

                                )

                        )

                    [tokens] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 8
                                    [form] => 行動
                                    [kana] => コウドウ
                                    [lemma] => 行動
                                    [pos] => 名詞
                                    [features] => Array
                                        (
                                            [0] => 動作
                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 9
                                    [form] => 遷移
                                    [kana] => センイ
                                    [lemma] => 遷移
                                    [pos] => 名詞
                                    [features] => Array
                                        (
                                            [0] => 動作
                                        )

                                    [dependency_labels] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [token_id] => 6
                                                    [label] => conj
                                                )

                                            [1] => Array
                                                (
                                                    [token_id] => 8
                                                    [label] => compound
                                                )

                                            [2] => Array
                                                (
                                                    [token_id] => 10
                                                    [label] => case
                                                )

                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                            [2] => Array
                                (
                                    [id] => 10
                                    [form] => を
                                    [kana] => ヲ
                                    [lemma] => を
                                    [pos] => 格助詞
                                    [features] => Array
                                        (
                                            [0] => 連用
                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                        )

                )

            [4] => Array
                (
                    [chunk_info] => Array
                        (
                            [id] => 4
                            [head] => 5
                            [dep] => D
                            [chunk_head] => 0
                            [chunk_func] => 2
                            [links] => Array
                                (
                                    [0] => Array
                                        (
                                            [link] => 3
                                            [label] => object
                                        )

                                )

                            [predicate] => Array
                                (
                                    [0] => past
                                )

                        )

                    [tokens] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 11
                                    [form] => 考慮
                                    [kana] => コウリョ
                                    [lemma] => 考慮
                                    [pos] => 名詞
                                    [features] => Array
                                        (
                                            [0] => 動作
                                        )

                                    [dependency_labels] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [token_id] => 9
                                                    [label] => dobj
                                                )

                                            [1] => Array
                                                (
                                                    [token_id] => 12
                                                    [label] => aux
                                                )

                                            [2] => Array
                                                (
                                                    [token_id] => 13
                                                    [label] => aux
                                                )

                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 12
                                    [form] => し
                                    [kana] => シ
                                    [lemma] => し
                                    [pos] => 動詞活用語尾
                                    [features] => Array
                                        (
                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                            [2] => Array
                                (
                                    [id] => 13
                                    [form] => た
                                    [kana] => タ
                                    [lemma] => た
                                    [pos] => 動詞接尾辞
                                    [features] => Array
                                        (
                                            [0] => 連体
                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                        )

                )

            [5] => Array
                (
                    [chunk_info] => Array
                        (
                            [id] => 5
                            [head] => -1
                            [dep] => O
                            [chunk_head] => 2
                            [chunk_func] => 2
                            [links] => Array
                                (
                                    [0] => Array
                                        (
                                            [link] => 4
                                            [label] => adjectivals
                                        )

                                )

                        )

                    [tokens] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 14
                                    [form] => 人物
                                    [kana] => ジンブツ
                                    [lemma] => 人物
                                    [pos] => 名詞
                                    [features] => Array
                                        (
                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 15
                                    [form] => 行動
                                    [kana] => コウドウ
                                    [lemma] => 行動
                                    [pos] => 名詞
                                    [features] => Array
                                        (
                                            [0] => 動作
                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                            [2] => Array
                                (
                                    [id] => 16
                                    [form] => 認識
                                    [kana] => ニンシキ
                                    [lemma] => 認識
                                    [pos] => 名詞
                                    [features] => Array
                                        (
                                            [0] => 動作
                                        )

                                    [dependency_labels] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [token_id] => 11
                                                    [label] => acl
                                                )

                                            [1] => Array
                                                (
                                                    [token_id] => 15
                                                    [label] => compound
                                                )

                                            [2] => Array
                                                (
                                                    [token_id] => 14
                                                    [label] => compound
                                                )

                                            [3] => Array
                                                (
                                                    [token_id] => 17
                                                    [label] => punct
                                                )

                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                            [3] => Array
                                (
                                    [id] => 17
                                    [form] => 。
                                    [kana] => 
                                    [lemma] => 。
                                    [pos] => 句点
                                    [features] => Array
                                        (
                                        )

                                    [attributes] => Array
                                        (
                                        )

                                )

                        )

                )

        )

    [status] => 0
    [message] => 
)

僕は、学校の制作のLINEBot開発に取り入れてみました。

フリーワード検索の文章版みたいなものを実装しようとしたのですが、単語区切りにして名詞や形容詞語幹を抽出してSQLを生成する事くらいしかできませんでした…。

割とあっさりとCOTOHA APIを使わせてもらいました。

もちろん商用利用などは有料となっているので、あくまで学校の制作内だけで。

他にもいろんな機能があるので詳しくはリファレンスで。

COTOHA リファレンス:
https://api.ce-cotoha.com/contents/reference.html

The present writer kazz.

関連記事

+メインPCをwindowsからLinuxにして満足している
+ブラウザで遊べるリバーシを作る
+「これ1冊でできる!ラズベリーパイ超入門」を読んで
+ブラウザで動くオンライン対戦リバーシ作ってみた
+PHPで簡易LINEBotを作ってみた