Android汉字转拼音工具类

Android源码中通讯录自带一个将汉字转成拼音的工具类

  1. HanziToPinyin.java,分享给有需要的朋友,可能在项目中用得到  


代码如下:


  1. package com.android.whether.tools;  
  2.   
  3. /* 
  4.  * Copyright (C) 2011 The Android Open Source Project 
  5.  * 
  6.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  7.  * you may not use this file except in compliance with the License. 
  8.  * You may obtain a copy of the License at 
  9.  * 
  10.  *      http://www.apache.org/licenses/LICENSE-2.0 
  11.  * 
  12.  * Unless required by applicable law or agreed to in writing, software 
  13.  * distributed under the License is distributed on an "AS IS" BASIS, 
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  15.  * See the License for the specific language governing permissions and 
  16.  * limitations under the License. 
  17.  */  
  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.HashMap;  
  25. import java.util.Locale;  
  26. import java.util.Map;  
  27.   
  28.   
  29. /** 
  30.  * An object to convert Chinese character to its corresponding pinyin string. For characters with 
  31.  * multiple possible pinyin string, only one is selected according to collator. Polyphone is not 
  32.  * supported in this implementation. This class is implemented to achieve the best runtime 
  33.  * performance and minimum runtime resources with tolerable sacrifice of accuracy. This 
  34.  * implementation highly depends on zh_CN ICU collation data and must be always synchronized with 
  35.  * ICU. 
  36.  * 
  37.  * Currently this file is aligned to zh.txt in ICU 4.6 
  38.  */  
  39. public class HanziToPinyin {  
  40.     private static final String TAG = "HanziToPinyin";  
  41.   
  42.     // Turn on this flag when we want to check internal data structure.  
  43.     private static final boolean DEBUG = false;  
  44.   
  45.     /** M: Special Hanzi list of multiple Pinyin @{ */  
  46.     public static Map<String,Character> specialHanzi = new HashMap<String, Character>();  
  47.         static {  
  48.             specialHanzi.put("5355"'\u5c71');   
  49.             specialHanzi.put("884c"'\u822a');   
  50.             specialHanzi.put("67cf"'\u535a');   
  51.             specialHanzi.put("8584"'\u535a');   
  52.             specialHanzi.put("90fd"'\u5ea6');   
  53.             specialHanzi.put("89e3"'\u5199');   
  54.             specialHanzi.put("957f"'\u6da8');   
  55.             specialHanzi.put("5c09"'\u4e0e');  
  56.             specialHanzi.put("5947"'\u5b63');   
  57.             specialHanzi.put("4e50"'\u6708');   
  58.             specialHanzi.put("6734"'\u7968');   
  59.             specialHanzi.put("4ec7"'\u6c42');   
  60.             specialHanzi.put("6298"'\u86c7');   
  61.             specialHanzi.put("67e5"'\u624e');   
  62.             specialHanzi.put("9002"'\u6269');   
  63.             specialHanzi.put("5bbf"'\u79c0');   
  64.             specialHanzi.put("8f66"'\u5177');   
  65.             specialHanzi.put("4f1a"'\u5feb');   
  66.         }  
  67.     /**@}*/  
  68.   
  69.     /** 
  70.      * Unihans array. 
  71.      * 
  72.      * Each unihans is the first one within same pinyin when collator is zh_CN. 
  73.      */  
  74.     public static final char[] UNIHANS = {  
  75.             '\u963f''\u54ce''\u5b89''\u80ae''\u51f9''\u516b',  
  76.             '\u6300''\u6273''\u90a6''\u52f9''\u9642''\u5954',  
  77.             '\u4f3b''\u5c44''\u8fb9''\u706c''\u618b''\u6c43',  
  78.             '\u51ab''\u7676''\u5cec''\u5693''\u5072''\u53c2',  
  79.             '\u4ed3''\u64a1''\u518a''\u5d7e''\u66fd''\u66fe',  
  80.             '\u5c64''\u53c9''\u8286''\u8fbf''\u4f25''\u6284',  
  81.             '\u8f66''\u62bb''\u6c88''\u6c89''\u9637''\u5403',  
  82.             '\u5145''\u62bd''\u51fa''\u6b3b''\u63e3''\u5ddb',  
  83.             '\u5205''\u5439''\u65fe''\u9034''\u5472''\u5306',  
  84.             '\u51d1''\u7c97''\u6c46''\u5d14''\u90a8''\u6413',  
  85.             '\u5491''\u5446''\u4e39''\u5f53''\u5200''\u561a',  
  86.             '\u6265''\u706f''\u6c10''\u55f2''\u7538''\u5201',  
  87.             '\u7239''\u4e01''\u4e1f''\u4e1c''\u543a''\u53be',  
  88.             '\u8011''\u8968''\u5428''\u591a''\u59b8''\u8bf6',  
  89.             '\u5940''\u97a5''\u513f''\u53d1''\u5e06''\u531a',  
  90.             '\u98de''\u5206''\u4e30''\u8985''\u4ecf''\u7d11',  
  91.             '\u4f15''\u65ee''\u4f85''\u7518''\u5188''\u768b',  
  92.             '\u6208''\u7ed9''\u6839''\u522f''\u5de5''\u52fe',  
  93.             '\u4f30''\u74dc''\u4e56''\u5173''\u5149''\u5f52',  
  94.             '\u4e28''\u5459''\u54c8''\u548d''\u4f44''\u592f',  
  95.             '\u8320''\u8bc3''\u9ed2''\u62eb''\u4ea8''\u5677',  
  96.             '\u53ff''\u9f41''\u4e6f''\u82b1''\u6000''\u72bf',  
  97.             '\u5ddf''\u7070''\u660f''\u5419''\u4e0c''\u52a0',  
  98.             '\u620b''\u6c5f''\u827d''\u9636''\u5dfe''\u5755',  
  99.             '\u5182''\u4e29''\u51e5''\u59e2''\u5658''\u519b',  
  100.             '\u5494''\u5f00''\u520a''\u5ffc''\u5c3b''\u533c',  
  101.             '\u808e''\u52a5''\u7a7a''\u62a0''\u625d''\u5938',  
  102.             '\u84af''\u5bbd''\u5321''\u4e8f''\u5764''\u6269',  
  103.             '\u5783''\u6765''\u5170''\u5577''\u635e''\u808b',  
  104.             '\u52d2''\u5d1a''\u5215''\u4fe9''\u5941''\u826f',  
  105.             '\u64a9''\u5217''\u62ce''\u5222''\u6e9c''\u56d6',  
  106.             '\u9f99''\u779c''\u565c''\u5a08''\u7567''\u62a1',  
  107.             '\u7f57''\u5463''\u5988''\u57cb''\u5ada''\u7264',  
  108.             '\u732b''\u4e48''\u5445''\u95e8''\u753f''\u54aa',  
  109.             '\u5b80''\u55b5''\u4e5c''\u6c11''\u540d''\u8c2c',  
  110.             '\u6478''\u54de''\u6bea''\u55ef''\u62cf''\u8149',  
  111.             '\u56e1''\u56d4''\u5b6c''\u7592''\u5a1e''\u6041',  
  112.             '\u80fd''\u59ae''\u62c8''\u5b22''\u9e1f''\u634f',  
  113.             '\u56dc''\u5b81''\u599e''\u519c''\u7fba''\u5974',  
  114.             '\u597b''\u759f''\u9ec1''\u90cd''\u5594''\u8bb4',  
  115.             '\u5991''\u62cd''\u7705''\u4e53''\u629b''\u5478',  
  116.             '\u55b7''\u5309''\u4e15''\u56e8''\u527d''\u6c15',  
  117.             '\u59d8''\u4e52''\u948b''\u5256''\u4ec6''\u4e03',  
  118.             '\u6390''\u5343''\u545b''\u6084''\u767f''\u4eb2',  
  119.             '\u72c5''\u828e''\u4e18''\u533a''\u5cd1''\u7f3a',  
  120.             '\u590b''\u5465''\u7a63''\u5a06''\u60f9''\u4eba',  
  121.             '\u6254''\u65e5''\u8338''\u53b9''\u909a''\u633c',  
  122.             '\u5827''\u5a51''\u77a4''\u637c''\u4ee8''\u6be2',  
  123.             '\u4e09''\u6852''\u63bb''\u95aa''\u68ee''\u50e7',  
  124.             '\u6740''\u7b5b''\u5c71''\u4f24''\u5f30''\u5962',  
  125.             '\u7533''\u8398''\u6552''\u5347''\u5c38''\u53ce',  
  126.             '\u4e66''\u5237''\u8870''\u95e9''\u53cc''\u8c01',  
  127.             '\u542e''\u8bf4''\u53b6''\u5fea''\u635c''\u82cf',  
  128.             '\u72fb''\u590a''\u5b59''\u5506''\u4ed6''\u56fc',  
  129.             '\u574d''\u6c64''\u5932''\u5fd1''\u71a5''\u5254',  
  130.             '\u5929''\u65eb''\u5e16''\u5385''\u56f2''\u5077',  
  131.             '\u51f8''\u6e4d''\u63a8''\u541e''\u4e47''\u7a75',  
  132.             '\u6b6a''\u5f2f''\u5c23''\u5371''\u6637''\u7fc1',  
  133.             '\u631d''\u4e4c''\u5915''\u8672''\u4eda''\u4e61',  
  134.             '\u7071''\u4e9b''\u5fc3''\u661f''\u51f6''\u4f11',  
  135.             '\u5401''\u5405''\u524a''\u5743''\u4e2b''\u6079',  
  136.             '\u592e''\u5e7a''\u503b''\u4e00''\u56d9''\u5e94',  
  137.             '\u54df''\u4f63''\u4f18''\u625c''\u56e6''\u66f0',  
  138.             '\u6655''\u7b60''\u7b7c''\u5e00''\u707d''\u5142',  
  139.             '\u5328''\u50ae''\u5219''\u8d3c''\u600e''\u5897',  
  140.             '\u624e''\u635a''\u6cbe''\u5f20''\u957f''\u9577',  
  141.             '\u4f4b''\u8707''\u8d1e''\u4e89''\u4e4b''\u5cd9',  
  142.             '\u5ea2''\u4e2d''\u5dde''\u6731''\u6293''\u62fd',  
  143.             '\u4e13''\u5986''\u96b9''\u5b92''\u5353''\u4e72',  
  144.             '\u5b97''\u90b9''\u79df''\u94bb''\u539c''\u5c0a',  
  145.             '\u6628''\u5159''\u9fc3''\u9fc4', };  
  146.   
  147.     /** 
  148.      * Pinyin array. 
  149.      * 
  150.      * Each pinyin is corresponding to unihans of same 
  151.      * offset in the unihans array. 
  152.      */  
  153.     public static final byte[][] PINYINS = {  
  154.             { 65,   0,   0,   0,   0,   0}, { 65,  73,   0,   0,   0,   0},  
  155.             { 65,  78,   0,   0,   0,   0}, { 65,  78,  71,   0,   0,   0},  
  156.             { 65,  79,   0,   0,   0,   0}, { 66,  65,   0,   0,   0,   0},  
  157.             { 66,  65,  73,   0,   0,   0}, { 66,  65,  78,   0,   0,   0},  
  158.             { 66,  65,  78,  71,   0,   0}, { 66,  65,  79,   0,   0,   0},  
  159.             { 66,  69,  73,   0,   0,   0}, { 66,  69,  78,   0,   0,   0},  
  160.             { 66,  69,  78,  71,   0,   0}, { 66,  73,   0,   0,   0,   0},  
  161.             { 66,  73,  65,  78,   0,   0}, { 66,  73,  65,  79,   0,   0},  
  162.             { 66,  73,  69,   0,   0,   0}, { 66,  73,  78,   0,   0,   0},  
  163.             { 66,  73,  78,  71,   0,   0}, { 66,  79,   0,   0,   0,   0},  
  164.             { 66,  85,   0,   0,   0,   0}, { 67,  65,   0,   0,   0,   0},  
  165.             { 67,  65,  73,   0,   0,   0}, { 67,  65,  78,   0,   0,   0},  
  166.             { 67,  65,  78,  71,   0,   0}, { 67,  65,  79,   0,   0,   0},  
  167.             { 67,  69,   0,   0,   0,   0}, { 67,  69,  78,   0,   0,   0},  
  168.             { 67,  69,  78,  71,   0,   0}, { 90,  69,  78,  71,   0,   0},  
  169.             { 67,  69,  78,  71,   0,   0}, { 67,  72,  65,   0,   0,   0},  
  170.             { 67,  72,  65,  73,   0,   0}, { 67,  72,  65,  78,   0,   0},  
  171.             { 67,  72,  65,  78,  71,   0}, { 67,  72,  65,  79,   0,   0},  
  172.             { 67,  72,  69,   0,   0,   0}, { 67,  72,  69,  78,   0,   0},  
  173.             { 83,  72,  69,  78,   0,   0}, { 67,  72,  69,  78,   0,   0},  
  174.             { 67,  72,  69,  78,  71,   0}, { 67,  72,  73,   0,   0,   0},  
  175.             { 67,  72,  79,  78,  71,   0}, { 67,  72,  79,  85,   0,   0},  
  176.             { 67,  72,  85,   0,   0,   0}, { 67,  72,  85,  65,   0,   0},  
  177.             { 67,  72,  85,  65,  73,   0}, { 67,  72,  85,  65,  78,   0},  
  178.             { 67,  72,  85,  65,  78,  71}, { 67,  72,  85,  73,   0,   0},  
  179.             { 67,  72,  85,  78,   0,   0}, { 67,  72,  85,  79,   0,   0},  
  180.             { 67,  73,   0,   0,   0,   0}, { 67,  79,  78,  71,   0,   0},  
  181.             { 67,  79,  85,   0,   0,   0}, { 67,  85,   0,   0,   0,   0},  
  182.             { 67,  85,  65,  78,   0,   0}, { 67,  85,  73,   0,   0,   0},  
  183.             { 67,  85,  78,   0,   0,   0}, { 67,  85,  79,   0,   0,   0},  
  184.             { 68,  65,   0,   0,   0,   0}, { 68,  65,  73,   0,   0,   0},  
  185.             { 68,  65,  78,   0,   0,   0}, { 68,  65,  78,  71,   0,   0},  
  186.             { 68,  65,  79,   0,   0,   0}, { 68,  69,   0,   0,   0,   0},  
  187.             { 68,  69,  78,   0,   0,   0}, { 68,  69,  78,  71,   0,   0},  
  188.             { 68,  73,   0,   0,   0,   0}, { 68,  73,  65,   0,   0,   0},  
  189.             { 68,  73,  65,  78,   0,   0}, { 68,  73,  65,  79,   0,   0},  
  190.             { 68,  73,  69,   0,   0,   0}, { 68,  73,  78,  71,   0,   0},  
  191.             { 68,  73,  85,   0,   0,   0}, { 68,  79,  78,  71,   0,   0},  
  192.             { 68,  79,  85,   0,   0,   0}, { 68,  85,   0,   0,   0,   0},  
  193.             { 68,  85,  65,  78,   0,   0}, { 68,  85,  73,   0,   0,   0},  
  194.             { 68,  85,  78,   0,   0,   0}, { 68,  85,  79,   0,   0,   0},  
  195.             { 69,   0,   0,   0,   0,   0}, { 69,  73,   0,   0,   0,   0},  
  196.             { 69,  78,   0,   0,   0,   0}, { 69,  78,  71,   0,   0,   0},  
  197.             { 69,  82,   0,   0,   0,   0}, { 70,  65,   0,   0,   0,   0},  
  198.             { 70,  65,  78,   0,   0,   0}, { 70,  65,  78,  71,   0,   0},  
  199.             { 70,  69,  73,   0,   0,   0}, { 70,  69,  78,   0,   0,   0},  
  200.             { 70,  69,  78,  71,   0,   0}, { 70,  73,  65,  79,   0,   0},  
  201.             { 70,  79,   0,   0,   0,   0}, { 70,  79,  85,   0,   0,   0},  
  202.             { 70,  85,   0,   0,   0,   0}, { 71,  65,   0,   0,   0,   0},  
  203.             { 71,  65,  73,   0,   0,   0}, { 71,  65,  78,   0,   0,   0},  
  204.             { 71,  65,  78,  71,   0,   0}, { 71,  65,  79,   0,   0,   0},  
  205.             { 71,  69,   0,   0,   0,   0}, { 71,  69,  73,   0,   0,   0},  
  206.             { 71,  69,  78,   0,   0,   0}, { 71,  69,  78,  71,   0,   0},  
  207.             { 71,  79,  78,  71,   0,   0}, { 71,  79,  85,   0,   0,   0},  
  208.             { 71,  85,   0,   0,   0,   0}, { 71,  85,  65,   0,   0,   0},  
  209.             { 71,  85,  65,  73,   0,   0}, { 71,  85,  65,  78,   0,   0},  
  210.             { 71,  85,  65,  78,  71,   0}, { 71,  85,  73,   0,   0,   0},  
  211.             { 71,  85,  78,   0,   0,   0}, { 71,  85,  79,   0,   0,   0},  
  212.             { 72,  65,   0,   0,   0,   0}, { 72,  65,  73,   0,   0,   0},  
  213.             { 72,  65,  78,   0,   0,   0}, { 72,  65,  78,  71,   0,   0},  
  214.             { 72,  65,  79,   0,   0,   0}, { 72,  69,   0,   0,   0,   0},  
  215.             { 72,  69,  73,   0,   0,   0}, { 72,  69,  78,   0,   0,   0},  
  216.             { 72,  69,  78,  71,   0,   0}, { 72,  77,   0,   0,   0,   0},  
  217.             { 72,  79,  78,  71,   0,   0}, { 72,  79,  85,   0,   0,   0},  
  218.             { 72,  85,   0,   0,   0,   0}, { 72,  85,  65,   0,   0,   0},  
  219.             { 72,  85,  65,  73,   0,   0}, { 72,  85,  65,  78,   0,   0},  
  220.             { 72,  85,  65,  78,  71,   0}, { 72,  85,  73,   0,   0,   0},  
  221.             { 72,  85,  78,   0,   0,   0}, { 72,  85,  79,   0,   0,   0},  
  222.             { 74,  73,   0,   0,   0,   0}, { 74,  73,  65,   0,   0,   0},  
  223.             { 74,  73,  65,  78,   0,   0}, { 74,  73,  65,  78,  71,   0},  
  224.             { 74,  73,  65,  79,   0,   0}, { 74,  73,  69,   0,   0,   0},  
  225.             { 74,  73,  78,   0,   0,   0}, { 74,  73,  78,  71,   0,   0},  
  226.             { 74,  73,  79,  78,  71,   0}, { 74,  73,  85,   0,   0,   0},  
  227.             { 74,  85,   0,   0,   0,   0}, { 74,  85,  65,  78,   0,   0},  
  228.             { 74,  85,  69,   0,   0,   0}, { 74,  85,  78,   0,   0,   0},  
  229.             { 75,  65,   0,   0,   0,   0}, { 75,  65,  73,   0,   0,   0},  
  230.             { 75,  65,  78,   0,   0,   0}, { 75,  65,  78,  71,   0,   0},  
  231.             { 75,  65,  79,   0,   0,   0}, { 75,  69,   0,   0,   0,   0},  
  232.             { 75,  69,  78,   0,   0,   0}, { 75,  69,  78,  71,   0,   0},  
  233.             { 75,  79,  78,  71,   0,   0}, { 75,  79,  85,   0,   0,   0},  
  234.             { 75,  85,   0,   0,   0,   0}, { 75,  85,  65,   0,   0,   0},  
  235.             { 75,  85,  65,  73,   0,   0}, { 75,  85,  65,  78,   0,   0},  
  236.             { 75,  85,  65,  78,  71,   0}, { 75,  85,  73,   0,   0,   0},  
  237.             { 75,  85,  78,   0,   0,   0}, { 75,  85,  79,   0,   0,   0},  
  238.             { 76,  65,   0,   0,   0,   0}, { 76,  65,  73,   0,   0,   0},  
  239.             { 76,  65,  78,   0,   0,   0}, { 76,  65,  78,  71,   0,   0},  
  240.             { 76,  65,  79,   0,   0,   0}, { 76,  69,   0,   0,   0,   0},  
  241.             { 76,  69,  73,   0,   0,   0}, { 76,  69,  78,  71,   0,   0},  
  242.             { 76,  73,   0,   0,   0,   0}, { 76,  73,  65,   0,   0,   0},  
  243.             { 76,  73,  65,  78,   0,   0}, { 76,  73,  65,  78,  71,   0},  
  244.             { 76,  73,  65,  79,   0,   0}, { 76,  73,  69,   0,   0,   0},  
  245.             { 76,  73,  78,   0,   0,   0}, { 76,  73,  78,  71,   0,   0},  
  246.             { 76,  73,  85,   0,   0,   0}, { 76,  79,   0,   0,   0,   0},  
  247.             { 76,  79,  78,  71,   0,   0}, { 76,  79,  85,   0,   0,   0},  
  248.             { 76,  85,   0,   0,   0,   0}, { 76,  85,  65,  78,   0,   0},  
  249.             { 76,  85,  69,   0,   0,   0}, { 76,  85,  78,   0,   0,   0},  
  250.             { 76,  85,  79,   0,   0,   0}, { 77,   0,   0,   0,   0,   0},  
  251.             { 77,  65,   0,   0,   0,   0}, { 77,  65,  73,   0,   0,   0},  
  252.             { 77,  65,  78,   0,   0,   0}, { 77,  65,  78,  71,   0,   0},  
  253.             { 77,  65,  79,   0,   0,   0}, { 77,  69,   0,   0,   0,   0},  
  254.             { 77,  69,  73,   0,   0,   0}, { 77,  69,  78,   0,   0,   0},  
  255.             { 77,  69,  78,  71,   0,   0}, { 77,  73,   0,   0,   0,   0},  
  256.             { 77,  73,  65,  78,   0,   0}, { 77,  73,  65,  79,   0,   0},  
  257.             { 77,  73,  69,   0,   0,   0}, { 77,  73,  78,   0,   0,   0},  
  258.             { 77,  73,  78,  71,   0,   0}, { 77,  73,  85,   0,   0,   0},  
  259.             { 77,  79,   0,   0,   0,   0}, { 77,  79,  85,   0,   0,   0},  
  260.             { 77,  85,   0,   0,   0,   0}, { 78,   0,   0,   0,   0,   0},  
  261.             { 78,  65,   0,   0,   0,   0}, { 78,  65,  73,   0,   0,   0},  
  262.             { 78,  65,  78,   0,   0,   0}, { 78,  65,  78,  71,   0,   0},  
  263.             { 78,  65,  79,   0,   0,   0}, { 78,  69,   0,   0,   0,   0},  
  264.             { 78,  69,  73,   0,   0,   0}, { 78,  69,  78,   0,   0,   0},  
  265.             { 78,  69,  78,  71,   0,   0}, { 78,  73,   0,   0,   0,   0},  
  266.             { 78,  73,  65,  78,   0,   0}, { 78,  73,  65,  78,  71,   0},  
  267.             { 78,  73,  65,  79,   0,   0}, { 78,  73,  69,   0,   0,   0},  
  268.             { 78,  73,  78,   0,   0,   0}, { 78,  73,  78,  71,   0,   0},  
  269.             { 78,  73,  85,   0,   0,   0}, { 78,  79,  78,  71,   0,   0},  
  270.             { 78,  79,  85,   0,   0,   0}, { 78,  85,   0,   0,   0,   0},  
  271.             { 78,  85,  65,  78,   0,   0}, { 78,  85,  69,   0,   0,   0},  
  272.             { 78,  85,  78,   0,   0,   0}, { 78,  85,  79,   0,   0,   0},  
  273.             { 79,   0,   0,   0,   0,   0}, { 79,  85,   0,   0,   0,   0},  
  274.             { 80,  65,   0,   0,   0,   0}, { 80,  65,  73,   0,   0,   0},  
  275.             { 80,  65,  78,   0,   0,   0}, { 80,  65,  78,  71,   0,   0},  
  276.             { 80,  65,  79,   0,   0,   0}, { 80,  69,  73,   0,   0,   0},  
  277.             { 80,  69,  78,   0,   0,   0}, { 80,  69,  78,  71,   0,   0},  
  278.             { 80,  73,   0,   0,   0,   0}, { 80,  73,  65,  78,   0,   0},  
  279.             { 80,  73,  65,  79,   0,   0}, { 80,  73,  69,   0,   0,   0},  
  280.             { 80,  73,  78,   0,   0,   0}, { 80,  73,  78,  71,   0,   0},  
  281.             { 80,  79,   0,   0,   0,   0}, { 80,  79,  85,   0,   0,   0},  
  282.             { 80,  85,   0,   0,   0,   0}, { 81,  73,   0,   0,   0,   0},  
  283.             { 81,  73,  65,   0,   0,   0}, { 81,  73,  65,  78,   0,   0},  
  284.             { 81,  73,  65,  78,  71,   0}, { 81,  73,  65,  79,   0,   0},  
  285.             { 81,  73,  69,   0,   0,   0}, { 81,  73,  78,   0,   0,   0},  
  286.             { 81,  73,  78,  71,   0,   0}, { 81,  73,  79,  78,  71,   0},  
  287.             { 81,  73,  85,   0,   0,   0}, { 81,  85,   0,   0,   0,   0},  
  288.             { 81,  85,  65,  78,   0,   0}, { 81,  85,  69,   0,   0,   0},  
  289.             { 81,  85,  78,   0,   0,   0}, { 82,  65,  78,   0,   0,   0},  
  290.             { 82,  65,  78,  71,   0,   0}, { 82,  65,  79,   0,   0,   0},  
  291.             { 82,  69,   0,   0,   0,   0}, { 82,  69,  78,   0,   0,   0},  
  292.             { 82,  69,  78,  71,   0,   0}, { 82,  73,   0,   0,   0,   0},  
  293.             { 82,  79,  78,  71,   0,   0}, { 82,  79,  85,   0,   0,   0},  
  294.             { 82,  85,   0,   0,   0,   0}, { 82,  85,  65,   0,   0,   0},  
  295.             { 82,  85,  65,  78,   0,   0}, { 82,  85,  73,   0,   0,   0},  
  296.             { 82,  85,  78,   0,   0,   0}, { 82,  85,  79,   0,   0,   0},  
  297.             { 83,  65,   0,   0,   0,   0}, { 83,  65,  73,   0,   0,   0},  
  298.             { 83,  65,  78,   0,   0,   0}, { 83,  65,  78,  71,   0,   0},  
  299.             { 83,  65,  79,   0,   0,   0}, { 83,  69,   0,   0,   0,   0},  
  300.             { 83,  69,  78,   0,   0,   0}, { 83,  69,  78,  71,   0,   0},  
  301.             { 83,  72,  65,   0,   0,   0}, { 83,  72,  65,  73,   0,   0},  
  302.             { 83,  72,  65,  78,   0,   0}, { 83,  72,  65,  78,  71,   0},  
  303.             { 83,  72,  65,  79,   0,   0}, { 83,  72,  69,   0,   0,   0},  
  304.             { 83,  72,  69,  78,   0,   0}, { 88,  73,  78,   0,   0,   0},  
  305.             { 83,  72,  69,  78,   0,   0}, { 83,  72,  69,  78,  71,   0},  
  306.             { 83,  72,  73,   0,   0,   0}, { 83,  72,  79,  85,   0,   0},  
  307.             { 83,  72,  85,   0,   0,   0}, { 83,  72,  85,  65,   0,   0},  
  308.             { 83,  72,  85,  65,  73,   0}, { 83,  72,  85,  65,  78,   0},  
  309.             { 83,  72,  85,  65,  78,  71}, { 83,  72,  85,  73,   0,   0},  
  310.             { 83,  72,  85,  78,   0,   0}, { 83,  72,  85,  79,   0,   0},  
  311.             { 83,  73,   0,   0,   0,   0}, { 83,  79,  78,  71,   0,   0},  
  312.             { 83,  79,  85,   0,   0,   0}, { 83,  85,   0,   0,   0,   0},  
  313.             { 83,  85,  65,  78,   0,   0}, { 83,  85,  73,   0,   0,   0},  
  314.             { 83,  85,  78,   0,   0,   0}, { 83,  85,  79,   0,   0,   0},  
  315.             { 84,  65,   0,   0,   0,   0}, { 84,  65,  73,   0,   0,   0},  
  316.             { 84,  65,  78,   0,   0,   0}, { 84,  65,  78,  71,   0,   0},  
  317.             { 84,  65,  79,   0,   0,   0}, { 84,  69,   0,   0,   0,   0},  
  318.             { 84,  69,  78,  71,   0,   0}, { 84,  73,   0,   0,   0,   0},  
  319.             { 84,  73,  65,  78,   0,   0}, { 84,  73,  65,  79,   0,   0},  
  320.             { 84,  73,  69,   0,   0,   0}, { 84,  73,  78,  71,   0,   0},  
  321.             { 84,  79,  78,  71,   0,   0}, { 84,  79,  85,   0,   0,   0},  
  322.             { 84,  85,   0,   0,   0,   0}, { 84,  85,  65,  78,   0,   0},  
  323.             { 84,  85,  73,   0,   0,   0}, { 84,  85,  78,   0,   0,   0},  
  324.             { 84,  85,  79,   0,   0,   0}, { 87,  65,   0,   0,   0,   0},  
  325.             { 87,  65,  73,   0,   0,   0}, { 87,  65,  78,   0,   0,   0},  
  326.             { 87,  65,  78,  71,   0,   0}, { 87,  69,  73,   0,   0,   0},  
  327.             { 87,  69,  78,   0,   0,   0}, { 87,  69,  78,  71,   0,   0},  
  328.             { 87,  79,   0,   0,   0,   0}, { 87,  85,   0,   0,   0,   0},  
  329.             { 88,  73,   0,   0,   0,   0}, { 88,  73,  65,   0,   0,   0},  
  330.             { 88,  73,  65,  78,   0,   0}, { 88,  73,  65,  78,  71,   0},  
  331.             { 88,  73,  65,  79,   0,   0}, { 88,  73,  69,   0,   0,   0},  
  332.             { 88,  73,  78,   0,   0,   0}, { 88,  73,  78,  71,   0,   0},  
  333.             { 88,  73,  79,  78,  71,   0}, { 88,  73,  85,   0,   0,   0},  
  334.             { 88,  85,   0,   0,   0,   0}, { 88,  85,  65,  78,   0,   0},  
  335.             { 88,  85,  69,   0,   0,   0}, { 88,  85,  78,   0,   0,   0},  
  336.             { 89,  65,   0,   0,   0,   0}, { 89,  65,  78,   0,   0,   0},  
  337.             { 89,  65,  78,  71,   0,   0}, { 89,  65,  79,   0,   0,   0},  
  338.             { 89,  69,   0,   0,   0,   0}, { 89,  73,   0,   0,   0,   0},  
  339.             { 89,  73,  78,   0,   0,   0}, { 89,  73,  78,  71,   0,   0},  
  340.             { 89,  79,   0,   0,   0,   0}, { 89,  79,  78,  71,   0,   0},  
  341.             { 89,  79,  85,   0,   0,   0}, { 89,  85,   0,   0,   0,   0},  
  342.             { 89,  85,  65,  78,   0,   0}, { 89,  85,  69,   0,   0,   0},  
  343.             { 89,  85,  78,   0,   0,   0}, { 74,  85,  78,   0,   0,   0},  
  344.             { 89,  85,  78,   0,   0,   0}, { 90,  65,   0,   0,   0,   0},  
  345.             { 90,  65,  73,   0,   0,   0}, { 90,  65,  78,   0,   0,   0},  
  346.             { 90,  65,  78,  71,   0,   0}, { 90,  65,  79,   0,   0,   0},  
  347.             { 90,  69,   0,   0,   0,   0}, { 90,  69,  73,   0,   0,   0},  
  348.             { 90,  69,  78,   0,   0,   0}, { 90,  69,  78,  71,   0,   0},  
  349.             { 90,  72,  65,   0,   0,   0}, { 90,  72,  65,  73,   0,   0},  
  350.             { 90,  72,  65,  78,   0,   0}, { 90,  72,  65,  78,  71,   0},  
  351.             { 67,  72,  65,  78,  71,   0}, { 90,  72,  65,  78,  71,   0},  
  352.             { 90,  72,  65,  79,   0,   0}, { 90,  72,  69,   0,   0,   0},  
  353.             { 90,  72,  69,  78,   0,   0}, { 90,  72,  69,  78,  71,   0},  
  354.             { 90,  72,  73,   0,   0,   0}, { 83,  72,  73,   0,   0,   0},  
  355.             { 90,  72,  73,   0,   0,   0}, { 90,  72,  79,  78,  71,   0},  
  356.             { 90,  72,  79,  85,   0,   0}, { 90,  72,  85,   0,   0,   0},  
  357.             { 90,  72,  85,  65,   0,   0}, { 90,  72,  85,  65,  73,   0},  
  358.             { 90,  72,  85,  65,  78,   0}, { 90,  72,  85,  65,  78,  71},  
  359.             { 90,  72,  85,  73,   0,   0}, { 90,  72,  85,  78,   0,   0},  
  360.             { 90,  72,  85,  79,   0,   0}, { 90,  73,   0,   0,   0,   0},  
  361.             { 90,  79,  78,  71,   0,   0}, { 90,  79,  85,   0,   0,   0},  
  362.             { 90,  85,   0,   0,   0,   0}, { 90,  85,  65,  78,   0,   0},  
  363.             { 90,  85,  73,   0,   0,   0}, { 90,  85,  78,   0,   0,   0},  
  364.             { 90,  85,  79,   0,   0,   0}, {  0,   0,   0,   0,   0,   0},  
  365.             { 83,  72,  65,  78,   0,   0}, {  0,   0,   0,   0,   0,   0}, };  
  366.   
  367.     /// The following lines are provided and maintained by Mediatek Inc.  
  368.     //New Feature ALPS00338325: dialer search support Russian, Arabic, Hebrew.  
  369.     private static Map<Character,Character> sMuiSupportMap = new HashMap<Character, Character>();  
  370.     // support Russian  
  371.     static {  
  372.         sMuiSupportMap.put('\u0410''2'); // 袗  
  373.         sMuiSupportMap.put('\u0411''2'); // 袘  
  374.         sMuiSupportMap.put('\u0412''2'); // 袙  
  375.         sMuiSupportMap.put('\u0413''2'); // 袚  
  376.   
  377.         sMuiSupportMap.put('\u0414''3'); // 袛  
  378.         sMuiSupportMap.put('\u0415''3'); // 袝  
  379.         sMuiSupportMap.put('\u0416''3'); // 袞  
  380.         sMuiSupportMap.put('\u0417''3'); // 袟  
  381.   
  382.         sMuiSupportMap.put('\u0418''4'); // 袠  
  383.         sMuiSupportMap.put('\u0419''4'); // 袡  
  384.         sMuiSupportMap.put('\u041a''4'); // 袣  
  385.         sMuiSupportMap.put('\u041b''4'); // 袥  
  386.   
  387.         sMuiSupportMap.put('\u041c''5'); // 袦  
  388.         sMuiSupportMap.put('\u041d''5'); // 袧  
  389.         sMuiSupportMap.put('\u041e''5'); // 袨  
  390.         sMuiSupportMap.put('\u041f''5'); // 袩  
  391.   
  392.         sMuiSupportMap.put('\u0420''6'); // 袪  
  393.         sMuiSupportMap.put('\u0421''6'); // 小  
  394.         sMuiSupportMap.put('\u0422''6'); // 孝  
  395.         sMuiSupportMap.put('\u0423''6'); // 校  
  396.   
  397.         sMuiSupportMap.put('\u0424''7'); // 肖  
  398.         sMuiSupportMap.put('\u0425''7'); // 啸  
  399.         sMuiSupportMap.put('\u0426''7'); // 笑  
  400.         sMuiSupportMap.put('\u0427''7'); // 效  
  401.   
  402.         sMuiSupportMap.put('\u0428''8'); // 楔  
  403.         sMuiSupportMap.put('\u0429''8'); // 些  
  404.         sMuiSupportMap.put('\u042a''8'); // 歇  
  405.         sMuiSupportMap.put('\u042b''8'); // 蝎  
  406.   
  407.         sMuiSupportMap.put('\u042c''9'); // 鞋  
  408.         sMuiSupportMap.put('\u042d''9'); // 协  
  409.         sMuiSupportMap.put('\u042e''9'); // 挟  
  410.         sMuiSupportMap.put('\u042f''9'); // 携  
  411.   
  412.         sMuiSupportMap.put('\u0430''2'); // 邪  
  413.         sMuiSupportMap.put('\u0431''2'); // 斜  
  414.         sMuiSupportMap.put('\u0432''2'); // 胁  
  415.         sMuiSupportMap.put('\u0433''2'); // 谐  
  416.   
  417.         sMuiSupportMap.put('\u0434''3'); // 写  
  418.         sMuiSupportMap.put('\u0435''3'); // 械  
  419.         sMuiSupportMap.put('\u0436''3'); // 卸  
  420.         sMuiSupportMap.put('\u0437''3'); // 蟹  
  421.   
  422.         sMuiSupportMap.put('\u0438''4'); // 懈  
  423.         sMuiSupportMap.put('\u0439''4'); // 泄  
  424.         sMuiSupportMap.put('\u043a''4'); // 泻  
  425.         sMuiSupportMap.put('\u043b''4'); // 谢  
  426.   
  427.         sMuiSupportMap.put('\u043c''5'); // 屑  
  428.         sMuiSupportMap.put('\u043d''5'); // 薪  
  429.         sMuiSupportMap.put('\u043e''5'); // 芯  
  430.         sMuiSupportMap.put('\u043f''5'); // 锌  
  431.   
  432.         sMuiSupportMap.put('\u0440''6'); // 褉  
  433.         sMuiSupportMap.put('\u0441''6'); // 褋  
  434.         sMuiSupportMap.put('\u0442''6'); // 褌  
  435.         sMuiSupportMap.put('\u0443''6'); // 褍  
  436.   
  437.         sMuiSupportMap.put('\u0444''7'); // 褎  
  438.         sMuiSupportMap.put('\u0445''7'); // 褏  
  439.         sMuiSupportMap.put('\u0446''7'); // 褑  
  440.         sMuiSupportMap.put('\u0447''7'); // 褔  
  441.   
  442.         sMuiSupportMap.put('\u0448''8'); // 褕  
  443.         sMuiSupportMap.put('\u0449''8'); // 褖  
  444.         sMuiSupportMap.put('\u044a''8'); // 褗  
  445.         sMuiSupportMap.put('\u044b''8'); // 褘  
  446.   
  447.         sMuiSupportMap.put('\u044c''9'); // 褜  
  448.         sMuiSupportMap.put('\u044d''9'); // 褝  
  449.         sMuiSupportMap.put('\u044e''9'); // 褞  
  450.         sMuiSupportMap.put('\u044f''9'); // 褟  
  451.   
  452.         sMuiSupportMap.put('\u0401''3'); // 衼  
  453.         sMuiSupportMap.put('\u0451''3'); // 褢  
  454.     }  
  455.   
  456.     // support Arabic  
  457.     static {  
  458.         sMuiSupportMap.put('\u0628''2'); // 亘  
  459.         sMuiSupportMap.put('\u0629''2'); // 丞  
  460.         sMuiSupportMap.put('\u062a''2'); // 鬲  
  461.         sMuiSupportMap.put('\u062b''2'); // 孬  
  462.   
  463.         sMuiSupportMap.put('\u0621''3'); // 亍  
  464.         sMuiSupportMap.put('\u0627''3'); // 丕  
  465.   
  466.         sMuiSupportMap.put('\u0633''4'); // 爻  
  467.         sMuiSupportMap.put('\u0634''4'); // 卮  
  468.         sMuiSupportMap.put('\u0635''4'); // 氐  
  469.         sMuiSupportMap.put('\u0636''4'); // 囟  
  470.   
  471.         sMuiSupportMap.put('\u062f''5'); // 丿  
  472.         sMuiSupportMap.put('\u0630''5'); // 匕  
  473.         sMuiSupportMap.put('\u0631''5'); // 乇  
  474.         sMuiSupportMap.put('\u0632''5'); // 夭  
  475.   
  476.         sMuiSupportMap.put('\u062c''6'); // 噩  
  477.         sMuiSupportMap.put('\u062d''6'); // 丨  
  478.         sMuiSupportMap.put('\u062e''6'); // 禺  
  479.   
  480.         sMuiSupportMap.put('\u0646''7'); // 賳  
  481.         sMuiSupportMap.put('\u0647''7'); // 賴  
  482.         sMuiSupportMap.put('\u0648''7'); // 賵  
  483.         sMuiSupportMap.put('\u0649''7'); // 賶  
  484.   
  485.         sMuiSupportMap.put('\u0641''8'); // 賮  
  486.         sMuiSupportMap.put('\u0642''8'); // 賯  
  487.         sMuiSupportMap.put('\u0643''8'); // 賰  
  488.         sMuiSupportMap.put('\u0644''8'); // 賱  
  489.         sMuiSupportMap.put('\u0645''8'); // 賲  
  490.   
  491.         sMuiSupportMap.put('\u0637''9'); // 胤  
  492.         sMuiSupportMap.put('\u0638''9'); // 馗  
  493.         sMuiSupportMap.put('\u0639''9'); // 毓  
  494.         sMuiSupportMap.put('\u063a''9'); // 睾  
  495.     }     
  496.     // support Hebrew  
  497.     static {  
  498.         sMuiSupportMap.put('\u05d3''2'); // 讚  
  499.         sMuiSupportMap.put('\u05d4''2'); // 讛  
  500.         sMuiSupportMap.put('\u05d5''2'); // 讜  
  501.   
  502.         sMuiSupportMap.put('\u05d0''3'); // 讗  
  503.         sMuiSupportMap.put('\u05d1''3'); // 讘  
  504.         sMuiSupportMap.put('\u05d2''3'); // 讙  
  505.   
  506.         sMuiSupportMap.put('\u05de''4'); // 诪  
  507.         sMuiSupportMap.put('\u05e0''4'); // 谞  
  508.   
  509.         sMuiSupportMap.put('\u05dc''5'); // 诇  
  510.         sMuiSupportMap.put('\u05db''5'); // 讻  
  511.   
  512.         sMuiSupportMap.put('\u05d6''6'); // 讝  
  513.         sMuiSupportMap.put('\u05d7''6'); // 讞  
  514.         sMuiSupportMap.put('\u05d8''6'); // 讟  
  515.   
  516.         sMuiSupportMap.put('\u05e8''7'); // 专  
  517.         sMuiSupportMap.put('\u05e9''7'); // 砖  
  518.         sMuiSupportMap.put('\u05ea''7'); // 转  
  519.   
  520.         sMuiSupportMap.put('\u05e6''8'); // 爪  
  521.         sMuiSupportMap.put('\u05e7''8'); // 拽  
  522.   
  523.         sMuiSupportMap.put('\u05e1''9'); // 住  
  524.         sMuiSupportMap.put('\u05e2''9'); // 注  
  525.         sMuiSupportMap.put('\u05e3''9'); // 祝  
  526.   
  527.     }  
  528.     /// The previous lines are provided and maintained by Mediatek Inc.  
  529.     /** First and last Chinese character with known Pinyin according to zh collation */  
  530.     private static final String FIRST_PINYIN_UNIHAN = "\u963F";  
  531.     private static final String LAST_PINYIN_UNIHAN = "\u9FFF";  
  532.   
  533.     private static final Collator COLLATOR = Collator.getInstance(Locale.CHINA);  
  534.   
  535.     private static HanziToPinyin sInstance;  
  536.     private final boolean mHasChinaCollator;  
  537.     /// The following lines are provided and maintained by Mediatek Inc.New  
  538.     // Feature ALPS00338325.  
  539.     /** First upper, last upper and last lower Russian character */  
  540.     private static final char FIRST_RUSSIAN_UPPER = '\u0410';  
  541.     private static final char LAST_RUSSIAN_UPPER = '\u042f';  
  542.     private static final char LAST_RUSSIAN_LOWER = '\u044f';  
  543.     /** First and last Arabic character */  
  544.     private static final char FIRST_ARABIC = '\u0628';  
  545.     private static final char LAST_ARABIC = '\u0649';  
  546.     /** First and last Hebrew character */  
  547.     private static final char FIRST_HEBREW = '\u05d0';  
  548.     private static final char LAST_HEBREW = '\u05ea';  
  549.   
  550.     /// The previous lines are provided and maintained by Mediatek Inc.  
  551.     public static class Token {  
  552.         /** 
  553.          * Separator between target string for each source char 
  554.          */  
  555.         public static final String SEPARATOR = " ";  
  556.   
  557.         public static final int LATIN = 1;  
  558.         public static final int PINYIN = 2;  
  559.         public static final int UNKNOWN = 3;  
  560.   
  561.         /// The following lines are provided and maintained by Mediatek Inc.  
  562.         // New Feature ALPS00338325.  
  563.         public static final int RUSSIAN = 14;  
  564.         public static final int ARABIC = 15;  
  565.         public static final int HEBREW = 16;  
  566.   
  567.         /// The previous lines are provided and maintained by Mediatek Inc.  
  568.         public Token() {  
  569.         }  
  570.   
  571.         public Token(int type, String source, String target) {  
  572.             this.type = type;  
  573.             this.source = source;  
  574.             this.target = target;  
  575.         }  
  576.   
  577.         /** 
  578.          * Type of this token, ASCII, PINYIN or UNKNOWN. 
  579.          */  
  580.         public int type;  
  581.         /** 
  582.          * Original string before translation. 
  583.          */  
  584.         public String source;  
  585.         /** 
  586.          * Translated string of source. For Han, target is corresponding Pinyin. Otherwise target is 
  587.          * original string in source. 
  588.          */  
  589.         public String target;  
  590.   
  591.         /** M: Second Pinyin of Hanzi @{ */  
  592.         public String secondTarget;  
  593.         /**@}*/  
  594.     }  
  595.   
  596.     protected HanziToPinyin(boolean hasChinaCollator) {  
  597.         mHasChinaCollator = hasChinaCollator;  
  598.     }  
  599.   
  600.     public static HanziToPinyin getInstance() {  
  601.         synchronized (HanziToPinyin.class) {  
  602.             if (sInstance != null) {  
  603.                 return sInstance;  
  604.             }  
  605.             // Check if zh_CN collation data is available  
  606.             final Locale locale[] = Collator.getAvailableLocales();  
  607.             for (int i = 0; i < locale.length; i++) {  
  608.                 if (locale[i].equals(Locale.CHINA)) {  
  609.                     // Do self validation just once.  
  610.                     if (DEBUG) {  
  611.                         Log.d(TAG, "Self validation. Result: " + doSelfValidation());  
  612.                     }  
  613.                     sInstance = new HanziToPinyin(true);  
  614.                     return sInstance;  
  615.                 }  
  616.             }  
  617.             Log.w(TAG, "There is no Chinese collator, HanziToPinyin is disabled");  
  618.             sInstance = new HanziToPinyin(false);  
  619.             return sInstance;  
  620.         }  
  621.     }  
  622.   
  623.     /** 
  624.      * Validate if our internal table has some wrong value. 
  625.      * 
  626.      * @return true when the table looks correct. 
  627.      */  
  628.     private static boolean doSelfValidation() {  
  629.         char lastChar = UNIHANS[0];  
  630.         String lastString = Character.toString(lastChar);  
  631.         for (char c : UNIHANS) {  
  632.             if (lastChar == c) {  
  633.                 continue;  
  634.             }  
  635.             final String curString = Character.toString(c);  
  636.             int cmp = COLLATOR.compare(lastString, curString);  
  637.             if (cmp >= 0) {  
  638.                 Log.e(TAG, "Internal error in Unihan table. " + "The last string \"" + lastString  
  639.                         + "\" is greater than current string \"" + curString + "\".");  
  640.                 return false;  
  641.             }  
  642.             lastString = curString;  
  643.         }  
  644.         return true;  
  645.     }  
  646.   
  647.     private Token getToken(char character) {  
  648.         Token token = new Token();  
  649.         final String letter = Character.toString(character);  
  650.         token.source = letter;  
  651.         int offset = -1;  
  652.         int cmp;  
  653.         if (character < 256) {  
  654.             token.type = Token.LATIN;  
  655.             token.target = letter;  
  656.             return token;  
  657.         } else {  
  658.             cmp = COLLATOR.compare(letter, FIRST_PINYIN_UNIHAN);  
  659.             if (cmp < 0) {  
  660.                 token.type = Token.UNKNOWN;  
  661.                 token.target = letter;  
  662.                 return token;  
  663.             } else if (cmp == 0) {  
  664.                 token.type = Token.PINYIN;  
  665.                 offset = 0;  
  666.             } else {  
  667.                 cmp = COLLATOR.compare(letter, LAST_PINYIN_UNIHAN);  
  668.                 if (cmp > 0) {  
  669.                     token.type = Token.UNKNOWN;  
  670.                     token.target = letter;  
  671.                     return token;  
  672.                 } else if (cmp == 0) {  
  673.                     token.type = Token.PINYIN;  
  674.                     offset = UNIHANS.length - 1;  
  675.                 }  
  676.             }  
  677.         }  
  678.   
  679.         token.type = Token.PINYIN;  
  680.         if (offset < 0) {  
  681.             int begin = 0;  
  682.             int end = UNIHANS.length - 1;  
  683.             while (begin <= end) {  
  684.                 offset = (begin + end) / 2;  
  685.                 final String unihan = Character.toString(UNIHANS[offset]);  
  686.                 cmp = COLLATOR.compare(letter, unihan);  
  687.                 if (cmp == 0) {  
  688.                     break;  
  689.                 } else if (cmp > 0) {  
  690.                     begin = offset + 1;  
  691.                 } else {  
  692.                     end = offset - 1;  
  693.                 }  
  694.             }  
  695.         }  
  696.         if (cmp < 0) {  
  697.             offset--;  
  698.         }  
  699.         StringBuilder pinyin = new StringBuilder();  
  700.         for (int j = 0; j < PINYINS[offset].length && PINYINS[offset][j] != 0; j++) {  
  701.             pinyin.append((char) PINYINS[offset][j]);  
  702.         }  
  703.         token.target = pinyin.toString();  
  704.         if (TextUtils.isEmpty(token.target)) {  
  705.             token.type = Token.UNKNOWN;  
  706.             token.target = token.source;  
  707.         }  
  708.         return token;  
  709.     }  
  710.   
  711.     /** 
  712.      * M: find mutiple Pinyin of Hanzi 
  713.      * 
  714.      * @param Hanzi character 
  715.      * @return Token 
  716.      */  
  717.     private Token getSubToken(char character) {  
  718.         Token token = getToken(character);   
  719.         //Log.d(TAG, "[getSubToken] target string: " + Integer.toHexString(character));  
  720.         if (specialHanzi.get(Integer.toHexString(character)) != null) {  
  721.             character = specialHanzi.get(Integer.toHexString(character));  
  722.             Token subToken = getToken(character);    
  723.             token.secondTarget = subToken.target;  
  724.         } else {  
  725.             token.secondTarget = null;  
  726.         }  
  727.         return token;  
  728.     }  
  729.   
  730.     /** 
  731.      * Convert the input to a array of tokens. The sequence of ASCII or Unknown characters without 
  732.      * space will be put into a Token, One Hanzi character which has pinyin will be treated as a 
  733.      * Token. If these is no China collator, the empty token array is returned. 
  734.      */  
  735.     public ArrayList<Token> get(final String input) {  
  736.         ArrayList<Token> tokens = new ArrayList<Token>();  
  737.         if (!mHasChinaCollator || TextUtils.isEmpty(input)) {  
  738.             // return empty tokens.  
  739.             return tokens;  
  740.         }  
  741.         final int inputLength = input.length();  
  742.         final StringBuilder sb = new StringBuilder();  
  743.         int tokenType = Token.LATIN;  
  744.         // Go through the input, create a new token when  
  745.         // a. Token type changed  
  746.         // b. Get the Pinyin of current charater.  
  747.         // c. current character is space.  
  748.         for (int i = 0; i < inputLength; i++) {  
  749.             final char character = input.charAt(i);  
  750.             if (character == ' ') {  
  751.                 if (sb.length() > 0) {  
  752.                     addToken(sb, tokens, tokenType);  
  753.                 }  
  754.             } else if (character < 256) {  
  755.                 if (tokenType != Token.LATIN && sb.length() > 0) {  
  756.                     addToken(sb, tokens, tokenType);  
  757.                 }  
  758.                 tokenType = Token.LATIN;  
  759.                 sb.append(character);  
  760.             } else {  
  761.                 /** M: find mutiple Pinyin of Hanzi @{ */  
  762.                 Token t = getSubToken(character);  
  763.                 /**@}*/  
  764.                 if (t.type == Token.PINYIN) {  
  765.                     if (sb.length() > 0) {  
  766.                         addToken(sb, tokens, tokenType);  
  767.                     }  
  768.                     tokens.add(t);  
  769.                     tokenType = Token.PINYIN;  
  770.                 } else {  
  771.                     if (tokenType != t.type && sb.length() > 0) {  
  772.                         addToken(sb, tokens, tokenType);  
  773.                     }  
  774.                     tokenType = t.type;  
  775.                     sb.append(character);  
  776.                 }  
  777.             }  
  778.         }  
  779.         if (sb.length() > 0) {  
  780.             addToken(sb, tokens, tokenType);  
  781.         }  
  782.         return tokens;  
  783.     }  
  784.   
  785.     private void addToken(  
  786.             final StringBuilder sb, final ArrayList<Token> tokens, final int tokenType) {  
  787.         String str = sb.toString();  
  788.         tokens.add(new Token(tokenType, str, str));  
  789.         sb.setLength(0);  
  790.     }  
  791.       
  792.   
  793.     //The fillowing lines are provided and maintained by Mediatek inc.  
  794.     private class DialerSearchToken extends Token {  
  795.         static final int FIRSTCASE = 0;  
  796.         static final int UPPERCASE = 1;  
  797.         static final int LOWERCASE = 2;  
  798.     }  
  799.       
  800.     public String getTokensForDialerSearch(final String input, StringBuilder offsets){  
  801.           
  802.         if(offsets == null || input == null || TextUtils.isEmpty(input)){  
  803.             // return empty tokens  
  804.             return null;  
  805.         }  
  806.           
  807.         StringBuilder subStrSet = new StringBuilder();  
  808.         ArrayList<Token> tokens = new ArrayList<Token>();  
  809.         ArrayList<String> shortSubStrOffset = new ArrayList<String>();  
  810.         final int inputLength = input.length();  
  811.         final StringBuilder subString = new StringBuilder();  
  812.         final StringBuilder subStrOffset = new StringBuilder();  
  813.         int tokenType = Token.LATIN;  
  814.         int caseTypePre = DialerSearchToken.FIRSTCASE;  
  815.         int caseTypeCurr = DialerSearchToken.UPPERCASE;  
  816.         int mPos = 0;  
  817.           
  818.         // Go through the input, create a new token when  
  819.         // a. Token type changed  
  820.         // b. Get the Pinyin of current charater.  
  821.         // c. current character is space.  
  822.         // d. Token case changed from lower case to upper case,   
  823.         // e. the first character is always a separated one  
  824.         // f character == '+' || character == '#' || character == '*' || character == ',' || character == ';'  
  825.         for (int i = 0; i < inputLength; i++) {  
  826.             final char character = input.charAt(i);  
  827.             if (character == '-' || character == ',' ){  
  828.                 mPos++;  
  829.             } else if (character == ' ') {  
  830.                 if (subString.length() > 0) {  
  831.                     addToken(subString, tokens, tokenType);  
  832.                     addOffsets(subStrOffset, shortSubStrOffset);  
  833.                 }  
  834.                 addSubString(tokens,shortSubStrOffset,subStrSet,offsets);  
  835.                 mPos++;  
  836.                 caseTypePre = DialerSearchToken.FIRSTCASE;  
  837.             } else if (character < 256) {  
  838.                 if (tokenType != Token.LATIN && subString.length() > 0) {  
  839.                     addToken(subString, tokens, tokenType);  
  840.                     addOffsets(subStrOffset, shortSubStrOffset);  
  841.                  }  
  842.                 caseTypeCurr = (character>='A' && character<='Z')?DialerSearchToken.UPPERCASE:DialerSearchToken.LOWERCASE;  
  843.                 if(caseTypePre == DialerSearchToken.LOWERCASE && caseTypeCurr == DialerSearchToken.UPPERCASE){  
  844.                     addToken(subString, tokens, tokenType);  
  845.                     addOffsets(subStrOffset, shortSubStrOffset);  
  846.                 }  
  847.                 caseTypePre = caseTypeCurr;   
  848.                 tokenType = Token.LATIN;  
  849.                 Character c = Character.toUpperCase(character);  
  850.                 if(c != null){  
  851.                     subString.append(c);  
  852.                     subStrOffset.append((char)mPos);  
  853.                 }  
  854.                 mPos++;  
  855.                 // / The following lines are provided and maintained by Mediatek Inc.New Feature ALPS00338325.  
  856.                 // if character is Russian  
  857.                 /** M: add special character for Russian */  
  858.             } else if (isSpecialRussianCharacter(character)  
  859.                     || (character >= FIRST_RUSSIAN_UPPER && character <= LAST_RUSSIAN_LOWER)) {  
  860.                 // if the pre character is not Russian, sub string from current character.  
  861.                 if (tokenType != Token.RUSSIAN && subString.length() > 0) {  
  862.                     addToken(subString, tokens, tokenType);  
  863.                     addOffsets(subStrOffset, shortSubStrOffset);  
  864.                 }  
  865.                 // current character is upper or lower.  
  866.                 /** M: add special character for Russian */  
  867.                 caseTypeCurr = (isUpperCaseSpecialRussianChar(character) || (character >= FIRST_RUSSIAN_UPPER && character <= LAST_RUSSIAN_UPPER)) ? DialerSearchToken.UPPERCASE  
  868.                         : DialerSearchToken.LOWERCASE;  
  869.                 // if current character is upper and pre character is lower, sub string like English.  
  870.                 if (caseTypePre == DialerSearchToken.LOWERCASE  
  871.                         && caseTypeCurr == DialerSearchToken.UPPERCASE) {  
  872.                     addToken(subString, tokens, tokenType);  
  873.                     addOffsets(subStrOffset, shortSubStrOffset);  
  874.                 }  
  875.                 // set caseTypePre and tokenType for next "for" circle.  
  876.                 caseTypePre = caseTypeCurr;  
  877.                 tokenType = Token.RUSSIAN;  
  878.                 // set the number of the character in subString, not the Russian character.  
  879.                 Character c = sMuiSupportMap.get(character);  
  880.                 if (c != null) {  
  881.                     subString.append(c);  
  882.                     subStrOffset.append((char) mPos);  
  883.                 }  
  884.                 mPos++;  
  885.                 // if character is Arabic  
  886.             } else if (character >= FIRST_ARABIC && character <= LAST_ARABIC) {  
  887.                 // if the pre character is not Arabic, sub string from current character.  
  888.                 if (tokenType != Token.ARABIC && subString.length() > 0) {  
  889.                     addToken(subString, tokens, tokenType);  
  890.                     addOffsets(subStrOffset, shortSubStrOffset);  
  891.                 }  
  892.   
  893.                 caseTypePre = caseTypeCurr;  
  894.                 tokenType = Token.ARABIC;  
  895.                 Character c = sMuiSupportMap.get(character);  
  896.                 if (c != null) {  
  897.                     subString.append(c);  
  898.                     subStrOffset.append((char) mPos);  
  899.                 }  
  900.                 mPos++;  
  901.                 // if character is Hebrew  
  902.             } else if (character >= FIRST_HEBREW && character <= LAST_HEBREW) {  
  903.                 // if the pre character is not Hebrew, sub string from current character.  
  904.                 if (tokenType != Token.HEBREW && subString.length() > 0) {  
  905.                     addToken(subString, tokens, tokenType);  
  906.                     addOffsets(subStrOffset, shortSubStrOffset);  
  907.                 }  
  908.   
  909.                 caseTypePre = caseTypeCurr;  
  910.                 tokenType = Token.HEBREW;  
  911.                 Character c = sMuiSupportMap.get(character);  
  912.                 if (c != null) {  
  913.                     subString.append(c);  
  914.                     subStrOffset.append((char) mPos);  
  915.                 }  
  916.                 mPos++;  
  917.                 // The previous lines are provided and maintained by Mediatek Inc.  
  918.                 // TODO Check  
  919.             //} else if (character < FIRST_UNIHAN) {  
  920.             //    mPos++;  
  921.             } else {  
  922.                 Token t = getToken(character);  
  923.                 int tokenSize = t.target.length();  
  924.                 //Current type is PINYIN  
  925.                 if (t.type == Token.PINYIN) {  
  926.                     if (subString.length() > 0) {  
  927.                         addToken(subString, tokens, tokenType);  
  928.                         addOffsets(subStrOffset, shortSubStrOffset);  
  929.                     }  
  930.                     tokens.add(t);  
  931.                     for(int j=0; j < tokenSize;j++)  
  932.                         subStrOffset.append((char)mPos);  
  933.                     addOffsets(subStrOffset,shortSubStrOffset);  
  934.                     tokenType = Token.PINYIN;  
  935.                     caseTypePre = DialerSearchToken.FIRSTCASE;  
  936.                     mPos++;  
  937.                 } else {  
  938.                     mPos++;  
  939.                 }  
  940.             }  
  941.             //IF the name string is too long, cut it off to meet the storage request of dialer search.  
  942.             if(mPos > 127)  
  943.                 break;  
  944.         }  
  945.         if (subString.length() > 0) {  
  946.             addToken(subString, tokens, tokenType);  
  947.             addOffsets(subStrOffset, shortSubStrOffset);  
  948.         }  
  949.         addSubString(tokens,shortSubStrOffset,subStrSet,offsets);  
  950.         return subStrSet.toString();  
  951.     }  
  952.       
  953.     /** 
  954.      * M:whether the character is special Russian character? 
  955.      * 
  956.      * @param specialchar 
  957.      * @return true: special character, false: not special. 
  958.      */  
  959.     private boolean isSpecialRussianCharacter(final char specialchar) {  
  960.         return (SPECIAL_CHARS_LIST_UPPER.contains(specialchar) || SPECIAL_CHARS_LIST_LOWER  
  961.                 .contains(specialchar));  
  962.     }  
  963.   
  964.     /** 
  965.      * M:Judge the special Russian character is Upper case? 
  966.      * 
  967.      * @param specialchar 
  968.      * @return true: Upper case, false: Lower case 
  969.      */  
  970.     private boolean isUpperCaseSpecialRussianChar(final char specialchar) {  
  971.        return SPECIAL_CHARS_LIST_UPPER.contains(specialchar);  
  972.     }  
  973.   
  974.     private void addOffsets(final StringBuilder sb, final ArrayList<String> shortSubStrOffset){  
  975.         String str = sb.toString();  
  976.         shortSubStrOffset.add(str);  
  977.         sb.setLength(0);  
  978.     }  
  979.       
  980.     private void addSubString(final ArrayList<Token> tokens, final ArrayList<String> shortSubStrOffset,  
  981.                             StringBuilder subStrSet, StringBuilder offsets){  
  982.         if(tokens == null || tokens.isEmpty())  
  983.             return;  
  984.           
  985.         int size = tokens.size();  
  986.         int len = 0;  
  987.         StringBuilder mShortSubStr = new StringBuilder();  
  988.         StringBuilder mShortSubStrOffsets = new StringBuilder();  
  989.         StringBuilder mShortSubStrSet = new StringBuilder();  
  990.         StringBuilder mShortSubStrOffsetsSet = new StringBuilder();  
  991.           
  992.         for(int i=size-1; i>=0 ; i--){  
  993.             String mTempStr = tokens.get(i).target;  
  994.             len += mTempStr.length();  
  995.             String mTempOffset = shortSubStrOffset.get(i);  
  996.             if(mShortSubStr.length()>0){  
  997.                 mShortSubStr.deleteCharAt(0);  
  998.                 mShortSubStrOffsets.deleteCharAt(0);  
  999.             }  
  1000.             mShortSubStr.insert(0, mTempStr);  
  1001.             mShortSubStr.insert(0,(char)len);  
  1002.             mShortSubStrOffsets.insert(0,mTempOffset);  
  1003.             mShortSubStrOffsets.insert(0,(char)len);  
  1004.             mShortSubStrSet.insert(0,mShortSubStr);  
  1005.             mShortSubStrOffsetsSet.insert(0, mShortSubStrOffsets);  
  1006.         }  
  1007.           
  1008.         subStrSet.append(mShortSubStrSet);  
  1009.         offsets.append(mShortSubStrOffsetsSet);  
  1010.         tokens.clear();  
  1011.         shortSubStrOffset.clear();  
  1012.     }  
  1013.     /** M:Special character list for Russian @{ */  
  1014.     private static final ArrayList<Character> SPECIAL_CHARS_LIST_UPPER = new ArrayList<Character>();  
  1015.     private static final ArrayList<Character> SPECIAL_CHARS_LIST_LOWER = new ArrayList<Character>();  
  1016.     static {  
  1017.         //Upper list  
  1018.         SPECIAL_CHARS_LIST_UPPER.add('\u0401'); // 衼  
  1019.         //Lower list  
  1020.         SPECIAL_CHARS_LIST_LOWER.add('\u0451'); // 褢  
  1021.     }  
  1022.     /**@}*/  
  1023.     //The previous lines are provided and maintained by Mediatek inc.      
  1024. }  

使用时只需调用如下的方法


  1. public String hanziTopinyin(String hanzi){  
  2.         ArrayList<Token> tokens = HanziToPinyin.getInstance().get(hanzi);    
  3.         StringBuilder sb = new StringBuilder();    
  4.         if (tokens != null && tokens.size() > 0) {    
  5.             for (Token token : tokens) {    
  6.                 if (Token.PINYIN == token.type) {    
  7.                     sb.append(token.target);    
  8.                 } else {    
  9.                     sb.append(token.source);    
  10.                 }    
  11.             }    
  12.         }    
  13.         return sb.toString().toLowerCase();   
  14. }  
即可将汉字转成拼音。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值