COTOHA API で構文解析 (PHP)


COTOHA API Portal の使用例です。

parsing.php
#! /usr/bin/php
<?php
// ------------------------------------------------------------------
//  parsing.php
//
//                      Feb/26/2020
//
// ------------------------------------------------------------------
include('Requests/library/Requests.php');
require_once './vendor/autoload.php';

// ------------------------------------------------------------------
include "get_config.php";
include "get_token.php";

// ------------------------------------------------------------------
fputs (STDERR,"*** 開始 ***\n");

Requests::register_autoloader();

$config = get_config_proc();
$access_token = get_token_proc($config);

// print($access_token . "\n");

$sentence = "特急はくたか";

$headers = array();
$headers['Content-Type'] = 'application/json';
$headers['Authorization'] = 'Bearer ' . $access_token;

$data = array();
$data['sentence'] = $sentence;
$data['type'] = 'default';

$str_json = json_encode ($data);

$url_target = $config['url_base'] . 'v1/parse';
$request = Requests::post($url_target, $headers, $str_json);

var_dump($request->status_code);

$json_string = $request->body;
$dict_aa = json_decode ($json_string,true);

foreach ($dict_aa['result'] as $key => $unit_aa)
    {
    foreach ($unit_aa['tokens'] as $key => $token)
        {
        $str_out = $token['form'] . "\t" . $token['pos'];
        print($str_out . "\n");
        }
    }

fputs (STDERR,"*** 終了 ***\n");

// ------------------------------------------------------------------
?>
get_config.php
<?php
// ------------------------------------------------------------------
//  get_config.php
//
//                      Feb/26/2020
//
// ------------------------------------------------------------------
function get_config_proc()
{
    $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
    $dotenv->load();

    $config = array ();

    $config['grantType'] = 'client_credentials';
    $config['clientId'] = getenv("CLIENT_ID");
    $config['clientSecret'] = getenv("CLIENT_SECRET");
    $config['url_base'] = getenv("DEVELOPER_API_BASE_URL");
    $config['url_publish'] = getenv("ACCESS_TOKEN_PUBLISH_URL");

    return $config;
}

// ------------------------------------------------------------------
?>
get_token.php
<?php
// ------------------------------------------------------------------
//  get_token.php
//
//                      Feb/26/2020
//
// ------------------------------------------------------------------
function get_token_proc($config)
{
    $str_json = json_encode ($config);
    $headers = array('Content-Type' => 'application/json');

    $request = Requests::post($config['url_publish'], $headers, $str_json);

//  var_dump($request->status_code);

    $json_string = $request->body;
    $dict_aa = json_decode ($json_string,true);

    $access_token = $dict_aa["access_token"];

    return $access_token;
}

// ------------------------------------------------------------------
?>

実行結果

$ ./parsing.php 
*** 開始 ***
int(200)
特急  名詞
は 動詞語幹
く 動詞接尾辞
たか  名詞
*** 終了 ***