Skip to content

Instantly share code, notes, and snippets.

@h-collector
Created October 5, 2014 11:42
Show Gist options
  • Save h-collector/a57b0930bceddc86d931 to your computer and use it in GitHub Desktop.
Save h-collector/a57b0930bceddc86d931 to your computer and use it in GitHub Desktop.
Kanji to romaji PHP + Kakasi
#!/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