如何在 Perl 中对中文、日文和韩文 (CJK) 字符进行排序?
据我所知,按笔划数排序 CJK 字符,然后按部首,似乎是这些语言的排序方式。还有一些按声音排序的方法,但这似乎不太常见。
我试过使用:
perl -e 'print join(" ", sort qw(工 然 一 人 三 古 二 )), "\n";'
# Prints: 一 三 二 人 古 工 然 which is incorrect
我试过使用 CPAN 中的 Unicode::Collate,但它说:
By default, CJK Unified Ideographs are ordered in Unicode codepoint order...
如果我可以获得每个字符的笔画数数据库,我可以轻松地对所有字符进行排序,但这似乎不是 Perl 附带的,也没有封装在我能找到的任何模块中。
如果您知道如何用其他语言对 CJK 进行排序,那么在回答这个问题时提及它会很有帮助。
最佳答案
见 TR38对于肮脏的细节和角落案例。这并不像您想象的那么简单,也不像此代码示例那样简单。
use 5.010;
use utf8;
use Encode;
use Unicode::Unihan;
my $u = Unicode::Unihan->new;
say encode_utf8 sprintf "Character $_ has the radical #%s and %d residual strokes." , split /[.]/, $u->RSUnicode($_) for qw(工 然 一 人 三 古 二);
__END__
Character 工 has the radical #48 and 0 residual strokes.
Character 然 has the radical #86 and 8 residual strokes.
Character 一 has the radical #1 and 0 residual strokes.
Character 人 has the radical #9 and 0 residual strokes.
Character 三 has the radical #1 and 2 residual strokes.
Character 古 has the radical #30 and 2 residual strokes.
Character 二 has the radical #7 and 0 residual strokes.
见 http://en.wikipedia.org/wiki/List_of_Kangxi_radicals用于从激进序数到笔画数的映射。
关于perl - 您如何在 Perl 或任何其他编程语言中对 CJK(亚洲)字符进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3891556/