如何在Perl或任何其他编程语言中对CJK(亚洲)字符进行排序?

时间:2010-10-08 14:28:14

标签: perl sorting unicode collation cjk

如何在Perl中对中文,日文和韩文(CJK)字符进行排序?

据我所知,按行程计数排序CJK字符,然后按激进排序,似乎就是这些语言的排序方式。还有一些按声音排序的方法,但这似乎不太常见。

我尝试过使用:

perl -e 'print join(" ", sort qw(工 然 一 人 三 古 二 )), "\n";'
# Prints: 一 三 二 人 古 工 然 which is incorrect

我尝试过使用CPAN中的Unicode :: Collat​​e,但它说:

  

默认情况下,CJK统一表意文字是   以Unicode代码点顺序排序......

如果我可以获得每个字符的笔画计数数据库,我可以轻松地对所有字符进行排序,但这似乎不是Perl所带来的,也不是封装在我能找到的任何模块中。

如果你知道如何用其他语言对CJK进行排序,那么在这个问题的答案中提及它会很有帮助。

3 个答案:

答案 0 :(得分:4)

请参阅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

答案 1 :(得分:2)

日语电话簿按语音分类(gojûon整理)。但是,无论是Unicode,JIS,S-JIS还是EUC,汉字字符顺序都不是基于语音。只有假名是基于语音顺序。这意味着如果没有语音转换,你就无法进行有意义的整理!

例如:

a) kanji:           東京駅
b) kana converted:  とうきょうえき
c) romanisation:    tôkyô eki

使用b)或c),您可以进行有意义的排序。但你不能只用a)。当然,您可以运行普通排序功能,但对日语没有意义。

答案 2 :(得分:2)

查看我的rubygem toPinyin,它会将UTF-8编码的中文字符转换为他们的PinYin(发音)。然后,可以轻松地对拼音进行排序。

简单地说,gem install toPinyin

require 'toPinyin'

words = "
人
没有
理想
跟
咸鱼
有
什么
区别
".split("\n")

words.sort! {|a ,b|   a.pinyin.join <=> b.pinyin.join }

https://github.com/pierrchen/toPinyin