KAKASI を 使って漢字かなまじり文を平仮名やローマ時に変換するプログラムの続きである。 今回は、テキストだけでなく、ファイルやURLを指定して分解できるように機能を拡張した。
0001: <?php
0002: /** parsewords2.php
0003: * 単語に分解する
0004: *
0005: * @copyright (c)studio pahoo
0006: * @author パパぱふぅ
0007: * @version 1.1 2007/09/22 1行取り出し処理を修正した
0008: * @version 2.0 2006/02/05
0009: */
0010: error_reporting(NULL); //システムのエラー出力を止める
0011: mb_internal_encoding('SJIS');
0012: $myself = basename($_SERVER['SCRIPT_NAME']);
0013:
0014: /**
0015: * kakasiの実行パス
0016: * @global string $Kakasi
0017: */
0018: $Kakasi = '/usr/bin/kakasi'; //Linuxの場合(例)
0019: //$Kakasi = 'C:\\kakasi\\bin\\kakasi.exe'; //Windowsの場合(例)
0020:
0021: /**
0022: * エラーメッセージ時出力後、プログラム終了
0023: * @param string $errmsg エラーメッセージ
0024: * @param string $errcode エラーコード(exitに渡す)
0025: */
0026: function error_exit($errmsg, $errcode) {
0027: echo "エラー: " . $errmsg;
0028: exit($errcode);
0029: }
0030:
0031: /**
0032: * kakasiを使って単語に分解する
0033: * @param string $kakasi kakasiの実行パス
0034: * @param string $str 分解するコンテンツ
0035: * @param string $array 分解結果を格納する配列
0036: * @return なし
0037: */
0038: function parsing($kakasi, $str, &$array) {
0039: //形態素解析をしたい文章を渡しつつ、kakasiへのハンドルオープン
0040: $handle = popen("echo '$str' | $kakasi -w ", "r");
0041:
0042: //結果を1行ずつ取得
0043: while ($get_kakasi = fgets($handle)) {
0044: //kakasiの結果を分解
0045: $result = split("[\t\r\n' ]", $get_kakasi);
0046: //結果を配列に格納する
0047: foreach ($result as $key=>$val) {
0048: if ($val != "") {
0049: if (isset($array[$val])) $array[$val]++;
0050: else $array[$val] = 1;
0051: }
0052: }
0053: }
0054: pclose($handle);
0055: }
0056:
0057: // 表示処理 =================================================================
0058: echo <<< EOF
0059: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
0060: "http://www.w3.org/TR/html4/loose.dtd">
0061: <html lang="ja">
0062: <head>
0063: <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" />
0064: <title>単語に分解・その2</title>
0065: </head>
0066: <body>
0067:
0068: EOF;
0069: // メニュー ================================================================
0070: if (isset($_POST["menu"]) == FALSE && isset($_POST["exec"]) == FALSE) {
0071: echo <<< EOF
0072: <h1>■単語に分解・その2 − メニュー</h1>
0073: <form method="post" action="$myself">
0074: <input type="radio" name="menu" value="1" /> テキスト<br />
0075: <input type="radio" name="menu" value="2" checked /> ファイル<br />
0076: <input type="radio" name="menu" value="3" /> URL<br />
0077: <input type="submit" name="submit" value="次へ" />
0078: <input type="submit" name="init" value="メニューに戻る" />
0079: </form>
0080:
0081: EOF;
0082: // データ入力(1)テキスト入力 ===========================================
0083: } else if ($_POST["menu"] == "1") {
0084: echo <<< EOF
0085: <h1>■単語に分解 − テキスト入力</h1>
0086: <form method="post" action="$myself">
0087: 分解する文章<br />
0088: <textarea name="text" rows="10" cols="80"></textarea><br />
0089: <input type="submit" name="exec" value="分解実行" />
0090: <input type="submit" name="init" value="メニューに戻る" />
0091: </form>
0092:
0093: EOF;
0094: // データ入力(2)ファイル名入力 =========================================
0095: } else if ($_POST["menu"] == "2") {
0096: echo <<< EOF
0097: <h1>■単語に分解 − ファイル名入力</h1>
0098: <form enctype="multipart/form-data" method="post" action="$myself">
0099: ファイル:
0100: <input name="upload" type="file" size="100" />
0101: <input type="submit" name="exec" value="分解実行" />
0102: <input type="submit" name="init" value="メニューに戻る" />
0103: </form>
0104:
0105: EOF;
0106: // データ入力(3)URL入力 ===============================================
0107: } else if ($_POST["menu"] == "3") {
0108: echo <<< EOF
0109: <h1>■単語に分解 − URL入力</h1>
0110: <form method="post" action="$myself">
0111: URL:
0112: <input name="url" type="text" size="100" />
0113: <input type="submit" name="exec" value="分解実行" />
0114: <input type="submit" name="init" value="メニューに戻る" />
0115: </form>
0116:
0117: EOF;
0118: // データ処理と出力 ======================================================
0119: } else if (isset($_POST["exec"])) {
0120: $keywords = array(); //配列を用意しておく
0121:
0122: //分解するコンテンツ
0123: if (isset($_FILES["upload"]["tmp_name"])) {
0124: $contents = file_get_contents($_FILES["upload"]["tmp_name"]); //ファイルを一気に読み込む
0125: $contents = mb_convert_encoding($contents, "SJIS", "auto");
0126: $contents = strip_tags($contents); //タグを消去しておく
0127: if ($contents == FALSE) {
0128: error_exit("ファイル " . $_FILES["upload"]["tmp_name"] . " は存在しません", 1);
0129: }
0130: } else if (isset($_POST["text"])) {
0131: $contents = $_POST["text"];
0132: if ($contents == "") {
0133: error_exit("入力コンテンツがありません", 1);
0134: }
0135: } else if (isset($_POST["url"])) {
0136: $contents = file_get_contents($_POST["url"]); //URLを一気に読み込む
0137: $contents = strip_tags($contents); //タグを消去しておく
0138: if ($contents == FALSE) {
0139: error_exit("URL " . $_POST["url"] . " は存在しません", 1);
0140: }
0141: } else {
0142: error_exit("コンテンツが存在しません", 1);
0143: }
0144:
0145: //1行ずつ取り出して単語に分解する
0146: $str = strtok($contents, "\n");
0147: while ($str != FALSE) {
0148: parsing($Kakasi, $str, $keywords);
0149: $str = strtok("\n");
0150: }
0151:
0152: arsort($keywords); //出現回数の多い順に並び替え
0153:
0154: echo <<< EOF
0155: <h1>■分解した単語</h1>
0156: <table border="1">
0157: <tr><th>出現回数</th><th>単語</th></tr>
0158:
0159: EOF;
0160: foreach ($keywords as $key=>$val) {
0161: printf("<tr><td align=\"right\">%d</td><td>%s</td></tr>\n", $val, $key);
0162: }
0163: echo <<< EOF
0164: </table>
0165: <form method="post" action="$myself">
0166: <input type="submit" name="init" value="メニューに戻る" />
0167: </form>
0168:
0169: EOF;
0170: // 異常終了 =============================================================
0171: } else {
0172: error_exit("プログラムに渡す値が異常です", 1);
0173: }
0174:
0175: echo <<< EOF
0176: </body>
0177: </html>
0178:
0179: EOF;
0180: ?>
| 2007年09月22日更新 | ||
| <<前へ | <目次> | 次へ>> |
| 戻る | 【関連ページ】 | |