本文本着学习分享的目的,介绍一种基于java.text.Collator的比较挫的方法,实现汉字转换为拼音字符串。当然我得暂时不考虑汉字多音字的问题。最终结果是这样:
吃 -> CHI 睡 -> SHUI 做 -> ZUO
需要直接拷贝代码的请拉至贴末尾。
1,Unicode编码
古代秦国统一六国汉字,而Unicode编码则统一了全世界文字。它整理了世界上大部分国家的文字,使得计算机可以简单地显示和处理它们。Unicode编码方式有UTF-8, UTF-16, UTF-32 (Unicode Transformation Format). 为了表示0x10FFFF这么多个字符,UTF-8, UTF-16 对不同范围的字符使用不同长度的编码。具体怎么划分编码范围,请自行网上搜索。这里只提起,汉字的编码范围是U+4E00 - U+9FFF。
Unicode字符百科 : http://unicode-table.com/cn/#4E00
2,java.text.Collator
Java 有别的一些比较和排序的类方法。Collator主要用来处理某种特定语言的比较和排序。比如我们这里的 中文。
它提供四种等级的比较:
Primary : 基本等级,字典里面会按照基本字符进行排序。比如 "a"<"b"<"c", "as"=="às", "a"=="A"
Secondary : 第二等级,若基本等级比较结果相等,则根据语言考虑字符的声调。比如 "as"<"às", "a"=="A"
Tertiary : 第三等级,若基本等级,第二等级比较结果相等,则根据字符的大小写。比如 "a"<"A"
Identical : 骨灰级,若上面几种对比均相等,则由这个等级决定雌雄。希伯来语Hebrew会用到这个等级。但是总的情况非常少,使用这个等级会降低代码效率。
在做比较之前,我们可以通过方法 setStrength( int ) 来设置比较等级:Collator.PRIMARY, Collator.SECONDARY, Collator.TERTIARY, Collator.IDENTICAL。
Collator类对汉字的排序,就是新华字典里面的排序。
3,汉字拼音
因为有多音字,拼音与汉字是一种多对多的关系。不好搞。然而多音字其实占一个小部分而已,所以我们这里先忽略多音字的存在。现在关系明朗了许多:拼音汉字是1对多的关系了。翻看新华词典,我们发现拼音数量是有限的。好了,那我们就通过查表来解决问题。我们的表是这样的:
表A: { 呵, 哎, 安, 肮, 凹, 八, 挀, ... }
表B: { A, AI, AN, ANG, AO, BA, BAI, ...}
好了,你应该看出来了。最卑鄙无耻的穷举大法!列出所有拼音,以及拼音对应的第一个汉字。总共有407个拼音。其实说多也不多吧?
接下来怎么办呢?通过很贱的二分法查找,找到汉字的同音字!比如:
我们要将"霸"字转换为拼音。
Collator 取表A内容,比较结果:"八" < "霸" < "挀"
结果取表B内容,输出 "BA"
4,思考
在代码里面定义两个表(二维数组)开销其实很少,另外,采用二分法查找,面对407个Item 最坏的情况也不会超过10次比较操作,我认为还是可取的。
有人会问,那有没有办法把拼音转换为汉字?首先,一个拼音对应的汉字非常多,这个问题的结果是一个集合。另外,实际情况我们似乎不需要这么搞。某个场合需要通过拼音找汉字,我们仍然可以先将汉字转换为拼音,然后再做匹配。
5,代码
package com.yarkey.hanzitopinyin;
import java.text.Collator;
import java.util.Locale;
public class HanziToPinyin {
// min & max unicode of chinese character
private static final char HANZI_UNICODE_MIN = '\u4E00';
private static final char HANZI_UNICODE_MAX = '\u9FFF';
// the first & last chinese character in dictionary
private static final String FIRST_HANZI = "\u963F";
private static final String LAST_HANZI = "\u84D9";
// array of the first chinese character of Pinyin
private static final char[] HANZI_UNICODE = { '\u5475', '\u54ce', '\u5b89', '\u80ae', '\u51f9', '\u516b', '\u6300',
'\u6273', '\u90a6', '\u5305', '\u5351', '\u5954', '\u4f3b', '\u5c44', '\u8fb9', '\u6807', '\u618b',
'\u90a0', '\u69df', '\u7676', '\u5cec', '\u5693', '\u5a47', '\u98e1', '\u4ed3', '\u64cd', '\u518a',
'\u5d7e', '\u564c', '\u53c9', '\u9497', '\u8fbf', '\u4f25', '\u6284', '\u8f66', '\u62bb', '\u67fd',
'\u5403', '\u5145', '\u62bd', '\u51fa', '\u6b3b', '\u63e3', '\u5ddd', '\u75ae', '\u5439', '\u6776',
'\u9034', '\u75b5', '\u5306', '\u51d1', '\u7c97', '\u6c46', '\u5d14', '\u90a8', '\u6413', '\u5491',
'\u5927', '\u75b8', '\u5f53', '\u5200', '\u6dc2', '\u5f97', '\u6265', '\u706f', '\u6c10', '\u55f2',
'\u7538', '\u5201', '\u7239', '\u4ec3', '\u4e1f', '\u4e1c', '\u5517', '\u561f', '\u5073', '\u5806',
'\u9413', '\u591a', '\u5a40', '\u8bf6', '\u5940', '\u97a5', '\u800c', '\u53d1', '\u5e06', '\u65b9',
'\u98de', '\u5206', '\u4e30', '\u8985', '\u4ecf', '\u7d11', '\u4f15', '\u65ee', '\u8be5', '\u7518',
'\u5188', '\u768b', '\u6208', '\u7d66', '\u6839', '\u5e9a', '\u5de5', '\u52fe', '\u4f30', '\u74dc',
'\u7f6b', '\u5173', '\u5149', '\u5f52', '\u886e', '\u5459', '\u54c8', '\u54b3', '\u9878', '\u82c0',
'\u84bf', '\u8bc3', '\u9ed2', '\u62eb', '\u4ea8', '\u5677', '\u543d', '\u9f41', '\u5322', '\u82b1',
'\u6000', '\u72bf', '\u5ddf', '\u7070', '\u660f', '\u5419', '\u4e0c', '\u52a0', '\u620b', '\u6c5f',
'\u827d', '\u9636', '\u5dfe', '\u52a4', '\u5182', '\u52fc', '\u530a', '\u5a1f', '\u5658', '\u519b',
'\u5494', '\u5f00', '\u520a', '\u95f6', '\u5c3b', '\u533c', '\u524b', '\u80af', '\u962c', '\u7a7a',
'\u62a0', '\u5233', '\u5938', '\u84af', '\u5bbd', '\u5321', '\u4e8f', '\u5764', '\u6269', '\u5783',
'\u6765', '\u5170', '\u5577', '\u635e', '\u4ec2', '\u52d2', '\u5844', '\u5215', '\u5006', '\u5941',
'\u826f', '\u64a9', '\u5217', '\u62ce', '\u3007', '\u6e9c', '\u9f99', '\u779c', '\u565c', '\u5a08',
'\u7567', '\u62a1', '\u7f57', '\u5463', '\u5988', '\u973e', '\u5ada', '\u9099', '\u732b', '\u9ebc',
'\u6c92', '\u95e8', '\u753f', '\u54aa', '\u7720', '\u55b5', '\u54a9', '\u6c11', '\u540d', '\u8c2c',
'\u6478', '\u54de', '\u6bea', '\u62cf', '\u5b7b', '\u56e1', '\u56ca', '\u5b6c', '\u8bb7', '\u9981',
'\u6041', '\u80fd', '\u59ae', '\u62c8', '\u5b22', '\u9e1f', '\u634f', '\u60a8', '\u5b81', '\u599e',
'\u519c', '\u7fba', '\u5974', '\u597b', '\u8650', '\u632a', '\u5594', '\u8bb4', '\u8db4', '\u62cd',
'\u7705', '\u4e53', '\u629b', '\u5478', '\u55b7', '\u5309', '\u4e15', '\u504f', '\u527d', '\u6c15',
'\u59d8', '\u4e52', '\u948b', '\u5256', '\u4ec6', '\u4e03', '\u6390', '\u5343', '\u545b', '\u6084',
'\u767f', '\u4fb5', '\u9751', '\u909b', '\u4e18', '\u66f2', '\u5f2e', '\u7f3a', '\u590b', '\u5465',
'\u7a63', '\u5a06', '\u60f9', '\u4eba', '\u6254', '\u65e5', '\u8338', '\u53b9', '\u5982', '\u5827',
'\u6875', '\u95f0', '\u82e5', '\u4ee8', '\u6be2', '\u4e09', '\u6852', '\u63bb', '\u8272', '\u68ee',
'\u50e7', '\u6740', '\u7b5b', '\u5c71', '\u4f24', '\u5f30', '\u5962', '\u7533', '\u5347', '\u5c38',
'\u53ce', '\u4e66', '\u5237', '\u6454', '\u95e9', '\u53cc', '\u8c01', '\u542e', '\u5981', '\u53b6',
'\u5fea', '\u635c', '\u82cf', '\u72fb', '\u590a', '\u5b59', '\u5506', '\u4ed6', '\u82d4', '\u574d',
'\u94f4', '\u5932', '\u5fd1', '\u71a5', '\u5254', '\u5929', '\u4f7b', '\u5e16', '\u5385', '\u56f2',
'\u5077', '\u92c0', '\u6e4d', '\u63a8', '\u541e', '\u6258', '\u6316', '\u6b6a', '\u5f2f', '\u5c2a',
'\u5371', '\u586d', '\u7fc1', '\u631d', '\u4e4c', '\u5915', '\u867e', '\u4eda', '\u4e61', '\u7071',
'\u4e9b', '\u5fc3', '\u661f', '\u51f6', '\u4f11', '\u65f4', '\u8f69', '\u75b6', '\u52cb', '\u4e2b',
'\u6079', '\u592e', '\u5e7a', '\u8036', '\u4e00', '\u6b2d', '\u5e94', '\u54df', '\u4f63', '\u4f18',
'\u625c', '\u9e22', '\u66f0', '\u6655', '\u531d', '\u707d', '\u7ccc', '\u7242', '\u50ae', '\u5219',
'\u8d3c', '\u600e', '\u5897', '\u5412', '\u635a', '\u6cbe', '\u5f20', '\u948a', '\u8707', '\u8d1e',
'\u4e89', '\u4e4b', '\u4e2d', '\u5dde', '\u6731', '\u6293', '\u8de9', '\u4e13', '\u5986', '\u96b9',
'\u5b92', '\u5353', '\u5b5c', '\u5b97', '\u90b9', '\u79df', '\u94bb', '\u539c', '\u5c0a', '\u6628' };
// array of Pinyin
private static final byte[][] PINYINS = { { 65, 0, 0, 0, 0, 0 }, { 65, 73, 0, 0, 0, 0 }, { 65, 78, 0, 0, 0, 0 },
{ 65, 78, 71, 0, 0, 0 }, { 65, 79, 0, 0, 0, 0 }, { 66, 65, 0, 0, 0, 0 }, { 66, 65, 73, 0, 0, 0 },
{ 66, 65, 78, 0, 0, 0 }, { 66, 65, 78, 71, 0, 0 }, { 66, 65, 79, 0, 0, 0 }, { 66, 69, 73, 0, 0, 0 },
{ 66, 69, 78, 0, 0, 0 }, { 66, 69, 78, 71, 0, 0 }, { 66, 73, 0, 0, 0, 0 }, { 66, 73, 65, 78, 0, 0 },
{ 66, 73, 65, 79, 0, 0 }, { 66, 73, 69, 0, 0, 0 }, { 66, 73, 78, 0, 0, 0 }, { 66, 73, 78, 71, 0, 0 },
{ 66, 79, 0, 0, 0, 0 }, { 66, 85, 0, 0, 0, 0 }, { 67, 65, 0, 0, 0, 0 }, { 67, 65, 73, 0, 0, 0 },
{ 67, 65, 78, 0, 0, 0 }, { 67, 65, 78, 71, 0, 0 }, { 67, 65, 79, 0, 0, 0 }, { 67, 69, 0, 0, 0, 0 },
{ 67, 69, 78, 0, 0, 0 }, { 67, 69, 78, 71, 0, 0 }, { 67, 72, 65, 0, 0, 0 }, { 67, 72, 65, 73, 0, 0 },
{ 67, 72, 65, 78, 0, 0 }, { 67, 72, 65, 78, 71, 0 }, { 67, 72, 65, 79, 0, 0 }, { 67, 72, 69, 0, 0, 0 },
{ 67, 72, 69, 78, 0, 0 }, { 67, 72, 69, 78, 71, 0 }, { 67, 72, 73, 0, 0, 0 }, { 67, 72, 79, 78, 71, 0 },
{ 67, 72, 79, 85, 0, 0 }, { 67, 72, 85, 0, 0, 0 }, { 67, 72, 85, 65, 0, 0 }, { 67, 72, 85, 65, 73, 0 },
{ 67, 72, 85, 65, 78, 0 }, { 67, 72, 85, 65, 78, 71 }, { 67, 72, 85, 73, 0, 0 }, { 67, 72, 85, 78, 0, 0 },
{ 67, 72, 85, 79, 0, 0 }, { 67, 73, 0, 0, 0, 0 }, { 67, 79, 78, 71, 0, 0 }, { 67, 79, 85, 0, 0, 0 },
{ 67, 85, 0, 0, 0, 0 }, { 67, 85, 65, 78, 0, 0 }, { 67, 85, 73, 0, 0, 0 }, { 67, 85, 78, 0, 0, 0 },
{ 67, 85, 79, 0, 0, 0 }, { 68, 65, 0, 0, 0, 0 }, { 68, 65, 73, 0, 0, 0 }, { 68, 65, 78, 0, 0, 0 },
{ 68, 65, 78, 71, 0, 0 }, { 68, 65, 79, 0, 0, 0 }, { 68, 69, 0, 0, 0, 0 }, { 68, 69, 73, 0, 0, 0 },
{ 68, 69, 78, 0, 0, 0 }, { 68, 69, 78, 71, 0, 0 }, { 68, 73, 0, 0, 0, 0 }, { 68, 73, 65, 0, 0, 0 },
{ 68, 73, 65, 78, 0, 0 }, { 68, 73, 65, 79, 0, 0 }, { 68, 73, 69, 0, 0, 0 }, { 68, 73, 78, 71, 0, 0 },
{ 68, 73, 85, 0, 0, 0 }, { 68, 79, 78, 71, 0, 0 }, { 68, 79, 85, 0, 0, 0 }, { 68, 85, 0, 0, 0, 0 },
{ 68, 85, 65, 78, 0, 0 }, { 68, 85, 73, 0, 0, 0 }, { 68, 85, 78, 0, 0, 0 }, { 68, 85, 79, 0, 0, 0 },
{ 69, 0, 0, 0, 0, 0 }, { 69, 73, 0, 0, 0, 0 }, { 69, 78, 0, 0, 0, 0 }, { 69, 78, 71, 0, 0, 0 },
{ 69, 82, 0, 0, 0, 0 }, { 70, 65, 0, 0, 0, 0 }, { 70, 65, 78, 0, 0, 0 }, { 70, 65, 78, 71, 0, 0 },
{ 70, 69, 73, 0, 0, 0 }, { 70, 69, 78, 0, 0, 0 }, { 70, 69, 78, 71, 0, 0 }, { 70, 73, 65, 79, 0, 0 },
{ 70, 79, 0, 0, 0, 0 }, { 70, 79, 85, 0, 0, 0 }, { 70, 85, 0, 0, 0, 0 }, { 71, 65, 0, 0, 0, 0 },
{ 71, 65, 73, 0, 0, 0 }, { 71, 65, 78, 0, 0, 0 }, { 71, 65, 78, 71, 0, 0 }, { 71, 65, 79, 0, 0, 0 },
{ 71, 69, 0, 0, 0, 0 }, { 71, 69, 73, 0, 0, 0 }, { 71, 69, 78, 0, 0, 0 }, { 71, 69, 78, 71, 0, 0 },
{ 71, 79, 78, 71, 0, 0 }, { 71, 79, 85, 0, 0, 0 }, { 71, 85, 0, 0, 0, 0 }, { 71, 85, 65, 0, 0, 0 },
{ 71, 85, 65, 73, 0, 0 }, { 71, 85, 65, 78, 0, 0 }, { 71, 85, 65, 78, 71, 0 }, { 71, 85, 73, 0, 0, 0 },
{ 71, 85, 78, 0, 0, 0 }, { 71, 85, 79, 0, 0, 0 }, { 72, 65, 0, 0, 0, 0 }, { 72, 65, 73, 0, 0, 0 },
{ 72, 65, 78, 0, 0, 0 }, { 72, 65, 78, 71, 0, 0 }, { 72, 65, 79, 0, 0, 0 }, { 72, 69, 0, 0, 0, 0 },
{ 72, 69, 73, 0, 0, 0 }, { 72, 69, 78, 0, 0, 0 }, { 72, 69, 78, 71, 0, 0 }, { 72, 77, 0, 0, 0, 0 },
{ 72, 79, 78, 71, 0, 0 }, { 72, 79, 85, 0, 0, 0 }, { 72, 85, 0, 0, 0, 0 }, { 72, 85, 65, 0, 0, 0 },
{ 72, 85, 65, 73, 0, 0 }, { 72, 85, 65, 78, 0, 0 }, { 72, 85, 65, 78, 71, 0 }, { 72, 85, 73, 0, 0, 0 },
{ 72, 85, 78, 0, 0, 0 }, { 72, 85, 79, 0, 0, 0 }, { 74, 73, 0, 0, 0, 0 }, { 74, 73, 65, 0, 0, 0 },
{ 74, 73, 65, 78, 0, 0 }, { 74, 73, 65, 78, 71, 0 }, { 74, 73, 65, 79, 0, 0 }, { 74, 73, 69, 0, 0, 0 },
{ 74, 73, 78, 0, 0, 0 }, { 74, 73, 78, 71, 0, 0 }, { 74, 73, 79, 78, 71, 0 }, { 74, 73, 85, 0, 0, 0 },
{ 74, 85, 0, 0, 0, 0 }, { 74, 85, 65, 78, 0, 0 }, { 74, 85, 69, 0, 0, 0 }, { 74, 85, 78, 0, 0, 0 },
{ 75, 65, 0, 0, 0, 0 }, { 75, 65, 73, 0, 0, 0 }, { 75, 65, 78, 0, 0, 0 }, { 75, 65, 78, 71, 0, 0 },
{ 75, 65, 79, 0, 0, 0 }, { 75, 69, 0, 0, 0, 0 }, { 75, 69, 73, 0, 0, 0 }, { 75, 69, 78, 0, 0, 0 },
{ 75, 69, 78, 71, 0, 0 }, { 75, 79, 78, 71, 0, 0 }, { 75, 79, 85, 0, 0, 0 }, { 75, 85, 0, 0, 0, 0 },
{ 75, 85, 65, 0, 0, 0 }, { 75, 85, 65, 73, 0, 0 }, { 75, 85, 65, 78, 0, 0 }, { 75, 85, 65, 78, 71, 0 },
{ 75, 85, 73, 0, 0, 0 }, { 75, 85, 78, 0, 0, 0 }, { 75, 85, 79, 0, 0, 0 }, { 76, 65, 0, 0, 0, 0 },
{ 76, 65, 73, 0, 0, 0 }, { 76, 65, 78, 0, 0, 0 }, { 76, 65, 78, 71, 0, 0 }, { 76, 65, 79, 0, 0, 0 },
{ 76, 69, 0, 0, 0, 0 }, { 76, 69, 73, 0, 0, 0 }, { 76, 69, 78, 71, 0, 0 }, { 76, 73, 0, 0, 0, 0 },
{ 76, 73, 65, 0, 0, 0 }, { 76, 73, 65, 78, 0, 0 }, { 76, 73, 65, 78, 71, 0 }, { 76, 73, 65, 79, 0, 0 },
{ 76, 73, 69, 0, 0, 0 }, { 76, 73, 78, 0, 0, 0 }, { 76, 73, 78, 71, 0, 0 }, { 76, 73, 85, 0, 0, 0 },
{ 76, 79, 78, 71, 0, 0 }, { 76, 79, 85, 0, 0, 0 }, { 76, 85, 0, 0, 0, 0 }, { 76, 85, 65, 78, 0, 0 },
{ 76, 85, 69, 0, 0, 0 }, { 76, 85, 78, 0, 0, 0 }, { 76, 85, 79, 0, 0, 0 }, { 77, 0, 0, 0, 0, 0 },
{ 77, 65, 0, 0, 0, 0 }, { 77, 65, 73, 0, 0, 0 }, { 77, 65, 78, 0, 0, 0 }, { 77, 65, 78, 71, 0, 0 },
{ 77, 65, 79, 0, 0, 0 }, { 77, 69, 0, 0, 0, 0 }, { 77, 69, 73, 0, 0, 0 }, { 77, 69, 78, 0, 0, 0 },
{ 77, 69, 78, 71, 0, 0 }, { 77, 73, 0, 0, 0, 0 }, { 77, 73, 65, 78, 0, 0 }, { 77, 73, 65, 79, 0, 0 },
{ 77, 73, 69, 0, 0, 0 }, { 77, 73, 78, 0, 0, 0 }, { 77, 73, 78, 71, 0, 0 }, { 77, 73, 85, 0, 0, 0 },
{ 77, 79, 0, 0, 0, 0 }, { 77, 79, 85, 0, 0, 0 }, { 77, 85, 0, 0, 0, 0 }, { 78, 65, 0, 0, 0, 0 },
{ 78, 65, 73, 0, 0, 0 }, { 78, 65, 78, 0, 0, 0 }, { 78, 65, 78, 71, 0, 0 }, { 78, 65, 79, 0, 0, 0 },
{ 78, 69, 0, 0, 0, 0 }, { 78, 69, 73, 0, 0, 0 }, { 78, 69, 78, 0, 0, 0 }, { 78, 69, 78, 71, 0, 0 },
{ 78, 73, 0, 0, 0, 0 }, { 78, 73, 65, 78, 0, 0 }, { 78, 73, 65, 78, 71, 0 }, { 78, 73, 65, 79, 0, 0 },
{ 78, 73, 69, 0, 0, 0 }, { 78, 73, 78, 0, 0, 0 }, { 78, 73, 78, 71, 0, 0 }, { 78, 73, 85, 0, 0, 0 },
{ 78, 79, 78, 71, 0, 0 }, { 78, 79, 85, 0, 0, 0 }, { 78, 85, 0, 0, 0, 0 }, { 78, 85, 65, 78, 0, 0 },
{ 78, 85, 69, 0, 0, 0 }, { 78, 85, 79, 0, 0, 0 }, { 79, 0, 0, 0, 0, 0 }, { 79, 85, 0, 0, 0, 0 },
{ 80, 65, 0, 0, 0, 0 }, { 80, 65, 73, 0, 0, 0 }, { 80, 65, 78, 0, 0, 0 }, { 80, 65, 78, 71, 0, 0 },
{ 80, 65, 79, 0, 0, 0 }, { 80, 69, 73, 0, 0, 0 }, { 80, 69, 78, 0, 0, 0 }, { 80, 69, 78, 71, 0, 0 },
{ 80, 73, 0, 0, 0, 0 }, { 80, 73, 65, 78, 0, 0 }, { 80, 73, 65, 79, 0, 0 }, { 80, 73, 69, 0, 0, 0 },
{ 80, 73, 78, 0, 0, 0 }, { 80, 73, 78, 71, 0, 0 }, { 80, 79, 0, 0, 0, 0 }, { 80, 79, 85, 0, 0, 0 },
{ 80, 85, 0, 0, 0, 0 }, { 81, 73, 0, 0, 0, 0 }, { 81, 73, 65, 0, 0, 0 }, { 81, 73, 65, 78, 0, 0 },
{ 81, 73, 65, 78, 71, 0 }, { 81, 73, 65, 79, 0, 0 }, { 81, 73, 69, 0, 0, 0 }, { 81, 73, 78, 0, 0, 0 },
{ 81, 73, 78, 71, 0, 0 }, { 81, 73, 79, 78, 71, 0 }, { 81, 73, 85, 0, 0, 0 }, { 81, 85, 0, 0, 0, 0 },
{ 81, 85, 65, 78, 0, 0 }, { 81, 85, 69, 0, 0, 0 }, { 81, 85, 78, 0, 0, 0 }, { 82, 65, 78, 0, 0, 0 },
{ 82, 65, 78, 71, 0, 0 }, { 82, 65, 79, 0, 0, 0 }, { 82, 69, 0, 0, 0, 0 }, { 82, 69, 78, 0, 0, 0 },
{ 82, 69, 78, 71, 0, 0 }, { 82, 73, 0, 0, 0, 0 }, { 82, 79, 78, 71, 0, 0 }, { 82, 79, 85, 0, 0, 0 },
{ 82, 85, 0, 0, 0, 0 }, { 82, 85, 65, 78, 0, 0 }, { 82, 85, 73, 0, 0, 0 }, { 82, 85, 78, 0, 0, 0 },
{ 82, 85, 79, 0, 0, 0 }, { 83, 65, 0, 0, 0, 0 }, { 83, 65, 73, 0, 0, 0 }, { 83, 65, 78, 0, 0, 0 },
{ 83, 65, 78, 71, 0, 0 }, { 83, 65, 79, 0, 0, 0 }, { 83, 69, 0, 0, 0, 0 }, { 83, 69, 78, 0, 0, 0 },
{ 83, 69, 78, 71, 0, 0 }, { 83, 72, 65, 0, 0, 0 }, { 83, 72, 65, 73, 0, 0 }, { 83, 72, 65, 78, 0, 0 },
{ 83, 72, 65, 78, 71, 0 }, { 83, 72, 65, 79, 0, 0 }, { 83, 72, 69, 0, 0, 0 }, { 83, 72, 69, 78, 0, 0 },
{ 83, 72, 69, 78, 71, 0 }, { 83, 72, 73, 0, 0, 0 }, { 83, 72, 79, 85, 0, 0 }, { 83, 72, 85, 0, 0, 0 },
{ 83, 72, 85, 65, 0, 0 }, { 83, 72, 85, 65, 73, 0 }, { 83, 72, 85, 65, 78, 0 }, { 83, 72, 85, 65, 78, 71 },
{ 83, 72, 85, 73, 0, 0 }, { 83, 72, 85, 78, 0, 0 }, { 83, 72, 85, 79, 0, 0 }, { 83, 73, 0, 0, 0, 0 },
{ 83, 79, 78, 71, 0, 0 }, { 83, 79, 85, 0, 0, 0 }, { 83, 85, 0, 0, 0, 0 }, { 83, 85, 65, 78, 0, 0 },
{ 83, 85, 73, 0, 0, 0 }, { 83, 85, 78, 0, 0, 0 }, { 83, 85, 79, 0, 0, 0 }, { 84, 65, 0, 0, 0, 0 },
{ 84, 65, 73, 0, 0, 0 }, { 84, 65, 78, 0, 0, 0 }, { 84, 65, 78, 71, 0, 0 }, { 84, 65, 79, 0, 0, 0 },
{ 84, 69, 0, 0, 0, 0 }, { 84, 69, 78, 71, 0, 0 }, { 84, 73, 0, 0, 0, 0 }, { 84, 73, 65, 78, 0, 0 },
{ 84, 73, 65, 79, 0, 0 }, { 84, 73, 69, 0, 0, 0 }, { 84, 73, 78, 71, 0, 0 }, { 84, 79, 78, 71, 0, 0 },
{ 84, 79, 85, 0, 0, 0 }, { 84, 85, 0, 0, 0, 0 }, { 84, 85, 65, 78, 0, 0 }, { 84, 85, 73, 0, 0, 0 },
{ 84, 85, 78, 0, 0, 0 }, { 84, 85, 79, 0, 0, 0 }, { 87, 65, 0, 0, 0, 0 }, { 87, 65, 73, 0, 0, 0 },
{ 87, 65, 78, 0, 0, 0 }, { 87, 65, 78, 71, 0, 0 }, { 87, 69, 73, 0, 0, 0 }, { 87, 69, 78, 0, 0, 0 },
{ 87, 69, 78, 71, 0, 0 }, { 87, 79, 0, 0, 0, 0 }, { 87, 85, 0, 0, 0, 0 }, { 88, 73, 0, 0, 0, 0 },
{ 88, 73, 65, 0, 0, 0 }, { 88, 73, 65, 78, 0, 0 }, { 88, 73, 65, 78, 71, 0 }, { 88, 73, 65, 79, 0, 0 },
{ 88, 73, 69, 0, 0, 0 }, { 88, 73, 78, 0, 0, 0 }, { 88, 73, 78, 71, 0, 0 }, { 88, 73, 79, 78, 71, 0 },
{ 88, 73, 85, 0, 0, 0 }, { 88, 85, 0, 0, 0, 0 }, { 88, 85, 65, 78, 0, 0 }, { 88, 85, 69, 0, 0, 0 },
{ 88, 85, 78, 0, 0, 0 }, { 89, 65, 0, 0, 0, 0 }, { 89, 65, 78, 0, 0, 0 }, { 89, 65, 78, 71, 0, 0 },
{ 89, 65, 79, 0, 0, 0 }, { 89, 69, 0, 0, 0, 0 }, { 89, 73, 0, 0, 0, 0 }, { 89, 73, 78, 0, 0, 0 },
{ 89, 73, 78, 71, 0, 0 }, { 89, 79, 0, 0, 0, 0 }, { 89, 79, 78, 71, 0, 0 }, { 89, 79, 85, 0, 0, 0 },
{ 89, 85, 0, 0, 0, 0 }, { 89, 85, 65, 78, 0, 0 }, { 89, 85, 69, 0, 0, 0 }, { 89, 85, 78, 0, 0, 0 },
{ 90, 65, 0, 0, 0, 0 }, { 90, 65, 73, 0, 0, 0 }, { 90, 65, 78, 0, 0, 0 }, { 90, 65, 78, 71, 0, 0 },
{ 90, 65, 79, 0, 0, 0 }, { 90, 69, 0, 0, 0, 0 }, { 90, 69, 73, 0, 0, 0 }, { 90, 69, 78, 0, 0, 0 },
{ 90, 69, 78, 71, 0, 0 }, { 90, 72, 65, 0, 0, 0 }, { 90, 72, 65, 73, 0, 0 }, { 90, 72, 65, 78, 0, 0 },
{ 90, 72, 65, 78, 71, 0 }, { 90, 72, 65, 79, 0, 0 }, { 90, 72, 69, 0, 0, 0 }, { 90, 72, 69, 78, 0, 0 },
{ 90, 72, 69, 78, 71, 0 }, { 90, 72, 73, 0, 0, 0 }, { 90, 72, 79, 78, 71, 0 }, { 90, 72, 79, 85, 0, 0 },
{ 90, 72, 85, 0, 0, 0 }, { 90, 72, 85, 65, 0, 0 }, { 90, 72, 85, 65, 73, 0 }, { 90, 72, 85, 65, 78, 0 },
{ 90, 72, 85, 65, 78, 71 }, { 90, 72, 85, 73, 0, 0 }, { 90, 72, 85, 78, 0, 0 }, { 90, 72, 85, 79, 0, 0 },
{ 90, 73, 0, 0, 0, 0 }, { 90, 79, 78, 71, 0, 0 }, { 90, 79, 85, 0, 0, 0 }, { 90, 85, 0, 0, 0, 0 },
{ 90, 85, 65, 78, 0, 0 }, { 90, 85, 73, 0, 0, 0 }, { 90, 85, 78, 0, 0, 0 }, { 90, 85, 79, 0, 0, 0 } };
// Java collator of chinese language
private static final Collator sHanziCollator = Collator.getInstance(Locale.CHINA);
/**
* Convert the input character to chinese Pinyin. If the input character is
* not chinese, return itself back as string directly.
*
* @param hanzi
* chinese character
* @return Upper-case string of chinese Pinyin
*/
public static String getPinyin(char hanzi) {
String han = Character.toString(hanzi);
if (hanzi < HANZI_UNICODE_MIN || hanzi > HANZI_UNICODE_MAX) {
return han;
}
if (sHanziCollator.compare(han, FIRST_HANZI) < 0 || sHanziCollator.compare(han, LAST_HANZI) > 0) {
return han;
}
if (sHanziCollator.compare(han, Character.toString(HANZI_UNICODE[HANZI_UNICODE.length - 1])) >= 0) {
System.out.println("last");
return getPinyinStr(HANZI_UNICODE.length - 1);
}
int begin = 0;
int end = HANZI_UNICODE.length - 1;
int mid = 0;
int result = 0;
while (end - begin > 1) {
mid = (begin + end) / 2;
result = sHanziCollator.compare(han, Character.toString(HANZI_UNICODE[mid]));
if (result < 0) {
end = mid;
continue;
} else if (result > 0) {
begin = mid;
continue;
} else {
return getPinyinStr(mid);
}
}
if (result == 0) {
return null;
} else if (result < 0) {
return getPinyinStr(mid - 1);
} else {
return getPinyinStr(mid);
}
}
private static String getPinyinStr(int index) {
if (index < 0 || index >= PINYINS.length) {
return null;
}
StringBuilder pinyin = new StringBuilder();
int length = PINYINS[index].length;
for (int i = 0; i < length && PINYINS[index][i] != 0; ++i) {
pinyin.append((char) PINYINS[index][i]);
}
return pinyin.toString();
}
}
欢迎批评指导!谢谢!