少し前に、これを行うための小さな perl モジュールを作成しました。アラビア語<=>日本語を変換できます。徹底的にテストしていませんが、かなり包括的だと思います。自由に改善してください。
package kanjiArabic;
use strict;
use warnings;
our $VERSION = "1.00";
use utf8;
our %big = (
十 => 10,百 => 100,千 => 1000,
);
our %bigger = (
万 => 10000,億 => 100000000,
兆 => 1000000000000,京 => 10000000000000000,
垓 => 100000000000000000000,
);
#precompile regexes
our $qr = qr/[0-9]/;
our $bigqr = qr/[十百千]/;
our $biggerqr = qr/[万億兆京垓]/;
#this routine does most of the real work.
sub kanji2arabic{
$_ = shift;
tr/〇一二三四五六七八九/0123456789/;
#optionally precompile for performance boost
s/(?<=${qr})(${bigqr})/\*${1}/g;
s/(?<=${bigqr})(${bigqr})/\+${1}/g;
s/(${bigqr})(?=${qr})/${1}\+/g;
s/(${bigqr})(?=${bigqr})/${1}\+/g;
s/(${bigqr})/${big{$1}}/g;
s/([0-9\+\*]+)/\(${1}\)/g;
s/(? "〇", 1 => "一", 2 => "二", 3 => "三", 4 => "四",
5 => "五", 6 => "六", 7 => "七", 8 => "八", 9 => "九",
);
our %places = (
1 => 10,
2 => 100,
3 => 1000,
4 => 10000,
8 => 100000000,
12 => 1000000000000,
16 => 10000000000000000,
20 => 100000000000000000000,
);
our %abig = (
10 => "十",
100 => "百",
1000 => "千",
10000 => "万",
100000000 => "億",
1000000000000 => "兆",
10000000000000000 => "京",
100000000000000000000 => "垓",
);
our $MAX = 24; #We only support numbers up to 24 digits!
sub arabic2kanji{
my @number = reverse(split(//,$_[0]));
my @kanji;
for(my $i=$#number;$i>=0;$i--){
if( $i==0 ){push(@kanji,$asmall{$number[$i]});}
elsif( $i % 4 == 0 ){
if( $number[$i] !~ m/[01]/ ){
push(@kanji,$asmall{$number[$i]});
}
push(@kanji,$abig{$places{$i}});
}else{
my $p = $i % 4;
if( $number[$i]==0 ){
next;
}elsif( $number[$i]==1 ){
push(@kanji,$abig{$places{$p}});
}else{
push(@kanji,$asmall{$number[$i]});
push(@kanji,$abig{$places{$p}});
}
}
}
return join("",@kanji);
}
sub eval_k2a{
#feed me utf-8!
if($_[0] !~ m/^[〇一二三四五六七八九十百千万億兆京垓]+$/){
print "Error: ".$_[0].
" not a Kanji number.\n" if defined($_[1])&&$_[1]==1;
return -1;
}
my $expression = kanji2arabic($_[0]);
print $expression."\n" if defined($_[1])&&$_[1]==1;
return eval($expression);
}
1;
次に、別のスクリプトから次のように呼び出します。
#!/usr/bin/perl -w
use strict;
use warnings;
use Encode;
use kanjiArabic;
my $kanji = kanjiArabic::arabic2kanji($ARGV[0]);
print "Kanji: ".encode("utf8",$kanji)."\n";
my $arabic = kanjiArabic::eval_k2a($kanji);
print "Back to arabic...\n";
print "Arabic: ".$arabic."\n";
このスクリプトを次のように使用します。
kettle:~/k2a$ ./k2a.pl 5000215
Kanji: 五百万二百十五
Back to arabic...
Arabic: 5000215
ロックオン。