| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Lingua::ZH::Romanize::DictZH - Dictionary converter | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | perl -MLingua::ZH::Romanize::DictZH -e 'Lingua::ZH::Romanize::DictZH->update();' | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | This module creates dictionary cache files for | ||
| 12 | L<Lingua::ZH::Romanize::Pinyin> module. | ||
| 13 | |||
| 14 | Source dictionary files: (included in this package) | ||
| 15 | |||
| 16 | Lingua/ZH/Romanize/big5/CTLauBig5.tit | ||
| 17 | Lingua/ZH/Romanize/big5/PY.tit | ||
| 18 | Lingua/ZH/Romanize/gb/CTLau.tit | ||
| 19 | Lingua/ZH/Romanize/gb/PY.tit | ||
| 20 | |||
| 21 | Cached dictionary files: | ||
| 22 | |||
| 23 | Lingua/ZH/Romanize/Cantonese.store | ||
| 24 | Lingua/ZH/Romanize/Pinyin.store | ||
| 25 | |||
| 26 | DictZH is called only from Makefile.PL in the Pinyin package. | ||
| 27 | |||
| 28 | =head1 REQUIRED MODULES | ||
| 29 | |||
| 30 | L<Storable> module is required to create cached dictionary files. | ||
| 31 | Both of L<Unicode::Map> and L<Unicode::String> modules are | ||
| 32 | required on Perl 5.005 and 5.6.x to install the Pinyin package. | ||
| 33 | L<Encode> module is used on Perl 5.8.x. | ||
| 34 | |||
| 35 | =head1 SEE ALSO | ||
| 36 | |||
| 37 | L<Lingua::ZH::Romanize::Pinyin> | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | package Lingua::ZH::Romanize::DictZH; | ||
| 42 | use strict; | ||
| 43 | use vars qw( $VERSION ); | ||
| 44 | $VERSION = "0.23"; | ||
| 45 | |||
| 46 | my $PERL581 = 1 if ( $] >= 5.008001 ); | ||
| 47 | |||
| 48 | my $DICT_FILES = { | ||
| 49 | 'Cantonese' => [qw( | ||
| 50 | cxterm/dict/big5/CTLauBig5.tit | ||
| 51 | cxterm/dict/gb/CTLau.tit | ||
| 52 | )], | ||
| 53 | 'Pinyin' => [qw( | ||
| 54 | cxterm/dict/big5/PY.tit | ||
| 55 | cxterm/dict/gb/PY.tit | ||
| 56 | )], | ||
| 57 | }; | ||
| 58 | |||
| 59 | sub target { | ||
| 60 | ( keys %$DICT_FILES ); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub update { | ||
| 64 | my $package = shift; | ||
| 65 | my $base = shift; | ||
| 66 | print "Updater: ", __PACKAGE__, " (", $VERSION, ")\n"; | ||
| 67 | |||
| 68 | unless ( defined $base ) { | ||
| 69 | $base = $INC{ join( '/', split( '::', (__PACKAGE__) . '.pm' ) ) }; | ||
| 70 | $base =~ s#/[^/]*$##; | ||
| 71 | } | ||
| 72 | |||
| 73 | my @target = $package->target(); | ||
| 74 | my $update = 0; | ||
| 75 | foreach my $mode ( @target ) { | ||
| 76 | my $storename = $mode.'.store'; | ||
| 77 | my $storepath = $base . '/' . $storename; | ||
| 78 | if ( -r $storepath ) { | ||
| 79 | warn "Already-Exist: $storepath\n"; | ||
| 80 | } | ||
| 81 | else { | ||
| 82 | print "Path: ", $storepath, "\n"; | ||
| 83 | $update++; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | warn "Loading-Module: Storable.pm\n"; | ||
| 88 | &require_storable(); # required | ||
| 89 | |||
| 90 | if ($PERL581) { | ||
| 91 | warn "Loading-Module: Encode.pm\n"; | ||
| 92 | &require_encode(); # Perl 5.8.x | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | warn "Loading-Module: Unicode::Map\n"; | ||
| 96 | &require_unicode_map(); # Perl 5.005/5.6.x | ||
| 97 | warn "Loading-Module: Unicode::String\n"; | ||
| 98 | &require_unicode_string(); # | ||
| 99 | } | ||
| 100 | |||
| 101 | foreach my $mode ( @target ) { | ||
| 102 | my $storename = $mode.'.store'; | ||
| 103 | my $hash = {}; | ||
| 104 | my $titlist = $DICT_FILES->{$mode}; | ||
| 105 | foreach my $titpath ( @$titlist ) { | ||
| 106 | warn "Loading-Dictionary: $titpath\n"; | ||
| 107 | $hash = &read_tit_dict( $titpath, $hash ); | ||
| 108 | } | ||
| 109 | foreach my $key ( keys %$hash ) { | ||
| 110 | next unless ref $hash->{$key}; | ||
| 111 | my $list = $hash->{$key}; | ||
| 112 | my $uniq = { map { $_ => 1 } @$list }; | ||
| 113 | foreach my $chk ( @$list ) { | ||
| 114 | next unless ( $chk =~ s/[0-9]+$// ); | ||
| 115 | delete $uniq->{$chk} if exists $uniq->{$chk}; | ||
| 116 | } | ||
| 117 | $hash->{$key} = join( '/', sort keys %$uniq ); | ||
| 118 | } | ||
| 119 | my $storepath = $base . '/' . $storename; | ||
| 120 | warn "Writing-Storable: $storepath\n"; | ||
| 121 | Storable::store( $hash, $storepath ) or die "$! - $storename\n"; | ||
| 122 | } | ||
| 123 | |||
| 124 | print "Done.\n"; | ||
| 125 | |||
| 126 | undef; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub read_tit_dict { | ||
| 130 | my $titname = shift or return; | ||
| 131 | my $hash = shift || {}; | ||
| 132 | my $cmap = {qw( GB GB2312 BIG5 BIG5 KS EUC-KR JIS EUC-JP )}; | ||
| 133 | my $unistr = Unicode::String->new() unless $PERL581; | ||
| 134 | |||
| 135 | # find ENCODE: and wait until BEGINDICTIONARY | ||
| 136 | open( TIT, $titname ) or die "$! - $titname\n"; | ||
| 137 | my $code; | ||
| 138 | while (<TIT>) { | ||
| 139 | next if /^#/; | ||
| 140 | $code = $cmap->{ uc($1) } if (/^ENCODE:\s*(\S+)/); | ||
| 141 | last if /^BEGINDICTIONARY/; | ||
| 142 | } | ||
| 143 | warn "Dictionary-Encoding: $code\n" if $code; | ||
| 144 | my $unimap = Unicode::Map->new($code) unless $PERL581; | ||
| 145 | |||
| 146 | while ( my $line = <TIT> ) { | ||
| 147 | next if ( $line =~ /^#/ ); | ||
| 148 | chomp $line; | ||
| 149 | |||
| 150 | my ( $roman, $kanji ) = split( /\s+/, $line, 2 ); | ||
| 151 | $roman =~ s/^\\[0-7]{3}//s; | ||
| 152 | # $roman =~ s/\d+$//s; | ||
| 153 | |||
| 154 | # convert encoding from GB/BIG5 to UTF-8 | ||
| 155 | if ($code) { | ||
| 156 | if ($PERL581) { | ||
| 157 | Encode::from_to( $kanji, $code, 'UTF-8' ); # GB/BIG5 to UTF-8 | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | my $utf16 = $unimap->to_unicode($kanji); # GB/BIG5 to UCS2 | ||
| 161 | $unistr->ucs2($utf16); | ||
| 162 | $kanji = $unistr->utf8(); # UCS2 to UTF-8 | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | # split every UTF-8 wide characters | ||
| 167 | while ( $kanji =~ /([\300-\377][\200-\277]+)/g ) { | ||
| 168 | my $char = $1; | ||
| 169 | if ( !exists $hash->{$char} ) { | ||
| 170 | $hash->{$char} = $roman; | ||
| 171 | } | ||
| 172 | elsif ( !ref $hash->{$char} ) { | ||
| 173 | $hash->{$char} = [ $hash->{$char}, $roman ]; | ||
| 174 | } | ||
| 175 | else { | ||
| 176 | push( @{ $hash->{$char} }, $roman ); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | } | ||
| 180 | close(TIT); | ||
| 181 | |||
| 182 | $hash; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub require_storable { | ||
| 186 | return if defined $Storable::VERSION; | ||
| 187 | local $@; | ||
| 188 | eval { require Storable; }; | ||
| 189 | die "Storable module is required.\n" if $@; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub require_encode { | ||
| 193 | return if defined $Encode::VERSION; | ||
| 194 | local $@; | ||
| 195 | eval { require Encode; }; | ||
| 196 | die "Encode module is required.\n" if $@; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub require_unicode_string { | ||
| 200 | return if defined $Unicode::String::VERSION; | ||
| 201 | local $@; | ||
| 202 | eval { require Unicode::String; }; | ||
| 203 | die "Unicode::String module is required.\n" if $@; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub require_unicode_map { | ||
| 207 | return if defined $Unicode::Map::VERSION; | ||
| 208 | local $@; | ||
| 209 | eval { require Unicode::Map; }; | ||
| 210 | die "Unicode::Map module is required.\n" if $@; | ||
| 211 | } | ||
| 212 | |||
| 213 | package Lingua::ZH::Romanize::DictZH::Pinyin; | ||
| 214 | use strict; | ||
| 215 | use base qw( Lingua::ZH::Romanize::DictZH ); | ||
| 216 | sub target { 'Pinyin' } | ||
| 217 | |||
| 218 | package Lingua::ZH::Romanize::DictZH::Cantonese; | ||
| 219 | use strict; | ||
| 220 | use base qw( Lingua::ZH::Romanize::DictZH ); | ||
| 221 | sub target { 'Cantonese' } | ||
| 222 | |||
| 223 | 1; |