1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.internal.util; 18 19 import android.text.TextUtils; 20 import android.util.Log; 21 22 import java.text.Collator; 23 import java.util.ArrayList; 24 import java.util.Locale; 25 26 /** 27 * An object to convert Chinese character to its corresponding pinyin string. 28 * For characters with multiple possible pinyin string, only one is selected 29 * according to collator. Polyphone is not supported in this implementation. 30 * This class is implemented to achieve the best runtime performance and minimum 31 * runtime resources with tolerable sacrifice of accuracy. This implementation 32 * highly depends on zh_CN ICU collation data and must be always synchronized with 33 * ICU. 34 */ 35 public class HanziToPinyin { 36 private static final String TAG = "HanziToPinyin"; 37 38 private static final char[] UNIHANS = { 39 '\u5416', '\u54ce', '\u5b89', '\u80ae', '\u51f9', '\u516b', '\u63b0', '\u6273', 40 '\u90a6', '\u52f9', '\u9642', '\u5954', '\u4f3b', '\u7680', '\u782d', '\u706c', 41 '\u618b', '\u6c43', '\u51ab', '\u7676', '\u5cec', '\u5693', '\u5072', '\u53c2', 42 '\u4ed3', '\u64a1', '\u518a', '\u5d7e', '\u564c', '\u6260', '\u62c6', '\u8fbf', 43 '\u4f25', '\u6284', '\u8f66', '\u62bb', '\u9637', '\u5403', '\u5145', '\u62bd', 44 '\u51fa', '\u640b', '\u5ddb', '\u5205', '\u5439', '\u65fe', '\u8e14', '\u5472', 45 '\u4ece', '\u51d1', '\u7c97', '\u6c46', '\u5d14', '\u90a8', '\u6413', '\u5491', 46 '\u5446', '\u4e39', '\u5f53', '\u5200', '\u6074', '\u6265', '\u706f', '\u4efe', 47 '\u55f2', '\u6541', '\u5201', '\u7239', '\u4e01', '\u4e1f', '\u4e1c', '\u543a', 48 '\u5262', '\u8011', '\u5796', '\u5428', '\u591a', '\u59b8', '\u5940', '\u97a5', 49 '\u800c', '\u53d1', '\u5e06', '\u531a', '\u98de', '\u5206', '\u4e30', '\u8985', 50 '\u4ecf', '\u57ba', '\u7d11', '\u592b', '\u7324', '\u65ee', '\u4f85', '\u5e72', 51 '\u5188', '\u768b', '\u6208', '\u7ed9', '\u6839', '\u63ef', '\u55bc', '\u55f0', 52 '\u5de5', '\u52fe', '\u4f30', '\u9e39', '\u4e56', '\u5173', '\u5149', '\u5f52', 53 '\u4e28', '\u8b34', '\u5459', '\u598e', '\u548d', '\u4f44', '\u592f', '\u8320', 54 '\u8bc3', '\u9ed2', '\u62eb', '\u4ea8', '\u53ff', '\u9f41', '\u4e4e', '\u82b1', 55 '\u6000', '\u6b22', '\u5ddf', '\u7070', '\u660f', '\u5419', '\u4e0c', '\u52a0', 56 '\u620b', '\u6c5f', '\u827d', '\u9636', '\u5dfe', '\u5755', '\u5182', '\u4e29', 57 '\u51e5', '\u59e2', '\u5658', '\u519b', '\u5494', '\u5f00', '\u938e', '\u5ffc', 58 '\u5c3b', '\u533c', '\u808e', '\u52a5', '\u7a7a', '\u62a0', '\u625d', '\u5938', 59 '\u84af', '\u5bbd', '\u5321', '\u4e8f', '\u5764', '\u6269', '\u62c9', '\u4f86', 60 '\u5170', '\u5577', '\u635e', '\u4ec2', '\u96f7', '\u8137', '\u68f1', '\u695e', 61 '\u550e', '\u4fe9', '\u5afe', '\u826f', '\u8e7d', '\u57d3', '\u53b8', '\u62ce', 62 '\u6e9c', '\u9f99', '\u5a04', '\u565c', '\u5b6a', '\u62a1', '\u9831', '\u5988', 63 '\u57cb', '\u989f', '\u7264', '\u732b', '\u5445', '\u95e8', '\u6c13', '\u54aa', 64 '\u5b80', '\u55b5', '\u4e5c', '\u6c11', '\u540d', '\u8c2c', '\u6478', '\u725f', 65 '\u6bcd', '\u62cf', '\u8149', '\u56e1', '\u56d4', '\u5b6c', '\u8bb7', '\u5a1e', 66 '\u5ae9', '\u80fd', '\u92b0', '\u62c8', '\u5a18', '\u9e1f', '\u634f', '\u56dc', 67 '\u5b81', '\u599e', '\u519c', '\u7fba', '\u5974', '\u597b', '\u9ec1', '\u90cd', 68 '\u5662', '\u8bb4', '\u5991', '\u62cd', '\u7705', '\u6c78', '\u629b', '\u5478', 69 '\u55b7', '\u5309', '\u4e76', '\u7247', '\u527d', '\u6c15', '\u59d8', '\u4e52', 70 '\u948b', '\u5256', '\u4ec6', '\u4e03', '\u6390', '\u5343', '\u545b', '\u6084', 71 '\u5207', '\u4eb2', '\u9751', '\u5b86', '\u74d7', '\u533a', '\u5cd1', '\u7094', 72 '\u590b', '\u5465', '\u7a63', '\u835b', '\u60f9', '\u4eba', '\u6254', '\u65e5', 73 '\u620e', '\u53b9', '\u909a', '\u5827', '\u6875', '\u95f0', '\u633c', '\u4ee8', 74 '\u6be2', '\u4e09', '\u6852', '\u63bb', '\u8272', '\u68ee', '\u50e7', '\u6740', 75 '\u7b5b', '\u5c71', '\u4f24', '\u5f30', '\u5962', '\u7533', '\u5347', '\u5c38', 76 '\u53ce', '\u4e66', '\u5237', '\u8870', '\u95e9', '\u53cc', '\u8c01', '\u542e', 77 '\u8bf4', '\u53b6', '\u5fea', '\u51c1', '\u82cf', '\u72fb', '\u590a', '\u5b59', 78 '\u5506', '\u4ed6', '\u5b61', '\u574d', '\u6c64', '\u5932', '\u5fd1', '\u81af', 79 '\u5254', '\u5929', '\u65eb', '\u6017', '\u5385', '\u70b5', '\u5077', '\u51f8', 80 '\u6e4d', '\u63a8', '\u541e', '\u8bac', '\u52b8', '\u6b6a', '\u5f2f', '\u5c23', 81 '\u5371', '\u6637', '\u7fc1', '\u631d', '\u4e4c', '\u5915', '\u5477', '\u4ed9', 82 '\u4e61', '\u7071', '\u4e9b', '\u5fc3', '\u5174', '\u51f6', '\u4f11', '\u620c', 83 '\u5405', '\u75b6', '\u7025', '\u4e2b', '\u54bd', '\u592e', '\u5e7a', '\u503b', 84 '\u4e00', '\u4e5a', '\u5e94', '\u5537', '\u4f63', '\u4f18', '\u7ea1', '\u56e6', 85 '\u66f0', '\u8480', '\u5e00', '\u707d', '\u5142', '\u7242', '\u50ae', '\u556b', 86 '\u9c61', '\u600e', '\u66fd', '\u5412', '\u635a', '\u6cbe', '\u5f20', '\u4f4b', 87 '\u8707', '\u8d1e', '\u9eee', '\u4e4b', '\u4e2d', '\u5dde', '\u6731', '\u6293', 88 '\u62fd', '\u4e13', '\u5986', '\u96b9', '\u5b92', '\u5353', '\u4ed4', '\u5b97', 89 '\u90b9', '\u79df', '\u5297', '\u55fa', '\u5c0a', '\u6628', 90 }; 91 private final static byte[][] PINYINS = { 92 {65, 00, 00, 00, 00, 00, }, {65, 73, 00, 00, 00, 00, }, 93 {65, 78, 00, 00, 00, 00, }, {65, 78, 71, 00, 00, 00, }, 94 {65, 79, 00, 00, 00, 00, }, {66, 65, 00, 00, 00, 00, }, 95 {66, 65, 73, 00, 00, 00, }, {66, 65, 78, 00, 00, 00, }, 96 {66, 65, 78, 71, 00, 00, }, {66, 65, 79, 00, 00, 00, }, 97 {66, 69, 73, 00, 00, 00, }, {66, 69, 78, 00, 00, 00, }, 98 {66, 69, 78, 71, 00, 00, }, {66, 73, 00, 00, 00, 00, }, 99 {66, 73, 65, 78, 00, 00, }, {66, 73, 65, 79, 00, 00, }, 100 {66, 73, 69, 00, 00, 00, }, {66, 73, 78, 00, 00, 00, }, 101 {66, 73, 78, 71, 00, 00, }, {66, 79, 00, 00, 00, 00, }, 102 {66, 85, 00, 00, 00, 00, }, {67, 65, 00, 00, 00, 00, }, 103 {67, 65, 73, 00, 00, 00, }, {67, 65, 78, 00, 00, 00, }, 104 {67, 65, 78, 71, 00, 00, }, {67, 65, 79, 00, 00, 00, }, 105 {67, 69, 00, 00, 00, 00, }, {67, 69, 78, 00, 00, 00, }, 106 {67, 69, 78, 71, 00, 00, }, {67, 72, 65, 00, 00, 00, }, 107 {67, 72, 65, 73, 00, 00, }, {67, 72, 65, 78, 00, 00, }, 108 {67, 72, 65, 78, 71, 00, }, {67, 72, 65, 79, 00, 00, }, 109 {67, 72, 69, 00, 00, 00, }, {67, 72, 69, 78, 00, 00, }, 110 {67, 72, 69, 78, 71, 00, }, {67, 72, 73, 00, 00, 00, }, 111 {67, 72, 79, 78, 71, 00, }, {67, 72, 79, 85, 00, 00, }, 112 {67, 72, 85, 00, 00, 00, }, {67, 72, 85, 65, 73, 00, }, 113 {67, 72, 85, 65, 78, 00, }, {67, 72, 85, 65, 78, 71, }, 114 {67, 72, 85, 73, 00, 00, }, {67, 72, 85, 78, 00, 00, }, 115 {67, 72, 85, 79, 00, 00, }, {67, 73, 00, 00, 00, 00, }, 116 {67, 79, 78, 71, 00, 00, }, {67, 79, 85, 00, 00, 00, }, 117 {67, 85, 00, 00, 00, 00, }, {67, 85, 65, 78, 00, 00, }, 118 {67, 85, 73, 00, 00, 00, }, {67, 85, 78, 00, 00, 00, }, 119 {67, 85, 79, 00, 00, 00, }, {68, 65, 00, 00, 00, 00, }, 120 {68, 65, 73, 00, 00, 00, }, {68, 65, 78, 00, 00, 00, }, 121 {68, 65, 78, 71, 00, 00, }, {68, 65, 79, 00, 00, 00, }, 122 {68, 69, 00, 00, 00, 00, }, {68, 69, 78, 00, 00, 00, }, 123 {68, 69, 78, 71, 00, 00, }, {68, 73, 00, 00, 00, 00, }, 124 {68, 73, 65, 00, 00, 00, }, {68, 73, 65, 78, 00, 00, }, 125 {68, 73, 65, 79, 00, 00, }, {68, 73, 69, 00, 00, 00, }, 126 {68, 73, 78, 71, 00, 00, }, {68, 73, 85, 00, 00, 00, }, 127 {68, 79, 78, 71, 00, 00, }, {68, 79, 85, 00, 00, 00, }, 128 {68, 85, 00, 00, 00, 00, }, {68, 85, 65, 78, 00, 00, }, 129 {68, 85, 73, 00, 00, 00, }, {68, 85, 78, 00, 00, 00, }, 130 {68, 85, 79, 00, 00, 00, }, {69, 00, 00, 00, 00, 00, }, 131 {69, 78, 00, 00, 00, 00, }, {69, 78, 71, 00, 00, 00, }, 132 {69, 82, 00, 00, 00, 00, }, {70, 65, 00, 00, 00, 00, }, 133 {70, 65, 78, 00, 00, 00, }, {70, 65, 78, 71, 00, 00, }, 134 {70, 69, 73, 00, 00, 00, }, {70, 69, 78, 00, 00, 00, }, 135 {70, 69, 78, 71, 00, 00, }, {70, 73, 65, 79, 00, 00, }, 136 {70, 79, 00, 00, 00, 00, }, {70, 85, 00, 00, 00, 00, }, 137 {70, 79, 85, 00, 00, 00, }, {70, 85, 00, 00, 00, 00, }, 138 {71, 85, 73, 00, 00, 00, }, {71, 65, 00, 00, 00, 00, }, 139 {71, 65, 73, 00, 00, 00, }, {71, 65, 78, 00, 00, 00, }, 140 {71, 65, 78, 71, 00, 00, }, {71, 65, 79, 00, 00, 00, }, 141 {71, 69, 00, 00, 00, 00, }, {71, 69, 73, 00, 00, 00, }, 142 {71, 69, 78, 00, 00, 00, }, {71, 69, 78, 71, 00, 00, }, 143 {74, 73, 69, 00, 00, 00, }, {71, 69, 00, 00, 00, 00, }, 144 {71, 79, 78, 71, 00, 00, }, {71, 79, 85, 00, 00, 00, }, 145 {71, 85, 00, 00, 00, 00, }, {71, 85, 65, 00, 00, 00, }, 146 {71, 85, 65, 73, 00, 00, }, {71, 85, 65, 78, 00, 00, }, 147 {71, 85, 65, 78, 71, 00, }, {71, 85, 73, 00, 00, 00, }, 148 {71, 85, 78, 00, 00, 00, }, {71, 85, 65, 78, 00, 00, }, 149 {71, 85, 79, 00, 00, 00, }, {72, 65, 00, 00, 00, 00, }, 150 {72, 65, 73, 00, 00, 00, }, {72, 65, 78, 00, 00, 00, }, 151 {72, 65, 78, 71, 00, 00, }, {72, 65, 79, 00, 00, 00, }, 152 {72, 69, 00, 00, 00, 00, }, {72, 69, 73, 00, 00, 00, }, 153 {72, 69, 78, 00, 00, 00, }, {72, 69, 78, 71, 00, 00, }, 154 {72, 79, 78, 71, 00, 00, }, {72, 79, 85, 00, 00, 00, }, 155 {72, 85, 00, 00, 00, 00, }, {72, 85, 65, 00, 00, 00, }, 156 {72, 85, 65, 73, 00, 00, }, {72, 85, 65, 78, 00, 00, }, 157 {72, 85, 65, 78, 71, 00, }, {72, 85, 73, 00, 00, 00, }, 158 {72, 85, 78, 00, 00, 00, }, {72, 85, 79, 00, 00, 00, }, 159 {74, 73, 00, 00, 00, 00, }, {74, 73, 65, 00, 00, 00, }, 160 {74, 73, 65, 78, 00, 00, }, {74, 73, 65, 78, 71, 00, }, 161 {74, 73, 65, 79, 00, 00, }, {74, 73, 69, 00, 00, 00, }, 162 {74, 73, 78, 00, 00, 00, }, {74, 73, 78, 71, 00, 00, }, 163 {74, 73, 79, 78, 71, 00, }, {74, 73, 85, 00, 00, 00, }, 164 {74, 85, 00, 00, 00, 00, }, {74, 85, 65, 78, 00, 00, }, 165 {74, 85, 69, 00, 00, 00, }, {74, 85, 78, 00, 00, 00, }, 166 {75, 65, 00, 00, 00, 00, }, {75, 65, 73, 00, 00, 00, }, 167 {75, 65, 78, 00, 00, 00, }, {75, 65, 78, 71, 00, 00, }, 168 {75, 65, 79, 00, 00, 00, }, {75, 69, 00, 00, 00, 00, }, 169 {75, 69, 78, 00, 00, 00, }, {75, 69, 78, 71, 00, 00, }, 170 {75, 79, 78, 71, 00, 00, }, {75, 79, 85, 00, 00, 00, }, 171 {75, 85, 00, 00, 00, 00, }, {75, 85, 65, 00, 00, 00, }, 172 {75, 85, 65, 73, 00, 00, }, {75, 85, 65, 78, 00, 00, }, 173 {75, 85, 65, 78, 71, 00, }, {75, 85, 73, 00, 00, 00, }, 174 {75, 85, 78, 00, 00, 00, }, {75, 85, 79, 00, 00, 00, }, 175 {76, 65, 00, 00, 00, 00, }, {76, 65, 73, 00, 00, 00, }, 176 {76, 65, 78, 00, 00, 00, }, {76, 65, 78, 71, 00, 00, }, 177 {76, 65, 79, 00, 00, 00, }, {76, 69, 00, 00, 00, 00, }, 178 {76, 69, 73, 00, 00, 00, }, {76, 73, 00, 00, 00, 00, }, 179 {76, 73, 78, 71, 00, 00, }, {76, 69, 78, 71, 00, 00, }, 180 {76, 73, 00, 00, 00, 00, }, {76, 73, 65, 00, 00, 00, }, 181 {76, 73, 65, 78, 00, 00, }, {76, 73, 65, 78, 71, 00, }, 182 {76, 73, 65, 79, 00, 00, }, {76, 73, 69, 00, 00, 00, }, 183 {76, 73, 78, 00, 00, 00, }, {76, 73, 78, 71, 00, 00, }, 184 {76, 73, 85, 00, 00, 00, }, {76, 79, 78, 71, 00, 00, }, 185 {76, 79, 85, 00, 00, 00, }, {76, 85, 00, 00, 00, 00, }, 186 {76, 85, 65, 78, 00, 00, }, {76, 85, 78, 00, 00, 00, }, 187 {76, 85, 79, 00, 00, 00, }, {77, 65, 00, 00, 00, 00, }, 188 {77, 65, 73, 00, 00, 00, }, {77, 65, 78, 00, 00, 00, }, 189 {77, 65, 78, 71, 00, 00, }, {77, 65, 79, 00, 00, 00, }, 190 {77, 69, 73, 00, 00, 00, }, {77, 69, 78, 00, 00, 00, }, 191 {77, 69, 78, 71, 00, 00, }, {77, 73, 00, 00, 00, 00, }, 192 {77, 73, 65, 78, 00, 00, }, {77, 73, 65, 79, 00, 00, }, 193 {77, 73, 69, 00, 00, 00, }, {77, 73, 78, 00, 00, 00, }, 194 {77, 73, 78, 71, 00, 00, }, {77, 73, 85, 00, 00, 00, }, 195 {77, 79, 00, 00, 00, 00, }, {77, 79, 85, 00, 00, 00, }, 196 {77, 85, 00, 00, 00, 00, }, {78, 65, 00, 00, 00, 00, }, 197 {78, 65, 73, 00, 00, 00, }, {78, 65, 78, 00, 00, 00, }, 198 {78, 65, 78, 71, 00, 00, }, {78, 65, 79, 00, 00, 00, }, 199 {78, 69, 00, 00, 00, 00, }, {78, 69, 73, 00, 00, 00, }, 200 {78, 69, 78, 00, 00, 00, }, {78, 69, 78, 71, 00, 00, }, 201 {78, 73, 00, 00, 00, 00, }, {78, 73, 65, 78, 00, 00, }, 202 {78, 73, 65, 78, 71, 00, }, {78, 73, 65, 79, 00, 00, }, 203 {78, 73, 69, 00, 00, 00, }, {78, 73, 78, 00, 00, 00, }, 204 {78, 73, 78, 71, 00, 00, }, {78, 73, 85, 00, 00, 00, }, 205 {78, 79, 78, 71, 00, 00, }, {78, 79, 85, 00, 00, 00, }, 206 {78, 85, 00, 00, 00, 00, }, {78, 85, 65, 78, 00, 00, }, 207 {78, 85, 78, 00, 00, 00, }, {78, 85, 79, 00, 00, 00, }, 208 {79, 00, 00, 00, 00, 00, }, {79, 85, 00, 00, 00, 00, }, 209 {80, 65, 00, 00, 00, 00, }, {80, 65, 73, 00, 00, 00, }, 210 {80, 65, 78, 00, 00, 00, }, {80, 65, 78, 71, 00, 00, }, 211 {80, 65, 79, 00, 00, 00, }, {80, 69, 73, 00, 00, 00, }, 212 {80, 69, 78, 00, 00, 00, }, {80, 69, 78, 71, 00, 00, }, 213 {80, 73, 00, 00, 00, 00, }, {80, 73, 65, 78, 00, 00, }, 214 {80, 73, 65, 79, 00, 00, }, {80, 73, 69, 00, 00, 00, }, 215 {80, 73, 78, 00, 00, 00, }, {80, 73, 78, 71, 00, 00, }, 216 {80, 79, 00, 00, 00, 00, }, {80, 79, 85, 00, 00, 00, }, 217 {80, 85, 00, 00, 00, 00, }, {81, 73, 00, 00, 00, 00, }, 218 {81, 73, 65, 00, 00, 00, }, {81, 73, 65, 78, 00, 00, }, 219 {81, 73, 65, 78, 71, 00, }, {81, 73, 65, 79, 00, 00, }, 220 {81, 73, 69, 00, 00, 00, }, {81, 73, 78, 00, 00, 00, }, 221 {81, 73, 78, 71, 00, 00, }, {81, 73, 79, 78, 71, 00, }, 222 {81, 73, 85, 00, 00, 00, }, {81, 85, 00, 00, 00, 00, }, 223 {81, 85, 65, 78, 00, 00, }, {81, 85, 69, 00, 00, 00, }, 224 {81, 85, 78, 00, 00, 00, }, {82, 65, 78, 00, 00, 00, }, 225 {82, 65, 78, 71, 00, 00, }, {82, 65, 79, 00, 00, 00, }, 226 {82, 69, 00, 00, 00, 00, }, {82, 69, 78, 00, 00, 00, }, 227 {82, 69, 78, 71, 00, 00, }, {82, 73, 00, 00, 00, 00, }, 228 {82, 79, 78, 71, 00, 00, }, {82, 79, 85, 00, 00, 00, }, 229 {82, 85, 00, 00, 00, 00, }, {82, 85, 65, 78, 00, 00, }, 230 {82, 85, 73, 00, 00, 00, }, {82, 85, 78, 00, 00, 00, }, 231 {82, 85, 79, 00, 00, 00, }, {83, 65, 00, 00, 00, 00, }, 232 {83, 65, 73, 00, 00, 00, }, {83, 65, 78, 00, 00, 00, }, 233 {83, 65, 78, 71, 00, 00, }, {83, 65, 79, 00, 00, 00, }, 234 {83, 69, 00, 00, 00, 00, }, {83, 69, 78, 00, 00, 00, }, 235 {83, 69, 78, 71, 00, 00, }, {83, 72, 65, 00, 00, 00, }, 236 {83, 72, 65, 73, 00, 00, }, {83, 72, 65, 78, 00, 00, }, 237 {83, 72, 65, 78, 71, 00, }, {83, 72, 65, 79, 00, 00, }, 238 {83, 72, 69, 00, 00, 00, }, {83, 72, 69, 78, 00, 00, }, 239 {83, 72, 69, 78, 71, 00, }, {83, 72, 73, 00, 00, 00, }, 240 {83, 72, 79, 85, 00, 00, }, {83, 72, 85, 00, 00, 00, }, 241 {83, 72, 85, 65, 00, 00, }, {83, 72, 85, 65, 73, 00, }, 242 {83, 72, 85, 65, 78, 00, }, {83, 72, 85, 65, 78, 71, }, 243 {83, 72, 85, 73, 00, 00, }, {83, 72, 85, 78, 00, 00, }, 244 {83, 72, 85, 79, 00, 00, }, {83, 73, 00, 00, 00, 00, }, 245 {83, 79, 78, 71, 00, 00, }, {83, 79, 85, 00, 00, 00, }, 246 {83, 85, 00, 00, 00, 00, }, {83, 85, 65, 78, 00, 00, }, 247 {83, 85, 73, 00, 00, 00, }, {83, 85, 78, 00, 00, 00, }, 248 {83, 85, 79, 00, 00, 00, }, {84, 65, 00, 00, 00, 00, }, 249 {84, 65, 73, 00, 00, 00, }, {84, 65, 78, 00, 00, 00, }, 250 {84, 65, 78, 71, 00, 00, }, {84, 65, 79, 00, 00, 00, }, 251 {84, 69, 00, 00, 00, 00, }, {84, 69, 78, 71, 00, 00, }, 252 {84, 73, 00, 00, 00, 00, }, {84, 73, 65, 78, 00, 00, }, 253 {84, 73, 65, 79, 00, 00, }, {84, 73, 69, 00, 00, 00, }, 254 {84, 73, 78, 71, 00, 00, }, {84, 79, 78, 71, 00, 00, }, 255 {84, 79, 85, 00, 00, 00, }, {84, 85, 00, 00, 00, 00, }, 256 {84, 85, 65, 78, 00, 00, }, {84, 85, 73, 00, 00, 00, }, 257 {84, 85, 78, 00, 00, 00, }, {84, 85, 79, 00, 00, 00, }, 258 {87, 65, 00, 00, 00, 00, }, {87, 65, 73, 00, 00, 00, }, 259 {87, 65, 78, 00, 00, 00, }, {87, 65, 78, 71, 00, 00, }, 260 {87, 69, 73, 00, 00, 00, }, {87, 69, 78, 00, 00, 00, }, 261 {87, 69, 78, 71, 00, 00, }, {87, 79, 00, 00, 00, 00, }, 262 {87, 85, 00, 00, 00, 00, }, {88, 73, 00, 00, 00, 00, }, 263 {88, 73, 65, 00, 00, 00, }, {88, 73, 65, 78, 00, 00, }, 264 {88, 73, 65, 78, 71, 00, }, {88, 73, 65, 79, 00, 00, }, 265 {88, 73, 69, 00, 00, 00, }, {88, 73, 78, 00, 00, 00, }, 266 {88, 73, 78, 71, 00, 00, }, {88, 73, 79, 78, 71, 00, }, 267 {88, 73, 85, 00, 00, 00, }, {88, 85, 00, 00, 00, 00, }, 268 {88, 85, 65, 78, 00, 00, }, {88, 85, 69, 00, 00, 00, }, 269 {88, 85, 78, 00, 00, 00, }, {89, 65, 00, 00, 00, 00, }, 270 {89, 65, 78, 00, 00, 00, }, {89, 65, 78, 71, 00, 00, }, 271 {89, 65, 79, 00, 00, 00, }, {89, 69, 00, 00, 00, 00, }, 272 {89, 73, 00, 00, 00, 00, }, {89, 73, 78, 00, 00, 00, }, 273 {89, 73, 78, 71, 00, 00, }, {89, 79, 00, 00, 00, 00, }, 274 {89, 79, 78, 71, 00, 00, }, {89, 79, 85, 00, 00, 00, }, 275 {89, 85, 00, 00, 00, 00, }, {89, 85, 65, 78, 00, 00, }, 276 {89, 85, 69, 00, 00, 00, }, {89, 85, 78, 00, 00, 00, }, 277 {90, 65, 00, 00, 00, 00, }, {90, 65, 73, 00, 00, 00, }, 278 {90, 65, 78, 00, 00, 00, }, {90, 65, 78, 71, 00, 00, }, 279 {90, 65, 79, 00, 00, 00, }, {90, 69, 00, 00, 00, 00, }, 280 {90, 69, 73, 00, 00, 00, }, {90, 69, 78, 00, 00, 00, }, 281 {90, 69, 78, 71, 00, 00, }, {90, 72, 65, 00, 00, 00, }, 282 {90, 72, 65, 73, 00, 00, }, {90, 72, 65, 78, 00, 00, }, 283 {90, 72, 65, 78, 71, 00, }, {90, 72, 65, 79, 00, 00, }, 284 {90, 72, 69, 00, 00, 00, }, {90, 72, 69, 78, 00, 00, }, 285 {90, 72, 69, 78, 71, 00, }, {90, 72, 73, 00, 00, 00, }, 286 {90, 72, 79, 78, 71, 00, }, {90, 72, 79, 85, 00, 00, }, 287 {90, 72, 85, 00, 00, 00, }, {90, 72, 85, 65, 00, 00, }, 288 {90, 72, 85, 65, 73, 00, }, {90, 72, 85, 65, 78, 00, }, 289 {90, 72, 85, 65, 78, 71, }, {90, 72, 85, 73, 00, 00, }, 290 {90, 72, 85, 78, 00, 00, }, {90, 72, 85, 79, 00, 00, }, 291 {90, 73, 00, 00, 00, 00, }, {90, 79, 78, 71, 00, 00, }, 292 {90, 79, 85, 00, 00, 00, }, {90, 85, 00, 00, 00, 00, }, 293 {90, 85, 65, 78, 00, 00, }, {90, 85, 73, 00, 00, 00, }, 294 {90, 85, 78, 00, 00, 00, }, {90, 85, 79, 00, 00, 00, }, 295 296 }; 297 298 /** First and last Chinese character with known Pinyin according to zh collation */ 299 private static final String FIRST_PINYIN_UNIHAN = "\u5416"; 300 private static final String LAST_PINYIN_UNIHAN = "\u5497"; 301 /** The first Chinese character in Unicode block */ 302 private static final char FIRST_UNIHAN = '\u3400'; 303 private static final Collator COLLATOR = Collator.getInstance(Locale.CHINA); 304 305 private static HanziToPinyin sInstance; 306 private final boolean mHasChinaCollator; 307 308 public static class Token { 309 /** 310 * Separator between target string for each source char 311 */ 312 public static final String SEPARATOR = " "; 313 314 public static final int LATIN = 1; 315 public static final int PINYIN = 2; 316 public static final int UNKNOWN = 3; 317 318 public Token() { 319 } 320 321 public Token(int type, String source, String target) { 322 this.type = type; 323 this.source = source; 324 this.target = target; 325 } 326 /** 327 * Type of this token, ASCII, PINYIN or UNKNOWN. 328 */ 329 public int type; 330 /** 331 * Original string before translation. 332 */ 333 public String source; 334 /** 335 * Translated string of source. For Han, target is corresponding Pinyin. 336 * Otherwise target is original string in source. 337 */ 338 public String target; 339 } 340 341 protected HanziToPinyin(boolean hasChinaCollator) { 342 mHasChinaCollator = hasChinaCollator; 343 } 344 345 public static HanziToPinyin getInstance() { 346 synchronized(HanziToPinyin.class) { 347 if (sInstance != null) { 348 return sInstance; 349 } 350 // Check if zh_CN collation data is available 351 final Locale locale[] = Collator.getAvailableLocales(); 352 for (int i = 0; i < locale.length; i++) { 353 if (locale[i].equals(Locale.CHINA)) { 354 sInstance = new HanziToPinyin(true); 355 return sInstance; 356 } 357 } 358 Log.w(TAG, "There is no Chinese collator, HanziToPinyin is disabled"); 359 sInstance = new HanziToPinyin(false); 360 return sInstance; 361 } 362 } 363 364 private Token getToken(char character) { 365 Token token = new Token(); 366 final String letter = Character.toString(character); 367 token.source = letter; 368 int offset = -1; 369 int cmp; 370 if (character < 256) { 371 token.type = Token.LATIN; 372 token.target = letter; 373 return token; 374 } else if (character < FIRST_UNIHAN) { 375 token.type = Token.UNKNOWN; 376 token.target = letter; 377 return token; 378 } else { 379 cmp = COLLATOR.compare(letter, FIRST_PINYIN_UNIHAN); 380 if (cmp < 0) { 381 token.type = Token.UNKNOWN; 382 token.target = letter; 383 return token; 384 } else if (cmp == 0) { 385 token.type = Token.PINYIN; 386 offset = 0; 387 } else { 388 cmp = COLLATOR.compare(letter, LAST_PINYIN_UNIHAN); 389 if (cmp > 0) { 390 token.type = Token.UNKNOWN; 391 token.target = letter; 392 return token; 393 } else if (cmp == 0) { 394 token.type = Token.PINYIN; 395 offset = UNIHANS.length - 1; 396 } 397 } 398 } 399 400 token.type = Token.PINYIN; 401 if (offset < 0) { 402 int begin = 0; 403 int end = UNIHANS.length - 1; 404 while (begin <= end) { 405 offset = (begin + end) / 2; 406 final String unihan = Character.toString(UNIHANS[offset]); 407 cmp = COLLATOR.compare(letter, unihan); 408 if (cmp == 0) { 409 break; 410 } else if (cmp > 0) { 411 begin = offset + 1; 412 } else { 413 end = offset - 1; 414 } 415 } 416 } 417 if (cmp < 0) { 418 offset--; 419 } 420 StringBuilder pinyin = new StringBuilder(); 421 for (int j = 0; j < PINYINS[offset].length && PINYINS[offset][j] != 0; j++) { 422 pinyin.append((char)PINYINS[offset][j]); 423 } 424 token.target = pinyin.toString(); 425 return token; 426 } 427 428 /** 429 * Convert the input to a array of tokens. The sequence of ASCII or Unknown 430 * characters without space will be put into a Token, One Hanzi character 431 * which has pinyin will be treated as a Token. 432 * If these is no China collator, the empty token array is returned. 433 */ 434 public ArrayList<Token> get(final String input) { 435 ArrayList<Token> tokens = new ArrayList<Token>(); 436 if (!mHasChinaCollator || TextUtils.isEmpty(input)) { 437 // return empty tokens. 438 return tokens; 439 } 440 final int inputLength = input.length(); 441 final StringBuilder sb = new StringBuilder(); 442 int tokenType = Token.LATIN; 443 // Go through the input, create a new token when 444 // a. Token type changed 445 // b. Get the Pinyin of current charater. 446 // c. current character is space. 447 for (int i = 0; i < inputLength; i++) { 448 final char character = input.charAt(i); 449 if (character == ' ') { 450 if (sb.length() > 0) { 451 addToken(sb, tokens, tokenType); 452 } 453 } else if (character < 256) { 454 if (tokenType != Token.LATIN && sb.length() > 0) { 455 addToken(sb, tokens, tokenType); 456 } 457 tokenType = Token.LATIN; 458 sb.append(character); 459 } else if (character < FIRST_UNIHAN) { 460 if (tokenType != Token.UNKNOWN && sb.length() > 0) { 461 addToken(sb, tokens, tokenType); 462 } 463 tokenType = Token.UNKNOWN; 464 sb.append(character); 465 } else { 466 Token t = getToken(character); 467 if (t.type == Token.PINYIN) { 468 if (sb.length() > 0) { 469 addToken(sb, tokens, tokenType); 470 } 471 tokens.add(t); 472 tokenType = Token.PINYIN; 473 } else { 474 if (tokenType != t.type && sb.length() > 0) { 475 addToken(sb, tokens, tokenType); 476 } 477 tokenType = t.type; 478 sb.append(character); 479 } 480 } 481 } 482 if (sb.length() > 0) { 483 addToken(sb, tokens, tokenType); 484 } 485 return tokens; 486 } 487 488 private void addToken(final StringBuilder sb, final ArrayList<Token> tokens, 489 final int tokenType) { 490 String str = sb.toString(); 491 tokens.add(new Token(tokenType, str, str)); 492 sb.setLength(0); 493 } 494 495 } 496