PHP code example of karakani / mecha-mocha

1. Go to this page and download the library: Download karakani/mecha-mocha library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

karakani / mecha-mocha example snippets


$tagger = Tagger::create();
$nodeGroups = $tagger->parse("すもももももももものうち");

foreach ($nodeGroups as $nodeGroup) {
    foreach ($nodeGroup as $node) {
        printf("%s(%s)\n", $node->surface, $node->feature->pos);
    }
}

// 次のように出力されます:
//
// すもも(名詞)
// も(助詞)
// もも(名詞)
// も(助詞)
// もも(名詞)
// の(助詞)
// うち(名詞)

$command = (new CommandBuilder())
    ->setBinPath('/usr/local/bin/mecab')
    ->setUserDic('/usr/local/lib/mecab/dic/mecab-ipadic-neologd')
    ->build();
$runner = CommandRunner::create($command);

Tagger::setDefaultRunner($runner);

$tagger = Tagger::create();

$runnerWithDefaultOption = CommandRunner::create();
$taggerA = Tagger::create($runnerWithDefaultOption);

$runnerWithCustomOption = CommandRunner::create(
    (new CommandBuilder())
        ->setBinPath('/usr/local/bin/mecab')
        ->setUserDic('/usr/local/lib/mecab/dic/mecab-ipadic-neologd')
        ->build()
);
$taggerB = Tagger::create($runnerWithCustomOption);

$runner = CommandRunner::create([
    '/usr/local/bin/mecab',
    '--dicdir=/usr/local/lib/mecab/dic/mecab-ipadic-neologd',
]);

Tagger::setDefaultRunner($runner);

Tagger::getDefaultRunner()->close();