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);
$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分"
}
]
}
}
"id": "1234-1",
"jsonrpc": "2.0",
"result": {
"phrases": [
{
"score": 100,
"text": "東京ミッドタウン"
},
{
"score": 73,
"text": "国立新美術館"
},
{
"score": 37,
"text": "5分"
}
]
}
}
コメント