PHPでキーフレーズ抽出を使う方法

Pocket

Yahoo!はテキスト解析のAPIを提供しています。その中にキーフレーズ抽出があります。Pythonのサンプルコードが載っています。


これをPHPでやってみました。

$query = "東京ミッドタウンから国立新美術館まで歩いて5分で着きます。";
$appid = "<あなたのアプリケーションID>";
$url = "https://jlp.yahooapis.jp/KeyphraseService/V2/extract";
$params = array(
  "id" => "1234-1",
  "jsonrpc" => "2.0",
  "method" => "jlp.keyphraseservice.extract",
  "params" => array("q"=>$query)
);
$params = json_encode($params);
$header = array(
  "Content-Length: ".strlen($params),
  "Accept: application/json",
  "Content-Type: application/json",
  "User-Agent: Yahoo AppID: ".$appid
);
$context = array(
  "http" => array(
    "method" => "POST",
    "header" => implode("\r\n", $header),
    "content" => $params
  )
);
$json_response = file_get_contents($url, false, stream_context_create($context));

print_r($json_response);

実行結果

{
    "id": "1234-1",
    "jsonrpc": "2.0",
    "result": {
        "phrases": [
            {
                "score": 100,
                "text": "東京ミッドタウン"
            },
            {
                "score": 73,
                "text": "国立新美術館"
            },
            {
                "score": 37,
                "text": "5分"
            }
        ]
    }
}

[ 2022年4月29日 | カテゴリー: PHP | タグ: , , ]

« | »

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

送信してください。


タグ

カテゴリー

最近の投稿

最近のコメント

固定ページ

アーカイブ

stabucky

写真

メタ情報