前提・実現したいこと
配列に格納されている各要素に対してmecabを使って形態素解析を行いたいのですが、出力をひとつの配列にするのではなく、要素ごとに配列に格納したいです。
形態素解析を行っている部分がまだいまいち理解できていなく、出力結果がwhile文の中のoutputという配列に入っているのはわかるのですが、各要素ごとに配列を作りその配列に形態素解析の結果を格納する場合、どのように変更すればよいのかわかりません。
現時点では以下のように出力されています。
foo Array ( [0] => foo 名詞,固有名詞,組織,*,*,*,* [1] => EOS [2] => ) bar Array ( [0] => foo 名詞,固有名詞,組織,*,*,*,* [1] => EOS [2] => [3] => bar 名詞,固有名詞,組織,*,*,*,* [4] => EOS [5] => ) hello Array ( [0] => foo 名詞,固有名詞,組織,*,*,*,* [1] => EOS [2] => [3] => bar 名詞,固有名詞,組織,*,*,*,* [4] => EOS [5] => [6] => hello 名詞,固有名詞,組織,*,*,*,* [7] => EOS [8] => ) world Array ( [0] => foo 名詞,固有名詞,組織,*,*,*,* [1] => EOS [2] => [3] => bar 名詞,固有名詞,組織,*,*,*,* [4] => EOS [5] => [6] => hello 名詞,固有名詞,組織,*,*,*,* [7] => EOS [8] => [9] => world 名詞,固有名詞,組織,*,*,*,* [10] => EOS [11] => )
該当のソースコード
<?php $array = array("foo", "bar", "hello", "world"); $path = '"C:\Program Files\MeCab\bin\mecab.exe"'; $i = 0; $count = count($array); while($i<$count){ $input = $array[$i]; echo $input; $descriptorspec = array( 0 => array("pipe","r"), 1 => array("pipe","w") ); $process = proc_open($path, $descriptorspec, $pipes); if(is_resource($process)) { fwrite($pipes[0], $input); fclose($pipes[0]); while(!feof($pipes[1])){ $output[] = fgets($pipes[1]); } fclose($pipes[1]); proc_close($process); } echo "<pre>"; print_r($output); //形態素解析の結果の出力 echo "</pre>"; $i++; } ?>
補足情報(FW/ツールのバージョンなど)
php5.6.37 eclipseOxygen
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。