テキスト解析サンプルコード
自然言語理解(php)
使用API | |
---|---|
構成環境 | Apache + PHP-7.0 ・以下のライブラリが動作する環境が必要となります。 - cURL |
このサンプルは自然言語理解APIを使用して、入力された文章を解析し、情報の抽出を行うものです。テキスト入力エリアに文章を入力します。[解析]ボタンを押下すると文章の解析を行い、結果を表示します。また、[リクエスト履歴を引き継ぐ]にチェックを入れると、過去のリクエスト履歴を参照し解析結果が変わります。
ダウンロード
サンプルご利用の際は、利用規約をご覧ください。利用規約は、ダウンロードパッケージ内のLICENSE.txtファイルに記載されています。

コード解説
ダウンロードしたアーカイブファイルを展開すると下記のようになります。
`-- sample4 |-- LICENSE.txt |-- README.txt `-- nlu_sample.php
nlu_sample.php
<?php /** * Yahoo! JAPAN Web APIのご利用には、アプリケーションIDの登録が必要です。 * あなたが登録したアプリケーションIDを $appid に設定してお使いください。 * アプリケーションIDの登録URLは、こちらです↓ * http://e.developer.yahoo.co.jp/webservices/register_application */ $appid = ''; // <-- ここにあなたのアプリケーションIDを設定してください。 $url = "https://jlp.yahooapis.jp/NLUService/V1/analyze"; $context = array(); function request($requests) { global $appid; global $url; $postdata = json_encode($requests); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'User-Agent: Yahoo AppID: '.$appid )); $response = curl_exec($ch); curl_close($ch); return $response; } function analyze($requests, $next_context = array()) { // 過去の文脈情報の引き継ぎ処理 foreach ($next_context as $k => $v) { $requests[$k] = $v; } $result_str = request($requests); return json_decode($result_str, true)['result']; } function prepareNextContext($prev_result, $context) { $next_context = array(); // 文脈引き継ぎ用の変数を作成 // 過去3回分までの文脈を引き継いでください foreach ($context as $prev_key => $prev_value) { if (preg_match("/prev3$/", $prev_key)) { continue; } if (preg_match("/prev2$/", $prev_key)) { $key = substr($prev_key, 0, -1) . "3"; $next_context[$key] = $prev_value; continue; } if (preg_match("/prev1$/", $prev_key)) { $key = substr($prev_key, 0, -1) . "2"; $next_context[$key] = $prev_value; continue; } $key = $prev_key . "_prev1"; $next_context[$key] = $prev_value; } foreach ($prev_result as $prev_key => $prev_value) { $key = $prev_key . "_prev1"; $next_context[$key] = $prev_value; } return $next_context; } function escapestring($str) { return htmlspecialchars($str, ENT_QUOTES); } if (isset($_POST['sentence'])) { $sentence = mb_convert_encoding($_POST['sentence'], 'utf-8', 'auto'); } else { $sentence = ""; } ?> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=UTF-8"> <title>テキスト解析デモ - 自然言語理解</title> </head> <body> <style type="text/css"> <!-- h1 { text-align: center; float: left; } h2 { text-align: justify; margin: 0 0 0 0; padding: 0; } div { clear:both; } .apiEx { padding-top: 10px; *padding-top: 0px; } p { text-align: justify; padding: 0; margin: 0 0 0 0; float:left; } form { text-align: justify } --> </style> <div> <h1>テキスト解析デモ - 自然言語理解</h1> </div> <div> <h2>入力文</h2> <form name="myForm" method="post" action="./nlu_sample.php"> <textarea name="sentence" cols=70 rows=8><?php echo escapestring($sentence); ?></textarea> <br/> <input type="checkbox" name="context" value="context" <?php if (isset($_POST['context'])) { echo "checked"; } ?>>リクエスト履歴を引き継ぐ</input> <br/> <input type="submit" name="exec" value="解析"></input> </form> </div> <div id="result"> <h2>解析結果</h2> <table width="622px" cellpadding="5" cellspacing="1"style="float:left" border=solid> <tbody> <?php session_start(); if ($sentence != "") { echo "<tr>"; echo "<td>キー</td>"; echo "<td>値</td>"; echo "</tr>"; $requests = array( "intext" => $sentence ); // リクエスト履歴の引き継ぎ処理 if (isset($_POST['context']) and isset($_SESSION['context'])) { $context = $_SESSION['context']; } $result = analyze($requests, $context); // レスポンスの出力 ksort($result); foreach ($result as $key => $value) { echo "<tr>"; echo "<td>" . $key . "</td>"; echo "<td>" . $value . "</td>"; echo "</tr>"; } // "リクエスト履歴を引き継ぐ" がチェックされている場合は、リクエスト履歴を_SESSIONに保持し次の解析に利用します if (isset($_POST['context'])) { $_SESSION['context'] = prepareNextContext($result, $context); } else { unset($_SESSION['context']); } } ?> </tbody> </table> </div> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="center"> <!-- Begin Yahoo! JAPAN Web Services Attribution Snippet --> <a href="http://developer.yahoo.co.jp/about"> <img src="http://i.yimg.jp/images/yjdn/yjdn_attbtn2_105_17.gif" width="105" height="17" title="Webサービス by Yahoo! JAPAN" alt="Webサービス by Yahoo! JAPAN" border="0" style="margin:15px 15px 15px 15px"></a> <!-- End Yahoo! JAPAN Web Services Attribution Snippet --> </td> </tr> </table> </body> </html>