PHP code example of daisukedaisuke / kanakanjiconverter

1. Go to this page and download the library: Download daisukedaisuke/kanakanjiconverter 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/ */

    

daisukedaisuke / kanakanjiconverter example snippets


composer 



declare(strict_types=1);

use kanakanjiconverter\PHPKanaKanjiConverter;

 = new PHPKanaKanjiConverter();
$a = $converter->convert($input);
echo $a["best"]["text"], "\n";



declare(strict_types=1);

use kanakanjiconverter\PHPKanaKanjiConverter;

esult = $converter->convert($input);
echo $result["best"]["text"], "\n";



declare(strict_types=1);

use kanakanjiconverter\PHPKanaKanjiConverter;
use kanakanjiconverter\UserDictionary;

= new UserDictionary();
$dict->addAll([
	// 「eremenntox」→ toHiragana → 「えれめんとx」だが x は変換されないため
	// removeIllegalFlag=true にするか、reading を「えれめんと」にして別途処理する
	['reading' => 'eremenntox', 'surface' => 'ElementX',  'mode' => UserDictionary::MODE_REPLACE,'word_cost' => -5000, 'pos' => "名詞"],
	// 「sod」→ 「そd」 → d が残るため reading は「そ」にするか入力を「sodo」等にする
		[
			'reading'   => 'anni',
			'surface'   => 'Annihilation',
			'mode'      => UserDictionary::MODE_SERVER,
			'word_cost' => 2000,
			'pos'       => '名詞',
			'subpos'    => '一般',
			'pos_label' => '名詞-一般',
			'left_id'   => 1852,
			'right_id'  => 1852,
		],
	//['reading' => 'sod',       'surface' => 'SOD SERVER',  'mode' => UserDictionary::MODE_REPLACE,'word_cost' => -5000, 'pos' => "名詞"],
]);

$converter->registerUserDict('server', $dict);


$time = microtime(true);

//English is not supported
foreach(["kannzeidekilyogisinnkokuwosaigennsurudoramagaarimasu","server", "konn", "sinnkannsenn", "converterですか", "annigasuki", "anninnsuto-ru"] as $input){
	$result = $converter->convert($input);


	//var_dump($result["kana"]);

	if(!$converter->isValid($result)){
		echo "kana: ".$result["kana"], "\n";
		continue;
	}

	echo "no-maru:". $result["best"]["text"], "\n";
	//var_dump($result["best"]["tokens"]);
}
$time = microtime(true) - $time;

echo "Time: {$time} sec\n";

echo ((memory_get_peak_usage() - $basemem) / 1024 / 1024) . " MB\n";
echo ((memory_get_usage() - $basemem1) / 1024 / 1024) . " MB\n";


no-maru:関税で虚偽申告を再現するドラマがあります
no-maru:せrヴェr
kana: こん
no-maru:新幹線
no-maru:今ヴェrてrですか
no-maru:Annihilationが好き
no-maru:アンインストール
Time: 0.10237002372742 sec
124.99425506592 MB
123.27954101562 MB


declare(strict_types=1);

use kanakanjiconverter\PHPKanaKanjiConverter;
use kanakanjiconverter\UserDictionary;

aKanjiConverter();
$input = "gaitametoennsouba";
$result = $converter->convert($input);
//var_dump($result["kana"]);

if(!$converter->isValid($result)){
	echo "kana: ".$result["kana"], "\n";
	return;
}

echo "no-maru:". $result["best"]["text"], "\n";

foreach($result["best"]["tokens"] as $item){
	var_dump($item);
}

$time = microtime(true) - $time;

echo "Time: {$time} sec\n";

echo ((memory_get_peak_usage() - $basemem) / 1024 / 1024) . " MB\n";
echo ((memory_get_usage() - $basemem1) / 1024 / 1024) . " MB\n";



declare(strict_types=1);

use kanakanjiconverter\PHPKanaKanjiConverter;

_peak_usage();
$converter = new PHPKanaKanjiConverter();
$input = "konoraiburarihamainnkurahutonotamenitukuraremasita";
$result = $converter->convert($input, false, 5);//Number of pieces
//var_dump($result["kana"]);

if(!$converter->isValid($result)){
	echo "kana: " . $result["kana"], "\n";
	return;
}

echo "no-maru:" . $result["best"]["text"], "\n";

foreach($result["candidates"] as $item){
	var_dump($item["text"]);
}

$time = microtime(true) - $time;

echo "Time: {$time} sec\n";

echo ((memory_get_peak_usage() - $basemem) / 1024 / 1024) . " MB\n";
echo ((memory_get_usage() - $basemem1) / 1024 / 1024) . " MB\n";