COTOHA で固有名詞の抽出 (PHP)


COTOHA API Portal の使用例です。

proper_noun.php
#! /usr/bin/php
<?php
// ------------------------------------------------------------------
//  proper_noun.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/ne';
$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)
    {
    print($unit_aa['form'] . "\n");
    }

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

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

get_config.php get_token.php はこちら
COTOHA API で構文解析 (PHP)

実行結果

$ ./proper_noun.php
*** 開始 ***
int(200)
富山
金沢
兼六園
*** 終了 ***