COTOHA でキーワードの抽出 (PHP)


COTOHA API Portal の使用例です。

key_word.php
#! /usr/bin/php
<?php
// ------------------------------------------------------------------
//  key_word.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();

$file_in = $argv[1];
echo    $file_in . "\n";

$doc = file_get_contents ($file_in);

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


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

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

$str_json = json_encode ($data);

$url_target = $config['url_base'] . 'v1/keyword';
$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)
    {
    $str_out = $unit_aa['form'] . "\t" . $unit_aa['score'];
    print($str_out . "\n");
    }

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

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

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

実行結果

$ ./key_word.php akai_rousoku.txt
*** 開始 ***
akai_rousoku.txt
int(200)
猿 135.52966
蝋燭  83.9601
花火  78.08584
亀 43.078
火 42.81965
*** 終了 ***