はじめまして。
現在MeCabにおいて、php_mecabを使わずproc_open()関数で、Mecab_Tagger オブジェクト(またはその機能を持ったオブジェクト)を生成したいと考えております。
どんな些細なことでも構いませんので、是非ご教授の程よろしくお願い申し上げます。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
ベストアンサー
proc_open()は子プロセスでコマンドを実行しパイプや仮想コンソールで処理をさせるので、mecab コマンドを呼び出してやれば作れそうですね。
MeCabのコマンドライン引数をproc_open()の引数に渡してパイプを読み書きすれば、Mecab_Targgerに近い処理ができるかと。
以下はこのサイトを参考に書いた疑似コードです。動作確認はしてません。
lang
1class MeCab_Tagger_dummy { 2 public $firstNode = null; 3 public function parseToNode($txt) { 4 $cmd = 'mecab '; // コマンドライン引数があったらここに追加する 5 $descriptorspec = array( 6 0 => array("pipe", "r"), // stdin は、子プロセスが読み込むパイプです。 7 1 => array("pipe", "w"), // stdout は、子プロセスが書き込むパイプです。 8 2 => array("file", "/home/mecab-output.txt", "a") // はファイルで、そこに書き込みます。 9 ); 10 $cwd = '/home'; 11 $process = proc_open($cmd, $descriptorspec, $pipes, $cwd); 12 13 if (is_resource($process)) { 14 fwrite($pipes[0], $txt); 15 fclose($pipes[0]); 16 17 while ( ($line = fgets($pipes[1])) !== false ) { 18 $node = new MeCab_Node_dummy($this->firstNode,$line); 19 if ( $this->firstNode === null ) $this->firstNode = $node; 20 } 21 fclose($pipes[1]); 22 23 proc_close($process); 24 } 25 return $this->firstNode; 26 } 27} 28class MeCab_Node_dummy { 29 public function __construct(MeCab_Node_dummy $prev,$feature) { 30 $prev->next = $this; 31 $this->feature = $feature; 32 if ( $feature ) { 33 $columns = mb_split(',',$feature); 34 $this->surface = mb_split(' ',$columns[0]); 35 } 36 } 37 protected $next; 38 protected $feature; 39 protected $surface = ''; 40 public function getNext() { return $this->next; } 41 public function getFeature() { return $this->feature; } 42 public function getSurface() { return $this->surface; } 43} 44 45$mecab = new MeCab_Tagger_dummy(); 46$txt = "現在MeCabにおいて、php_mecabを使わずproc_open()関数で、Mecab_Tagger オブジェクト(またはその機能を持ったオブジェクト)を生成したいと考えております。"; 47$meisi = array(); //名詞配列 48for ( $node=$mecab->parseToNode($txt); $node; $node=$node->getNext() ) { 49 $meisi[] = $node->getSurface(); 50} 51//名詞の重複削除 52$meisi = array_unique($meisi); 53print_r($meisi);
投稿2015/02/25 19:16
編集2015/02/26 02:30総合スコア209
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/02/26 00:20
2015/02/26 00:47
2015/03/01 08:36