Created
October 5, 2014 11:42
-
-
Save h-collector/a57b0930bceddc86d931 to your computer and use it in GitHub Desktop.
Kanji to romaji PHP + Kakasi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
$descriptorspec = [ | |
0 => ['pipe', 'r'], // stdin | |
1 => ['file', 'php://stdout', 'w'], // stdout | |
2 => ['file', 'php://stderr', 'a'], // stderr | |
]; | |
$kakasi = 'kakasi -i euc -w | kakasi -i euc -Ha -Ka -Ja -Ea -ka'; | |
$kanjiToRomaji = function($kanji) use ($descriptorspec, $kakasi){ | |
$process = proc_open($kakasi, $descriptorspec, $pipes); | |
if (is_resource($process)) { | |
fwrite($pipes[0], iconv('utf-8', 'eucjp', $kanji)); | |
fclose($pipes[0]); | |
proc_close($process); | |
} | |
}; | |
if ($args = array_slice($argv, 1)){ | |
foreach($args as $line) $kanjiToRomaji($line . "\n"); | |
} else { | |
while($line = fgets(STDIN)) $kanjiToRomaji($line); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment