#============================================================
# MyCharConvEUCJP
#============================================================
package MyCharConvEUCJP;
use MyCharConv;
@ISA = qw(MyCharConv);
use strict;
use Encode;
use Jcode;
#use Text::Kakasi;
#===============================================
# 旦弁伉皿玄釐啗庍醒
#===============================================
my $ScriptCodeCharCode = Jcode::getcode("畸紹踹枆嗯蘿ゥ﹛");
my $UseKakasiModule = 0;
my $KakasiPath = "/usr/local/bin/kakasi";
my $KakasiCharCode = "euc";
if($^O =~ /^MSWin/) {
$KakasiPath = "c:\\kakasi\\bin\\kakasi.exe";
$KakasiCharCode = "sjis";
}
my %ConvTbl = (
'a' => '丐',
'i' => '中',
'u' => '丹',
'e' => '尹',
'o' => '云',
'ka' => '井',
'ki' => '五',
'ku' => '仁',
'ke' => '仃',
'ko' => '仇',
'sa' => '今',
'shi' => '仄',
'si' => '仄',
'su' => '允',
'se' => '六',
'so' => '公',
'ta' => '凶',
'chi' => '切',
'ti' => '切',
'tu' => '勾',
'te' => '化',
'to' => '午',
'na' => '卅',
'ni' => '卞',
'nu' => '厄',
'ne' => '友',
'no' => '及',
'ha' => '反',
'hi' => '夫',
'hu' => '孔',
'he' => '尺',
'ho' => '幻',
'ma' => '引',
'mi' => '心',
'mu' => '戈',
'me' => '戶',
'mo' => '手',
'ya' => '支',
'yi' => '中',
'yu' => '斗',
'ye' => '尹',
'yo' => '方',
'ra' => '日',
'ri' => '曰',
'ru' => '月',
're' => '木',
'ro' => '欠',
'wa' => '歹',
'wi' => '丹不',
'wu' => '丹',
'we' => '丹之',
'wo' => '毛',
'n' => '氏',
'm' => '氏',
'da' => '分',
'di' => '刈',
'du' => '勿',
'de' => '匹',
'do' => '升',
'ba' => '壬',
'bi' => '太',
'bu' => '少',
'be' => '屯',
'bo' => '廿',
'ca' => '五扎',
'ci' => '切',
'cu' => '五文',
'ce' => '六',
'co' => '仇',
'fa' => '孔丑',
'fi' => '孔不',
'fu' => '孔',
'fe' => '孔之',
'fo' => '孔予',
'ga' => '互',
'gi' => '亢',
'gu' => '什',
'ge' => '仆',
'go' => '仍',
'ja' => '元扎',
'ji' => '元',
'ju' => '元文',
'je' => '元之',
'jo' => '元斤',
'la' => '日',
'li' => '曰',
'lu' => '月',
'le' => '木',
'lo' => '欠',
'pa' => '天',
'pi' => '太',
'pu' => '少',
'pe' => '巴',
'po' => '弔',
'qa' => '仁丑',
'qi' => '仁不',
'qu' => '仁',
'qe' => '仁之',
'qo' => '仁予',
'va' => '任丑',
'vi' => '任不',
'vu' => '任',
've' => '任之',
'vo' => '任予',
'xa' => '仄扎',
'xi' => '仄不',
'xu' => '允丰',
'xe' => '仄之',
'xo' => '允予',
'za' => '介',
'zi' => '元',
'zu' => '內',
'ze' => '兮',
'zo' => '冗',
'cha' => '切扎',
'chi' => '切',
'chu' => '切文',
'che' => '切之',
'cho' => '切斤',
'tsa' => '勾丑',
'tsi' => '切',
'tsu' => '勾',
'tse' => '勾之',
'tso' => '勾予',
'tha' => '仄扎',
'thi' => '仄',
'thu' => '切文',
'the' => '六',
'tho' => '公',
'kya' => '五扎',
'kyi' => '五不',
'kyu' => '五扎',
'kye' => '五之',
'kyo' => '五斤',
'sya' => '仄扎',
'syi' => '仄不',
'syu' => '仄文',
'sye' => '仄之',
'syo' => '仄斤',
'sha' => '仄扎',
'shi' => '仄',
'shu' => '仄文',
'she' => '仄之',
'sho' => '仄斤',
'tya' => '切扎',
'tyi' => '切不',
'tyu' => '切文',
'tye' => '切之',
'tyo' => '切予',
'nya' => '卞扎',
'nyi' => '卞不',
'nyu' => '卞文',
'nye' => '卞之',
'nyo' => '卞斤',
'hya' => '夫扎',
'hyi' => '夫不',
'hyu' => '夫文',
'hye' => '夫之',
'hyo' => '夫斤',
'mya' => '心扎',
'myi' => '心不',
'myu' => '心不',
'mye' => '心之',
'myo' => '心斤',
'rya' => '曰扎',
'ryi' => '曰不',
'ryu' => '曰文',
'rye' => '曰之',
'ryo' => '曰斤',
'wya' => '歹丑',
'wyi' => '歹不',
'wyu' => '歹丰',
'wye' => '歹之',
'wyo' => '歹予',
'nji' => '氏元',
'nta' => '氏凶',
);
foreach my $key (keys %ConvTbl) {
Jcode::convert(\$ConvTbl{$key}, "utf8", $ScriptCodeCharCode);
}
my $ltu = "勻";
Jcode::convert(\$ltu, "utf8", $ScriptCodeCharCode);
my $oh = "云云";
Jcode::convert(\$oh, "utf8", $ScriptCodeCharCode);
my $nn = "氏";
Jcode::convert(\$nn, "utf8", $ScriptCodeCharCode);
#===============================================
# 戊件旦玄仿弁正’犯旦玄仿弁正
#===============================================
sub new
{
my ($module, $buff) = @_;
my $this = {};
bless $this;
$this->{pJcode} = new Jcode;
$this->{pUTF8} = find_encoding('utf8');
$this->{pEUCjp} = find_encoding('eucjp');
$this->{pSJIS} = find_encoding('sjis');
return $this;
}
sub DESTROY
{
my $this = shift;
$this->Close();
}
sub Initialize
{
my ($this) = @_;
}
#===============================================
# 庍醒潸ぜ楮醒
#===============================================
#===============================================
# 域�怚嵽韞迡媬�
#===============================================
sub Hankaku2Zenkaku
{
my ($this, $s, $sourcecharcode, $ConvertKana) = @_;
$sourcecharcode = $this->getcode($s) if(!defined $sourcecharcode);
$ConvertKana = 1 if(!defined $ConvertKana);
#print("c[$ScriptCodeCharCode][$sourcecharcode]
\n");
$this->convert(\$s, $ScriptCodeCharCode, $sourcecharcode);
# $s = Jcode->new($s)->tr('a-zA-Z0-9 @!\"#$%&\'()=-^\\|{}:;+*?_/<>',
# '��-����-�琭�-ㄨ﹛→!﹎#$☉↓﹊()=≡※∴ ̄-ד”+←?〃/∼∩')->h2z() . '';
$s = Jcode->new($s)->tr('a-zA-Z0-9 ', '��-����-�琭�-ㄨ﹛');
$s = $s->tr('@!\"#$%&\'', '→〞﹎↑♀☉↓﹊');
$s = $s->tr('()=-^\\|{}:;+*?_/<>', '()=≡※∴ ̄-ד”+←〝〃▼∼∩');
if($ConvertKana) {
$s = $s->h2z() . '';
}
else {
$s = $s . '';
}
$this->convert(\$s, $sourcecharcode, $ScriptCodeCharCode);
return $s;
# return Jcode->new($s)->h2z();
}
sub Zenkaku2Hankaku
{
my ($this, $s, $sourcecharcode, $ConvertKana) = @_;
$sourcecharcode = $this->getcode($s) if(!defined $sourcecharcode);
$ConvertKana = 1 if(!defined $ConvertKana);
$this->convert(\$s, $ScriptCodeCharCode, $sourcecharcode);
# $s = Jcode->new($s)->tr('��-����-�琭�-ㄨ﹛→〞﹎↑♀☉↓﹊﹋﹌﹥≡※∴ ̄-ד”≒←〝〃▼∼∩',
# 'a-zA-Z0-9 @!"#$%&\'()=-^\\|{}:;+*?_/<>')->z2h() . '';
$s = Jcode->new($s)->tr('��-����-�琭�-ㄨ﹛', 'a-zA-Z0-9 ');
$s = $s->tr('→〞﹎↑♀☉↓﹊', '@!\"#$%&\'');
$s = $s->tr('()=≡※∴ ̄-ד”+←〝〃▼∼∩', '()=-^\\|{}:;+*?_/<>');
if($ConvertKana) {
$s = $s->z2h() . '';
}
else {
$s = $s . '';
}
$this->convert(\$s, $sourcecharcode, $ScriptCodeCharCode);
return $s;
# return Jcode->new($s)->z2h();
}
sub Zenkaku2HankakuKana
{
my ($this, $s, $sourcecharcode) = @_;
$sourcecharcode = $this->getcode($s) if(!defined $sourcecharcode);
$s = Jcode->new($s)->z2h() . '';
$this->convert(\$s, $sourcecharcode);
return $s;
}
sub HankakuKana2Zenkaku
{
my ($this, $s, $sourcecharcode) = @_;
$sourcecharcode = $this->getcode($s) if(!defined $sourcecharcode);
$this->convert(\$s, $ScriptCodeCharCode, $sourcecharcode);
$s = Jcode->new($s)->h2z() . '';
$this->convert(\$s, $sourcecharcode, $ScriptCodeCharCode);
return $s;
}
sub Hiragana2Katakana
{
my ($this, $s, $sourcecharcode) = @_;
$sourcecharcode = $this->getcode($s) if(!defined $sourcecharcode);
$s = Jcode->new($s)->tr('丑-氏', '央-件') . '';
$this->convert(\$s, $sourcecharcode, $ScriptCodeCharCode);
return $s;
}
sub Katakana2Hiragana
{
my ($this, $s, $sourcecharcode) = @_;
$sourcecharcode = $this->getcode($s) if(!defined $sourcecharcode);
$this->convert(\$s, $ScriptCodeCharCode, $sourcecharcode);
$s = $this->HankakuKana2Zenkaku($s, $sourcecharcode);
$s = Jcode->new($s)->tr('失-件', '丐-氏') . '';
$this->convert(\$s, $sourcecharcode);
return $s;
}
#my $ZenkakuKigou = "[〞-℅﹛]";
#my $ZenkakuSpace = "([!-~])﹛([!-~])";
#my $ZenkakuAlphabet = "[��-�琭�-��ㄟ-ㄨ﹛≡▽﹞﹜﹝‘]";
#Jcode::convert(\$ZenkakuKigou, 'utf8', $ScriptCodeCharCode);
#Jcode::convert(\$ZenkakuSpace, 'utf8', $ScriptCodeCharCode);
#Jcode::convert(\$ZenkakuAlphabet, 'utf8', $ScriptCodeCharCode);
sub Zen2Han
{
my ($this, $s, $sourcecharcode) = @_;
$sourcecharcode = $this->getcode($s) if(!defined $sourcecharcode);
$s = $this->Zenkaku2Hankaku($s, $sourcecharcode) . '';
# $this->convert(\$s, $sourcecharcode);
return $s;
}
sub Roma2Kana {
my ($this, $s, $charcode, $sourcecharcode) = @_;
$charcode = 'sjis' if(!defined $charcode);
$sourcecharcode = $this->getcode($s) if(!defined $sourcecharcode);
$s = lc $this->Zenkaku2Hankaku($s, $sourcecharcode);
$this->convert(\$s, "utf8", $sourcecharcode);
$s =~ s/([aiueo])h([bcdfghjklmnpqrstvwxyz])/$1$1$2/g;
$s =~ s/[nm]([^aiueon])/$nn$1/g;
$s =~ s/nn/$nn/g;
$s =~ s/n-([aiueo])/$nn$1/g;
my ($others, $hit, $rest);
my $ret = '';
while(1) {
#print "s=[$s]\n";
my ($others, $hit, $rest) = ($s =~ /^(.*?)([bcdfghjklmnpqrstvwxyz]*[aiueon])(.*)$/sg);
if(!defined $hit) {
$this->convert(\$s, $charcode, "utf8");
$ret .= $s;
return $ret;
}
my $pre = '';
if($hit =~ /^([bcdfghjklmnpqrstvwxyz])([bcdfghjklmnpqrstvwxyz])(.*)$/) {
if($1 eq $2) {
# $pre = $ltu;
# $hit = $2 . $3;
}
}
if($hit =~ /^(oh)(.*)$/) {
# $pre .= $oh;
# $hit = $2;
}
$hit = $ConvTbl{$hit} if(defined $ConvTbl{$hit});
$this->convert(\$others, $charcode, "utf8");
$this->convert(\$hit, $charcode, "utf8");
$this->convert(\$pre, $charcode, "utf8");
$ret .= $others . $pre .$hit;
$s = $rest;
}
}
sub Conv2Hiragana
{
my ($this, $s, $sourcecharcode, $targetcharcode) = @_;
$sourcecharcode = Jcode::getcode($s);
$this->convert(\$s, $ScriptCodeCharCode, $sourcecharcode);
if($this->match($s, "^[丑-氏央-件��-�廅*\$")) {
$this->convert(\$s, $sourcecharcode, $ScriptCodeCharCode);
return $this->Katakana2Hiragana($s, $sourcecharcode, $targetcharcode);
}
$this->convert(\$s, $sourcecharcode, $ScriptCodeCharCode);
return $this->Kakasi($s, $sourcecharcode, $targetcharcode, qw/-aH -jH -KH -JH -EH -kH/);
# return $this->Kakasi($s, $sourcecharcode, $targetcharcode, qw/-ieuc -oeuc -aH -jH -KH -JH -EH -kH/);
}
sub Conv2Roma
{
my ($this, $s, $sourcecharcode, $targetcharcode) = @_;
return $this->Kakasi($s, $sourcecharcode, $targetcharcode, qw/-Ha -ja -Ka -Ja -Ea -ka/);
# return $this->Kakasi($s, $sourcecharcode, $targetcharcode, qw/-ieuc -oeuc -Ha -ja -Ka -Ja -Ea -ka/);
}
sub Kakasi {
my ($this, $s, $sourcecharcode, $targetcharcode, @options) = @_;
$sourcecharcode = $this->getcode($sourcecharcode) if(!defined $sourcecharcode);
$targetcharcode = $sourcecharcode if(!defined $targetcharcode);
return $s if($s eq '');
#print "s1: [$s]
\n";
$s =~ s/([^\\])([\(\)\{\}\[\]\>\<\*\?\|\!\$\%\&\~\=\-\'\"\`\/\#])/$1\\$2/g;
$this->convert(\$s, $KakasiCharCode, $sourcecharcode);
#print "s2: [$s]
\n";
if($UseKakasiModule) {
my $kakasi = Text::Kakasi->new(@options);
$s = $kakasi->get($s);
}
else {
my $cmd = "$KakasiPath " . join(' ', @options);
#print "cmd: [$cmd]\n";
#$s =~ s/\(/\\(/g;
#$s =~ s/\)/\\(/g;
#print "[$s]
\n";
$s = `echo $s | $cmd`;
$s =~ s/[\r\n]+$//s;
}
$this->convert(\$s, $targetcharcode, $KakasiCharCode);
return $s;
}
1;