Skip to content

Commit

Permalink
Use namespace for PHP 7
Browse files Browse the repository at this point in the history
  • Loading branch information
rsky committed Dec 2, 2015
1 parent 908eb0e commit 52ae513
Show file tree
Hide file tree
Showing 13 changed files with 400 additions and 73 deletions.
16 changes: 16 additions & 0 deletions mecab/examples/allmorphs-ns.php
@@ -0,0 +1,16 @@
<?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();
41 changes: 29 additions & 12 deletions mecab/examples/common.inc.php
Expand Up @@ -89,19 +89,36 @@ function writefln($fmt)
echo PHP_EOL;
}

function format($surface, $feature, $id, $stat)
{
switch ($stat) {
case MECAB_BOS_NODE:
$str = 'BOS';
break;
case MECAB_EOS_NODE:
$str = 'EOS';
break;
default:
$str = $surface;
if (defined('MECAB_BOS_NODE')) {
function format($surface, $feature, $id, $stat)
{
switch ($stat) {
case MECAB_BOS_NODE:
$str = 'BOS';
break;
case MECAB_EOS_NODE:
$str = 'EOS';
break;
default:
$str = $surface;
}
return sprintf('%d: %s [%s]', $id, $str, $feature);
}
} else {
function format($surface, $feature, $id, $stat)
{
switch ($stat) {
case MeCab\BOS_NODE:
$str = 'BOS';
break;
case MeCab\EOS_NODE:
$str = 'EOS';
break;
default:
$str = $surface;
}
return sprintf('%d: %s [%s]', $id, $str, $feature);
}
return sprintf('%d: %s [%s]', $id, $str, $feature);
}

function catcher($errno, $errstr, $errfile, $errline, $errcontext)
Expand Down
21 changes: 21 additions & 0 deletions mecab/examples/dicinfo-ns.php
@@ -0,0 +1,21 @@
<?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();
73 changes: 73 additions & 0 deletions mecab/examples/dumpall-ns.php
@@ -0,0 +1,73 @@
<?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' => '-',
);
if ($rpath_rnode = $rpath->getRNode()) {
$ar['rpath']['rnode'] = call_format($rpath_rnode);
}
if ($rpath_lnode = $rpath->getLNode()) {
$ar['rpath']['lnode'] = call_format($rpath_lnode);
}
}
if ($lpath = $node->getLPath()) {
$ar['lpath'] = array(
'prob' => $lpath->getProb(),
'cost' => $lpath->getCost(),
'rnode' => '-',
'lnode' => '-',
);
if ($lpath_rnode = $lpath->getRNode()) {
$ar['lpath']['rnode'] = call_format($lpath_rnode);
}
if ($lpath_lnode = $lpath->getLNode()) {
$ar['lpath']['lnode'] = call_format($lpath_lnode);
}
}
print_r($ar);
border();
$node = $node->getNext();
}
}
72 changes: 72 additions & 0 deletions mecab/examples/dumpall-ns2.php
@@ -0,0 +1,72 @@
<?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' => '-',
);
if ($rpath->rnode) {
$ar['rpath']['rnode'] = call_format($rpath->rnode);
}
if ($rpath->lnode) {
$ar['rpath']['lnode'] = call_format($rpath->lnode);
}
}
if ($lpath = $node->lpath) {
$ar['lpath'] = array(
'prob' => $lpath->prob,
'cost' => $lpath->cost,
'rnode' => '-',
'lnode' => '-',
);
if ($lpath->rnode) {
$ar['lpath']['rnode'] = call_format($lpath->rnode);
}
if ($lpath->lnode) {
$ar['lpath']['lnode'] = call_format($lpath->lnode);
}
}
print_r($ar);
border();
}
}
28 changes: 28 additions & 0 deletions mecab/examples/format-ns.php
@@ -0,0 +1,28 @@
<?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();
24 changes: 24 additions & 0 deletions mecab/examples/format-ns2.php
@@ -0,0 +1,24 @@
<?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();
18 changes: 18 additions & 0 deletions mecab/examples/nbest-ns.php
@@ -0,0 +1,18 @@
<?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();
}
}
16 changes: 16 additions & 0 deletions mecab/examples/parse-ns.php
@@ -0,0 +1,16 @@
<?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();
10 changes: 10 additions & 0 deletions mecab/examples/split-ns.php
@@ -0,0 +1,10 @@
<?php
/**
* php-mecab/examples
* split string into an array of part-of-speech
* charset=utf-8
*/

require_once dirname(__FILE__) . '/common.inc.php';

print_r(MeCab\split($str));
39 changes: 39 additions & 0 deletions mecab/examples/test-ns.php
@@ -0,0 +1,39 @@
<?php
/**
* 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']);
}
20 changes: 20 additions & 0 deletions mecab/examples/wakati-ns.php
@@ -0,0 +1,20 @@
<?php
/**
* php-mecab/examples
* parse string, wakati output format
* charset=utf-8
*/

require_once dirname(__FILE__) . '/common.inc.php';

$mecab = new MeCab\Tagger($arg_wakati);

border();

echo $mecab->parse($str_long);

border();

print_r(MeCab\split($str));

border();

0 comments on commit 52ae513

Please sign in to comment.