Page2にてMecab Extentionなるものも存在するんですが、こちらなどを参考にしてmorph_analysis関数を作ってみました。
やってる内容は至極簡単。
MeCabプロセスに対して文字列を投げて返ってきた値をパースしているだけ。
define('Mecab_Encoding',
'SJIS');
define('Mecab_ResultEncoding',
'UTF-8');
define('MeCab_Path',
'mecab.exe');
function morph_analysis($text) {
$descriptorspec =
array (
0 =>
array ("pipe",
"r"),
// stdin
1 =>
array ("pipe",
"w") // stdout
);
$process =
proc_open(MeCab_Path,
$descriptorspec,
$pipes);
// mecabに文章を与える
// 結果を読み取る
while (!
feof($pipes[1])) {
$result .=
fread($pipes[1],
4096);
}
foreach($lines as $line) {
$word = $s[0];
//基本形などを利用したい場合はここに追加
'word' => $word,
'class' => $words[0],
'detail1' => $words[1],
'detail2' => $words[2],
'detail3' => $words[3],
'conjugation1' => $words[4],
'conjugation2' => $words[5]
);
}
return $res;
} else {
return false;
}
}
MeCabを使ってブログの形態素解析をしているんですが、やはり精度はかなり悪いですね。ブログには顔文字もあるし、誤字・脱字も多い&口語表現・・・・なにかいい方法はないか模索中。。