/** * mecab classifier * @param $document * @return array|void */ public static function toWords($document) { if (!extension_loaded('mecab')) { Log::error('mecab extension not loaded'); return; } $mecab = new \MeCab\Tagger(); $nodes = $mecab->parseToNode($document); $words = []; foreach ($nodes as $n) { $word = $n->getSurface(); if (static::isInvalidWord($word)) { continue; } if (strpos($n->getFeature(), '名詞') !== false) { $words[] = $word; } if (strpos($n->getFeature(), '動詞') !== false) { $words[] = $word; } if (strpos($n->getFeature(), '助動詞') !== false) { $words[] = $word; } if (strpos($n->getFeature(), '形容詞') !== false) { $words[] = $word; } } return $words; }
<?php /** * php-mecab/examples * parse string, wakati output format (with SPL and autocast) * charset=utf-8 */ require_once dirname(__FILE__) . '/common.inc.php'; $mecab = new MeCab\Tagger($arg_format); border(); foreach ($mecab->parseToNode($str_long) as $node) { echo $mecab->formatNode($node); } border(); foreach ($mecab->parseToNode($str_long) as $node) { echo $node; } border();
<?php /** * php-mecab/examples * parse string * charset=utf-8 */ require_once dirname(__FILE__) . '/common.inc.php'; $mecab = new MeCab\Tagger(); border(); echo $mecab->parse($str_long); border();
/** * php-mecab/examples * test like official bindings examples * charset=utf-8 */ require_once dirname(__FILE__) . '/common.inc.php'; $sentence = '太郎はこの本を二郎を見た女性に渡した。'; if (isset($_SERVER['argv'])) { $options = $_SERVER['argv']; array_shift($options); } else { $options = array(); } writeln(MeCab\VERSION); $t = new MeCab\Tagger($options); writeln($t->parse($sentence)); foreach ($t->parseToNode($sentence) as $m) { writeln($m->surface . "\t" . $m->feature); } writeln('EOS'); $di = $t->dictionaryInfo(); foreach ($di as $d) { writefln('filename: %s', $d['filename']); writefln('charset: %s', $d['charset']); writefln('size: %d', $d['size']); writefln('type: %d', $d['type']); writefln('lsize: %d', $d['lsize']); writefln('rsize: %d', $d['rsize']); writefln('version: %d', $d['version']); }
<?php /** * php-mecab/examples * parse string, wakati output format * charset=utf-8 */ require_once dirname(__FILE__) . '/common.inc.php'; $mecab = new MeCab\Tagger($arg_all_morphs); border(); echo $mecab->parse($str); border();
<?php /** * php-mecab/examples * parse string, wakati output format * charset=utf-8 */ require_once dirname(__FILE__) . '/common.inc.php'; $mecab = new MeCab\Tagger($arg_format); border(); $node = $mecab->parseToNode($str_long); while ($node) { echo $mecab->formatNode($node); $node = $node->getNext(); } border(); $node = $mecab->parseToNode($str_long); while ($node) { echo $node->toString(); $node = $node->getNext(); } border();
<?php /** * php-mecab/examples * parse N-Best * charset=utf-8 */ require_once dirname(__FILE__) . '/common.inc.php'; $mecab = new MeCab\Tagger($arg); if ($mecab->parseNBestInit($str_long)) { border(); for ($i = 0; $i < NBEST_MAX_RESULT && ($next = $mecab->next()); $i++) { echo $next; border(); } }
<?php /** * php-mecab/examples * show dictionary information * charset=utf-8 */ require_once dirname(__FILE__) . '/common.inc.php'; $mecab = new MeCab\Tagger($arg); border(); writefln('MeCab Version: %s', MeCab\VERSION); border(); writeln('Dictionary Information:'); print_r($mecab->dictionaryInfo()); border();
<?php /** * php-mecab/examples * dump all nodes (with SPL and Overloading) * charset=utf-8 */ require_once dirname(__FILE__) . '/common.inc.php'; $mecab = new MeCab\Tagger($arg); function call_format($node) { return format($node->surface, $node->feature, $node->id, $node->stat); } if ($iter = $mecab->parseToNode($str)) { border(); foreach ($iter as $node) { $ar = array('node' => $node->toArray(), 'prev' => '-', 'next' => '-', 'enext' => '-', 'bnext' => '-', 'rpath' => '-', 'lpath' => '-'); if ($node->prev) { $ar['prev'] = call_format($node->prev); } if ($node->next) { $ar['next'] = call_format($node->next); } if ($node->enext) { $ar['enext'] = call_format($node->enext); } if ($node->bnext) { $ar['bnext'] = call_format($node->bnext); } if ($rpath = $node->rpath) { $ar['rpath'] = array('prob' => $rpath->prob, 'cost' => $rpath->cost, 'rnode' => '-', 'lnode' => '-');
<?php /** * php-mecab/examples * dump all nodes * charset=utf-8 */ require_once dirname(__FILE__) . '/common.inc.php'; $mecab = new MeCab\Tagger($arg); function call_format($node) { return format($node->getSurface(), $node->getFeature(), $node->getId(), $node->getStat()); } if ($node = $mecab->parseToNode($str)) { border(); while ($node) { $ar = array('node' => $node->toArray(), 'prev' => '-', 'next' => '-', 'enext' => '-', 'bnext' => '-', 'rpath' => '-', 'lpath' => '-'); if ($prev = $node->getPrev()) { $ar['prev'] = call_format($prev); } if ($next = $node->getNext()) { $ar['next'] = call_format($next); } if ($enext = $node->getENext()) { $ar['enext'] = call_format($enext); } if ($bnext = $node->getBNext()) { $ar['bnext'] = call_format($bnext); } if ($rpath = $node->getRPath()) { $ar['rpath'] = array('prob' => $rpath->getProb(), 'cost' => $rpath->getCost(), 'rnode' => '-', 'lnode' => '-');