手抜き日中判別(繁体字)

前回の続き。
繁体字の場合。

use strict;
use utf8;
use Encode;

my $enc_cp950 = find_encoding('cp950');
my $enc_cp932 = find_encoding('cp932');

my $level2_eucjp;
$level2_eucjp .= chr(0xd0 + ($_ / 94)) . chr(0xa1 + ($_ % 94)) for 0..3389; # 3390 : number of level2 chars
my $level2 = Encode::decode('euc-jp', $level2_eucjp);
my $count_level2 = eval "sub { \$_[0] =~ tr/$level2//; }";

my $str = '(判別したいテキスト)';
my $is_zhTW = 0;
my $questions_before = ($str =~ tr/?//);
my $questions_cp950 = ($enc_cp950->encode($str) =~ tr/?//);
my $questions_cp932_and_level2 = ($enc_cp932->encode($str) =~ tr/?//) + &$count_level2($str);
$is_zhTW = 1 if $questions_before == $questions_cp950 and $questions_cp932_and_level2 > $questions_before;

CP950 で文字化けがなく、cp932 での文字化けまたは第二水準の漢字がある時に限り、繁体字中国語と見なす。

アドホックな、いかにも Perl らしいコードだけど、一応動くはず。