LoginSignup
14
14

More than 5 years have passed since last update.

Yahooの日本語形態素解析API

Posted at
yahoo_ma_test.php
<?php
require_once 'HTTP/Request2.php';
$appid = '<your appid>';

$sentence = $argv[1];

$request = new HTTP_Request2('http://jlp.yahooapis.jp/MAService/V1/parse', HTTP_Request2::METHOD_GET);
$url = $request->getUrl();
$url->setQueryVariables(array(
  'appid' => $appid,
  'sentence' => $sentence,
  'results' => 'uniq',
  'filter' => '9'
));

try {
  $response = $request->send();
  if (200 == $response->getStatus()) {
    $xml = new SimpleXMLElement($response->getBody());
    foreach ($xml->uniq_result->word_list->word as $word) {
      printf("%s\t%d\n", $word->surface, $word->count);
    }
  } else {
    echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
      $response->getReasonPhrase();
  }
} catch (HTTP_Request2_Exception $e) {
  echo 'Error: ' . $e->getMessage();
}
?>
14
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14