将中文转换为对应的拼音字

  • 将中文字符转换为对应的拼音字符串的对象。
  • 对于有多音的汉字只选择一个
  • android Q 以上用系统的
  • 多音字处理不是太好


import android.icu.text.Transliterator
import android.os.Build
import android.text.TextUtils
import java.text.Collator
import java.util.Locale

/**
 * 将中文字符转换为对应的拼音字符串的对象。
 * 对于有多音的汉字只选择一个
 * android Q 以上用系统的
 * 高度依赖于zh_CN ICU排序数据,必须始终同步
 */
open class HanZiToPinYin private constructor(hasChinaCollator: Boolean) {
    private val hasChinaCollator: Boolean
    private var pinYinTrans: Transliterator? = null

    class Token {
        constructor()
        constructor(type: Int, source: String?, target: String?) {
            this.type = type
            this.source = source
            this.target = target
        }

        /**
         * 此令牌的类型,ASCII,拼音或未知。
         */
        var type = 0

        /**
         * 翻译前的原始字符串.
         */
        var source: String? = null

        /**
         * 源的翻译字符串。对于汉族,目标是对应的拼音。
         * 否则目标是源中的原始字符串。
         */
        var target: String? = null

        companion object {
            const val LATIN = 1
            const val PINYIN = 2
            const val UNKNOWN = 3
        }
    }

    init {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            try {
                pinYinTrans = Transliterator.getInstance("Han-Latin/Names; Latin-Ascii; Any-Upper")
            } catch (e: IllegalArgumentException) {
                LogUtil.d(" HanZiToPinYin is disabled: " + e.message)
            }
        }
        this.hasChinaCollator = hasChinaCollator
    }

    private fun getToken(character: Char): Token {
        val token = Token()
        val letter = character.toString()
        token.source = letter
        var offset = -1
        var cmp: Int
        val length = 256
        if (character.code < length) {
            token.type = Token.LATIN
            token.target = letter
            return token
        } else {
            cmp = COLLATOR.compare(letter, FIRST_PINYIN_UNI_HAN)
            if (cmp < 0) {
                token.type = Token.UNKNOWN
                token.target = letter
                return token
            } else if (cmp == 0) {
                offset = 0
            } else {
                cmp = COLLATOR.compare(letter, LAST_PINYIN_UNI_HAN)
                if (cmp > 0) {
                    token.type = Token.UNKNOWN
                    token.target = letter
                    return token
                } else if (cmp == 0) {
                    offset = UNI_HANS.size - 1
                }
            }
        }
        token.type = Token.PINYIN
        if (offset < 0) {
            var begin = 0
            var end = UNI_HANS.size - 1
            while (begin <= end) {
                offset = (begin + end) / 2
                val hanZi = UNI_HANS[offset].toString()
                cmp = COLLATOR.compare(letter, hanZi)
                if (cmp == 0) {
                    break
                } else if (cmp > 0) {
                    begin = offset + 1
                } else {
                    end = offset - 1
                }
            }
        }
        if (cmp < 0) {
            offset--
        }
        val pinyin = StringBuilder()
        var j = 0
        while (j < PIN_YIN[offset].size && PIN_YIN[offset][j].toInt() != 0) {
            pinyin.append(Char(PIN_YIN[offset][j].toUShort()))
            j++
        }
        token.target = pinyin.toString()
        if (TextUtils.isEmpty(token.target)) {
            token.type = Token.UNKNOWN
            token.target = token.source
        }
        return token
    }

    /**
     * 将输入转换为令牌数组。ASCII 或未知的序列
     * 没有空格的字符将被放入令牌中,一个汉字字符
     * 有拼音的将被视为令牌。如果这些不是中国
     * 收集人,返回空令牌数组。
     */
    operator fun get(input: String): ArrayList<Token> {
        val tokens = ArrayList<Token>()
        if (!hasChinaCollator || TextUtils.isEmpty(input)) {
            // 返回空令牌。
            return tokens
        }
        val inputLength = input.length
        val sb = StringBuilder()
        var tokenType = Token.LATIN
        // 遍历输入,在以下情况下创建一个新令牌
        // a. 令牌类型已更改
        // b.获取当前字符的拼音。
        // c. 当前字符为空格.
        for (i in 0 until inputLength) {
            val character = input[i]
            if (character == ' ') {
                if (sb.isNotEmpty()) {
                    addToken(sb, tokens, tokenType)
                }
            } else if (character.code < 256) {
                if (tokenType != Token.LATIN && sb.isNotEmpty()) {
                    addToken(sb, tokens, tokenType)
                }
                tokenType = Token.LATIN
                sb.append(character)
            } else {
                val t = getToken(character)
                if (t.type == Token.PINYIN) {
                    if (sb.isNotEmpty()) {
                        addToken(sb, tokens, tokenType)
                    }
                    tokens.add(t)
                    tokenType = Token.PINYIN
                } else {
                    if (tokenType != t.type && sb.isNotEmpty()) {
                        addToken(sb, tokens, tokenType)
                    }
                    tokenType = t.type
                    sb.append(character)
                }
            }
        }
        if (sb.isNotEmpty()) {
            addToken(sb, tokens, tokenType)
        }
        return tokens
    }

    private fun addToken(sb: StringBuilder, tokens: ArrayList<Token>, tokenType: Int) {
        val str = sb.toString()
        tokens.add(Token(tokenType, str, str))
        sb.setLength(0)
    }

    /**
     * 输入汉字返回拼音的通用方法函数。
     *
     * @param hanZi 输入文本
     */
    fun transliterate(hanZi: String): String {
        if (TextUtils.isEmpty(hanZi)) {
            return hanZi
        }
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            if (pinYinTrans != null) {
                pinYinTrans?.transliterate(hanZi) ?: ""
            } else {
                getPinYin(hanZi)
            }
        } else {
            getPinYin(hanZi)
        }
    }

    /**
     * 输入汉字返回拼音的通用方法函数。
     *
     * @param hanZi 输入文本
     */
    private fun getPinYin(hanZi: String?): String {
        if (hanZi.isNullOrEmpty()) {
            return ""
        }
        val tokens = get(hanZi)
        val sb = StringBuilder()
        if (tokens.size > 0) {
            for (token in tokens) {
                if (Token.PINYIN == token.type) {
                    sb.append(token.target)
                } else {
                    sb.append(token.source)
                }
            }
        }
        return sb.toString()
    }

    companion object {
        /**
         * Uni_hans数组。
         *
         *
         * 当zh_CN整理器时,每个 unihans 都是同一拼音中的第一个。
         */
        val UNI_HANS = charArrayOf(
            '\u963f',
            '\u54ce',
            '\u5b89',
            '\u80ae',
            '\u51f9',
            '\u516b',
            '\u6300',
            '\u6273',
            '\u90a6',
            '\u52f9',
            '\u9642',
            '\u5954',
            '\u4f3b',
            '\u5c44',
            '\u8fb9',
            '\u706c',
            '\u618b',
            '\u6c43',
            '\u51ab',
            '\u7676',
            '\u5cec',
            '\u5693',
            '\u5072',
            '\u53c2',
            '\u4ed3',
            '\u64a1',
            '\u518a',
            '\u5d7e',
            '\u66fd',
            '\u66fe',
            '\u5c64',
            '\u53c9',
            '\u8286',
            '\u8fbf',
            '\u4f25',
            '\u6284',
            '\u8f66',
            '\u62bb',
            '\u6c88',
            '\u6c89',
            '\u9637',
            '\u5403',
            '\u5145',
            '\u62bd',
            '\u51fa',
            '\u6b3b',
            '\u63e3',
            '\u5ddb',
            '\u5205',
            '\u5439',
            '\u65fe',
            '\u9034',
            '\u5472',
            '\u5306',
            '\u51d1',
            '\u7c97',
            '\u6c46',
            '\u5d14',
            '\u90a8',
            '\u6413',
            '\u5491',
            '\u5446',
            '\u4e39',
            '\u5f53',
            '\u5200',
            '\u561a',
            '\u6265',
            '\u706f',
            '\u6c10',
            '\u55f2',
            '\u7538',
            '\u5201',
            '\u7239',
            '\u4e01',
            '\u4e1f',
            '\u4e1c',
            '\u543a',
            '\u53be',
            '\u8011',
            '\u8968',
            '\u5428',
            '\u591a',
            '\u59b8',
            '\u8bf6',
            '\u5940',
            '\u97a5',
            '\u513f',
            '\u53d1',
            '\u5e06',
            '\u531a',
            '\u98de',
            '\u5206',
            '\u4e30',
            '\u8985',
            '\u4ecf',
            '\u7d11',
            '\u4f15',
            '\u65ee',
            '\u4f85',
            '\u7518',
            '\u5188',
            '\u768b',
            '\u6208',
            '\u7ed9',
            '\u6839',
            '\u522f',
            '\u5de5',
            '\u52fe',
            '\u4f30',
            '\u74dc',
            '\u4e56',
            '\u5173',
            '\u5149',
            '\u5f52',
            '\u4e28',
            '\u5459',
            '\u54c8',
            '\u548d',
            '\u4f44',
            '\u592f',
            '\u8320',
            '\u8bc3',
            '\u9ed2',
            '\u62eb',
            '\u4ea8',
            '\u5677',
            '\u53ff',
            '\u9f41',
            '\u4e6f',
            '\u82b1',
            '\u6000',
            '\u72bf',
            '\u5ddf',
            '\u7070',
            '\u660f',
            '\u5419',
            '\u4e0c',
            '\u52a0',
            '\u620b',
            '\u6c5f',
            '\u827d',
            '\u9636',
            '\u5dfe',
            '\u5755',
            '\u5182',
            '\u4e29',
            '\u51e5',
            '\u59e2',
            '\u5658',
            '\u519b',
            '\u5494',
            '\u5f00',
            '\u520a',
            '\u5ffc',
            '\u5c3b',
            '\u533c',
            '\u808e',
            '\u52a5',
            '\u7a7a',
            '\u62a0',
            '\u625d',
            '\u5938',
            '\u84af',
            '\u5bbd',
            '\u5321',
            '\u4e8f',
            '\u5764',
            '\u6269',
            '\u5783',
            '\u6765',
            '\u5170',
            '\u5577',
            '\u635e',
            '\u808b',
            '\u52d2',
            '\u5d1a',
            '\u5215',
            '\u4fe9',
            '\u5941',
            '\u826f',
            '\u64a9',
            '\u5217',
            '\u62ce',
            '\u5222',
            '\u6e9c',
            '\u56d6',
            '\u9f99',
            '\u779c',
            '\u565c',
            '\u5a08',
            '\u7567',
            '\u62a1',
            '\u7f57',
            '\u5463',
            '\u5988',
            '\u57cb',
            '\u5ada',
            '\u7264',
            '\u732b',
            '\u4e48',
            '\u5445',
            '\u95e8',
            '\u753f',
            '\u54aa',
            '\u5b80',
            '\u55b5',
            '\u4e5c',
            '\u6c11',
            '\u540d',
            '\u8c2c',
            '\u6478',
            '\u54de',
            '\u6bea',
            '\u55ef',
            '\u62cf',
            '\u8149',
            '\u56e1',
            '\u56d4',
            '\u5b6c',
            '\u7592',
            '\u5a1e',
            '\u6041',
            '\u80fd',
            '\u59ae',
            '\u62c8',
            '\u5b22',
            '\u9e1f',
            '\u634f',
            '\u56dc',
            '\u5b81',
            '\u599e',
            '\u519c',
            '\u7fba',
            '\u5974',
            '\u597b',
            '\u759f',
            '\u9ec1',
            '\u90cd',
            '\u5594',
            '\u8bb4',
            '\u5991',
            '\u62cd',
            '\u7705',
            '\u4e53',
            '\u629b',
            '\u5478',
            '\u55b7',
            '\u5309',
            '\u4e15',
            '\u56e8',
            '\u527d',
            '\u6c15',
            '\u59d8',
            '\u4e52',
            '\u948b',
            '\u5256',
            '\u4ec6',
            '\u4e03',
            '\u6390',
            '\u5343',
            '\u545b',
            '\u6084',
            '\u767f',
            '\u4eb2',
            '\u72c5',
            '\u828e',
            '\u4e18',
            '\u533a',
            '\u5cd1',
            '\u7f3a',
            '\u590b',
            '\u5465',
            '\u7a63',
            '\u5a06',
            '\u60f9',
            '\u4eba',
            '\u6254',
            '\u65e5',
            '\u8338',
            '\u53b9',
            '\u909a',
            '\u633c',
            '\u5827',
            '\u5a51',
            '\u77a4',
            '\u637c',
            '\u4ee8',
            '\u6be2',
            '\u4e09',
            '\u6852',
            '\u63bb',
            '\u95aa',
            '\u68ee',
            '\u50e7',
            '\u6740',
            '\u7b5b',
            '\u5c71',
            '\u4f24',
            '\u5f30',
            '\u5962',
            '\u7533',
            '\u8398',
            '\u6552',
            '\u5347',
            '\u5c38',
            '\u53ce',
            '\u4e66',
            '\u5237',
            '\u8870',
            '\u95e9',
            '\u53cc',
            '\u8c01',
            '\u542e',
            '\u8bf4',
            '\u53b6',
            '\u5fea',
            '\u635c',
            '\u82cf',
            '\u72fb',
            '\u590a',
            '\u5b59',
            '\u5506',
            '\u4ed6',
            '\u56fc',
            '\u574d',
            '\u6c64',
            '\u5932',
            '\u5fd1',
            '\u71a5',
            '\u5254',
            '\u5929',
            '\u65eb',
            '\u5e16',
            '\u5385',
            '\u56f2',
            '\u5077',
            '\u51f8',
            '\u6e4d',
            '\u63a8',
            '\u541e',
            '\u4e47',
            '\u7a75',
            '\u6b6a',
            '\u5f2f',
            '\u5c23',
            '\u5371',
            '\u6637',
            '\u7fc1',
            '\u631d',
            '\u4e4c',
            '\u5915',
            '\u8672',
            '\u4eda',
            '\u4e61',
            '\u7071',
            '\u4e9b',
            '\u5fc3',
            '\u661f',
            '\u51f6',
            '\u4f11',
            '\u5401',
            '\u5405',
            '\u524a',
            '\u5743',
            '\u4e2b',
            '\u6079',
            '\u592e',
            '\u5e7a',
            '\u503b',
            '\u4e00',
            '\u56d9',
            '\u5e94',
            '\u54df',
            '\u4f63',
            '\u4f18',
            '\u625c',
            '\u56e6',
            '\u66f0',
            '\u6655',
            '\u7b60',
            '\u7b7c',
            '\u5e00',
            '\u707d',
            '\u5142',
            '\u5328',
            '\u50ae',
            '\u5219',
            '\u8d3c',
            '\u600e',
            '\u5897',
            '\u624e',
            '\u635a',
            '\u6cbe',
            '\u5f20',
            '\u957f',
            '\u9577',
            '\u4f4b',
            '\u8707',
            '\u8d1e',
            '\u4e89',
            '\u4e4b',
            '\u5cd9',
            '\u5ea2',
            '\u4e2d',
            '\u5dde',
            '\u6731',
            '\u6293',
            '\u62fd',
            '\u4e13',
            '\u5986',
            '\u96b9',
            '\u5b92',
            '\u5353',
            '\u4e72',
            '\u5b97',
            '\u90b9',
            '\u79df',
            '\u94bb',
            '\u539c',
            '\u5c0a',
            '\u6628',
            '\u5159',
            '\u9fc3',
            '\u9fc4'
        )

        /**
         * 拼音数组。
         *
         *
         * 每个拼音对应单音中偏移量相同的 unihans
         * 数组。
         */
        val PIN_YIN = arrayOf(
            byteArrayOf(65, 0, 0, 0, 0, 0),
            byteArrayOf(65, 73, 0, 0, 0, 0),
            byteArrayOf(65, 78, 0, 0, 0, 0),
            byteArrayOf(65, 78, 71, 0, 0, 0),
            byteArrayOf(65, 79, 0, 0, 0, 0),
            byteArrayOf(66, 65, 0, 0, 0, 0),
            byteArrayOf(66, 65, 73, 0, 0, 0),
            byteArrayOf(66, 65, 78, 0, 0, 0),
            byteArrayOf(66, 65, 78, 71, 0, 0),
            byteArrayOf(66, 65, 79, 0, 0, 0),
            byteArrayOf(66, 69, 73, 0, 0, 0),
            byteArrayOf(66, 69, 78, 0, 0, 0),
            byteArrayOf(66, 69, 78, 71, 0, 0),
            byteArrayOf(66, 73, 0, 0, 0, 0),
            byteArrayOf(66, 73, 65, 78, 0, 0),
            byteArrayOf(66, 73, 65, 79, 0, 0),
            byteArrayOf(66, 73, 69, 0, 0, 0),
            byteArrayOf(66, 73, 78, 0, 0, 0),
            byteArrayOf(66, 73, 78, 71, 0, 0),
            byteArrayOf(66, 79, 0, 0, 0, 0),
            byteArrayOf(66, 85, 0, 0, 0, 0),
            byteArrayOf(67, 65, 0, 0, 0, 0),
            byteArrayOf(67, 65, 73, 0, 0, 0),
            byteArrayOf(67, 65, 78, 0, 0, 0),
            byteArrayOf(67, 65, 78, 71, 0, 0),
            byteArrayOf(67, 65, 79, 0, 0, 0),
            byteArrayOf(67, 69, 0, 0, 0, 0),
            byteArrayOf(67, 69, 78, 0, 0, 0),
            byteArrayOf(67, 69, 78, 71, 0, 0),
            byteArrayOf(90, 69, 78, 71, 0, 0),
            byteArrayOf(67, 69, 78, 71, 0, 0),
            byteArrayOf(67, 72, 65, 0, 0, 0),
            byteArrayOf(67, 72, 65, 73, 0, 0),
            byteArrayOf(67, 72, 65, 78, 0, 0),
            byteArrayOf(67, 72, 65, 78, 71, 0),
            byteArrayOf(67, 72, 65, 79, 0, 0),
            byteArrayOf(67, 72, 69, 0, 0, 0),
            byteArrayOf(67, 72, 69, 78, 0, 0),
            byteArrayOf(83, 72, 69, 78, 0, 0),
            byteArrayOf(67, 72, 69, 78, 0, 0),
            byteArrayOf(67, 72, 69, 78, 71, 0),
            byteArrayOf(67, 72, 73, 0, 0, 0),
            byteArrayOf(67, 72, 79, 78, 71, 0),
            byteArrayOf(67, 72, 79, 85, 0, 0),
            byteArrayOf(67, 72, 85, 0, 0, 0),
            byteArrayOf(67, 72, 85, 65, 0, 0),
            byteArrayOf(67, 72, 85, 65, 73, 0),
            byteArrayOf(67, 72, 85, 65, 78, 0),
            byteArrayOf(67, 72, 85, 65, 78, 71),
            byteArrayOf(67, 72, 85, 73, 0, 0),
            byteArrayOf(67, 72, 85, 78, 0, 0),
            byteArrayOf(67, 72, 85, 79, 0, 0),
            byteArrayOf(67, 73, 0, 0, 0, 0),
            byteArrayOf(67, 79, 78, 71, 0, 0),
            byteArrayOf(67, 79, 85, 0, 0, 0),
            byteArrayOf(67, 85, 0, 0, 0, 0),
            byteArrayOf(67, 85, 65, 78, 0, 0),
            byteArrayOf(67, 85, 73, 0, 0, 0),
            byteArrayOf(67, 85, 78, 0, 0, 0),
            byteArrayOf(67, 85, 79, 0, 0, 0),
            byteArrayOf(68, 65, 0, 0, 0, 0),
            byteArrayOf(68, 65, 73, 0, 0, 0),
            byteArrayOf(68, 65, 78, 0, 0, 0),
            byteArrayOf(68, 65, 78, 71, 0, 0),
            byteArrayOf(68, 65, 79, 0, 0, 0),
            byteArrayOf(68, 69, 0, 0, 0, 0),
            byteArrayOf(68, 69, 78, 0, 0, 0),
            byteArrayOf(68, 69, 78, 71, 0, 0),
            byteArrayOf(68, 73, 0, 0, 0, 0),
            byteArrayOf(68, 73, 65, 0, 0, 0),
            byteArrayOf(68, 73, 65, 78, 0, 0),
            byteArrayOf(68, 73, 65, 79, 0, 0),
            byteArrayOf(68, 73, 69, 0, 0, 0),
            byteArrayOf(68, 73, 78, 71, 0, 0),
            byteArrayOf(68, 73, 85, 0, 0, 0),
            byteArrayOf(68, 79, 78, 71, 0, 0),
            byteArrayOf(68, 79, 85, 0, 0, 0),
            byteArrayOf(68, 85, 0, 0, 0, 0),
            byteArrayOf(68, 85, 65, 78, 0, 0),
            byteArrayOf(68, 85, 73, 0, 0, 0),
            byteArrayOf(68, 85, 78, 0, 0, 0),
            byteArrayOf(68, 85, 79, 0, 0, 0),
            byteArrayOf(69, 0, 0, 0, 0, 0),
            byteArrayOf(69, 73, 0, 0, 0, 0),
            byteArrayOf(69, 78, 0, 0, 0, 0),
            byteArrayOf(69, 78, 71, 0, 0, 0),
            byteArrayOf(69, 82, 0, 0, 0, 0),
            byteArrayOf(70, 65, 0, 0, 0, 0),
            byteArrayOf(70, 65, 78, 0, 0, 0),
            byteArrayOf(70, 65, 78, 71, 0, 0),
            byteArrayOf(70, 69, 73, 0, 0, 0),
            byteArrayOf(70, 69, 78, 0, 0, 0),
            byteArrayOf(70, 69, 78, 71, 0, 0),
            byteArrayOf(70, 73, 65, 79, 0, 0),
            byteArrayOf(70, 79, 0, 0, 0, 0),
            byteArrayOf(70, 79, 85, 0, 0, 0),
            byteArrayOf(70, 85, 0, 0, 0, 0),
            byteArrayOf(71, 65, 0, 0, 0, 0),
            byteArrayOf(71, 65, 73, 0, 0, 0),
            byteArrayOf(71, 65, 78, 0, 0, 0),
            byteArrayOf(71, 65, 78, 71, 0, 0),
            byteArrayOf(71, 65, 79, 0, 0, 0),
            byteArrayOf(71, 69, 0, 0, 0, 0),
            byteArrayOf(71, 69, 73, 0, 0, 0),
            byteArrayOf(71, 69, 78, 0, 0, 0),
            byteArrayOf(71, 69, 78, 71, 0, 0),
            byteArrayOf(71, 79, 78, 71, 0, 0),
            byteArrayOf(71, 79, 85, 0, 0, 0),
            byteArrayOf(71, 85, 0, 0, 0, 0),
            byteArrayOf(71, 85, 65, 0, 0, 0),
            byteArrayOf(71, 85, 65, 73, 0, 0),
            byteArrayOf(71, 85, 65, 78, 0, 0),
            byteArrayOf(71, 85, 65, 78, 71, 0),
            byteArrayOf(71, 85, 73, 0, 0, 0),
            byteArrayOf(71, 85, 78, 0, 0, 0),
            byteArrayOf(71, 85, 79, 0, 0, 0),
            byteArrayOf(72, 65, 0, 0, 0, 0),
            byteArrayOf(72, 65, 73, 0, 0, 0),
            byteArrayOf(72, 65, 78, 0, 0, 0),
            byteArrayOf(72, 65, 78, 71, 0, 0),
            byteArrayOf(72, 65, 79, 0, 0, 0),
            byteArrayOf(72, 69, 0, 0, 0, 0),
            byteArrayOf(72, 69, 73, 0, 0, 0),
            byteArrayOf(72, 69, 78, 0, 0, 0),
            byteArrayOf(72, 69, 78, 71, 0, 0),
            byteArrayOf(72, 77, 0, 0, 0, 0),
            byteArrayOf(72, 79, 78, 71, 0, 0),
            byteArrayOf(72, 79, 85, 0, 0, 0),
            byteArrayOf(72, 85, 0, 0, 0, 0),
            byteArrayOf(72, 85, 65, 0, 0, 0),
            byteArrayOf(72, 85, 65, 73, 0, 0),
            byteArrayOf(72, 85, 65, 78, 0, 0),
            byteArrayOf(72, 85, 65, 78, 71, 0),
            byteArrayOf(72, 85, 73, 0, 0, 0),
            byteArrayOf(72, 85, 78, 0, 0, 0),
            byteArrayOf(72, 85, 79, 0, 0, 0),
            byteArrayOf(74, 73, 0, 0, 0, 0),
            byteArrayOf(74, 73, 65, 0, 0, 0),
            byteArrayOf(74, 73, 65, 78, 0, 0),
            byteArrayOf(74, 73, 65, 78, 71, 0),
            byteArrayOf(74, 73, 65, 79, 0, 0),
            byteArrayOf(74, 73, 69, 0, 0, 0),
            byteArrayOf(74, 73, 78, 0, 0, 0),
            byteArrayOf(74, 73, 78, 71, 0, 0),
            byteArrayOf(74, 73, 79, 78, 71, 0),
            byteArrayOf(74, 73, 85, 0, 0, 0),
            byteArrayOf(74, 85, 0, 0, 0, 0),
            byteArrayOf(74, 85, 65, 78, 0, 0),
            byteArrayOf(74, 85, 69, 0, 0, 0),
            byteArrayOf(74, 85, 78, 0, 0, 0),
            byteArrayOf(75, 65, 0, 0, 0, 0),
            byteArrayOf(75, 65, 73, 0, 0, 0),
            byteArrayOf(75, 65, 78, 0, 0, 0),
            byteArrayOf(75, 65, 78, 71, 0, 0),
            byteArrayOf(75, 65, 79, 0, 0, 0),
            byteArrayOf(75, 69, 0, 0, 0, 0),
            byteArrayOf(75, 69, 78, 0, 0, 0),
            byteArrayOf(75, 69, 78, 71, 0, 0),
            byteArrayOf(75, 79, 78, 71, 0, 0),
            byteArrayOf(75, 79, 85, 0, 0, 0),
            byteArrayOf(75, 85, 0, 0, 0, 0),
            byteArrayOf(75, 85, 65, 0, 0, 0),
            byteArrayOf(75, 85, 65, 73, 0, 0),
            byteArrayOf(75, 85, 65, 78, 0, 0),
            byteArrayOf(75, 85, 65, 78, 71, 0),
            byteArrayOf(75, 85, 73, 0, 0, 0),
            byteArrayOf(75, 85, 78, 0, 0, 0),
            byteArrayOf(75, 85, 79, 0, 0, 0),
            byteArrayOf(76, 65, 0, 0, 0, 0),
            byteArrayOf(76, 65, 73, 0, 0, 0),
            byteArrayOf(76, 65, 78, 0, 0, 0),
            byteArrayOf(76, 65, 78, 71, 0, 0),
            byteArrayOf(76, 65, 79, 0, 0, 0),
            byteArrayOf(76, 69, 0, 0, 0, 0),
            byteArrayOf(76, 69, 73, 0, 0, 0),
            byteArrayOf(76, 69, 78, 71, 0, 0),
            byteArrayOf(76, 73, 0, 0, 0, 0),
            byteArrayOf(76, 73, 65, 0, 0, 0),
            byteArrayOf(76, 73, 65, 78, 0, 0),
            byteArrayOf(76, 73, 65, 78, 71, 0),
            byteArrayOf(76, 73, 65, 79, 0, 0),
            byteArrayOf(76, 73, 69, 0, 0, 0),
            byteArrayOf(76, 73, 78, 0, 0, 0),
            byteArrayOf(76, 73, 78, 71, 0, 0),
            byteArrayOf(76, 73, 85, 0, 0, 0),
            byteArrayOf(76, 79, 0, 0, 0, 0),
            byteArrayOf(76, 79, 78, 71, 0, 0),
            byteArrayOf(76, 79, 85, 0, 0, 0),
            byteArrayOf(76, 85, 0, 0, 0, 0),
            byteArrayOf(76, 85, 65, 78, 0, 0),
            byteArrayOf(76, 85, 69, 0, 0, 0),
            byteArrayOf(76, 85, 78, 0, 0, 0),
            byteArrayOf(76, 85, 79, 0, 0, 0),
            byteArrayOf(77, 0, 0, 0, 0, 0),
            byteArrayOf(77, 65, 0, 0, 0, 0),
            byteArrayOf(77, 65, 73, 0, 0, 0),
            byteArrayOf(77, 65, 78, 0, 0, 0),
            byteArrayOf(77, 65, 78, 71, 0, 0),
            byteArrayOf(77, 65, 79, 0, 0, 0),
            byteArrayOf(77, 69, 0, 0, 0, 0),
            byteArrayOf(77, 69, 73, 0, 0, 0),
            byteArrayOf(77, 69, 78, 0, 0, 0),
            byteArrayOf(77, 69, 78, 71, 0, 0),
            byteArrayOf(77, 73, 0, 0, 0, 0),
            byteArrayOf(77, 73, 65, 78, 0, 0),
            byteArrayOf(77, 73, 65, 79, 0, 0),
            byteArrayOf(77, 73, 69, 0, 0, 0),
            byteArrayOf(77, 73, 78, 0, 0, 0),
            byteArrayOf(77, 73, 78, 71, 0, 0),
            byteArrayOf(77, 73, 85, 0, 0, 0),
            byteArrayOf(77, 79, 0, 0, 0, 0),
            byteArrayOf(77, 79, 85, 0, 0, 0),
            byteArrayOf(77, 85, 0, 0, 0, 0),
            byteArrayOf(78, 0, 0, 0, 0, 0),
            byteArrayOf(78, 65, 0, 0, 0, 0),
            byteArrayOf(78, 65, 73, 0, 0, 0),
            byteArrayOf(78, 65, 78, 0, 0, 0),
            byteArrayOf(78, 65, 78, 71, 0, 0),
            byteArrayOf(78, 65, 79, 0, 0, 0),
            byteArrayOf(78, 69, 0, 0, 0, 0),
            byteArrayOf(78, 69, 73, 0, 0, 0),
            byteArrayOf(78, 69, 78, 0, 0, 0),
            byteArrayOf(78, 69, 78, 71, 0, 0),
            byteArrayOf(78, 73, 0, 0, 0, 0),
            byteArrayOf(78, 73, 65, 78, 0, 0),
            byteArrayOf(78, 73, 65, 78, 71, 0),
            byteArrayOf(78, 73, 65, 79, 0, 0),
            byteArrayOf(78, 73, 69, 0, 0, 0),
            byteArrayOf(78, 73, 78, 0, 0, 0),
            byteArrayOf(78, 73, 78, 71, 0, 0),
            byteArrayOf(78, 73, 85, 0, 0, 0),
            byteArrayOf(78, 79, 78, 71, 0, 0),
            byteArrayOf(78, 79, 85, 0, 0, 0),
            byteArrayOf(78, 85, 0, 0, 0, 0),
            byteArrayOf(78, 85, 65, 78, 0, 0),
            byteArrayOf(78, 85, 69, 0, 0, 0),
            byteArrayOf(78, 85, 78, 0, 0, 0),
            byteArrayOf(78, 85, 79, 0, 0, 0),
            byteArrayOf(79, 0, 0, 0, 0, 0),
            byteArrayOf(79, 85, 0, 0, 0, 0),
            byteArrayOf(80, 65, 0, 0, 0, 0),
            byteArrayOf(80, 65, 73, 0, 0, 0),
            byteArrayOf(80, 65, 78, 0, 0, 0),
            byteArrayOf(80, 65, 78, 71, 0, 0),
            byteArrayOf(80, 65, 79, 0, 0, 0),
            byteArrayOf(80, 69, 73, 0, 0, 0),
            byteArrayOf(80, 69, 78, 0, 0, 0),
            byteArrayOf(80, 69, 78, 71, 0, 0),
            byteArrayOf(80, 73, 0, 0, 0, 0),
            byteArrayOf(80, 73, 65, 78, 0, 0),
            byteArrayOf(80, 73, 65, 79, 0, 0),
            byteArrayOf(80, 73, 69, 0, 0, 0),
            byteArrayOf(80, 73, 78, 0, 0, 0),
            byteArrayOf(80, 73, 78, 71, 0, 0),
            byteArrayOf(80, 79, 0, 0, 0, 0),
            byteArrayOf(80, 79, 85, 0, 0, 0),
            byteArrayOf(80, 85, 0, 0, 0, 0),
            byteArrayOf(81, 73, 0, 0, 0, 0),
            byteArrayOf(81, 73, 65, 0, 0, 0),
            byteArrayOf(81, 73, 65, 78, 0, 0),
            byteArrayOf(81, 73, 65, 78, 71, 0),
            byteArrayOf(81, 73, 65, 79, 0, 0),
            byteArrayOf(81, 73, 69, 0, 0, 0),
            byteArrayOf(81, 73, 78, 0, 0, 0),
            byteArrayOf(81, 73, 78, 71, 0, 0),
            byteArrayOf(81, 73, 79, 78, 71, 0),
            byteArrayOf(81, 73, 85, 0, 0, 0),
            byteArrayOf(81, 85, 0, 0, 0, 0),
            byteArrayOf(81, 85, 65, 78, 0, 0),
            byteArrayOf(81, 85, 69, 0, 0, 0),
            byteArrayOf(81, 85, 78, 0, 0, 0),
            byteArrayOf(82, 65, 78, 0, 0, 0),
            byteArrayOf(82, 65, 78, 71, 0, 0),
            byteArrayOf(82, 65, 79, 0, 0, 0),
            byteArrayOf(82, 69, 0, 0, 0, 0),
            byteArrayOf(82, 69, 78, 0, 0, 0),
            byteArrayOf(82, 69, 78, 71, 0, 0),
            byteArrayOf(82, 73, 0, 0, 0, 0),
            byteArrayOf(82, 79, 78, 71, 0, 0),
            byteArrayOf(82, 79, 85, 0, 0, 0),
            byteArrayOf(82, 85, 0, 0, 0, 0),
            byteArrayOf(82, 85, 65, 0, 0, 0),
            byteArrayOf(82, 85, 65, 78, 0, 0),
            byteArrayOf(82, 85, 73, 0, 0, 0),
            byteArrayOf(82, 85, 78, 0, 0, 0),
            byteArrayOf(82, 85, 79, 0, 0, 0),
            byteArrayOf(83, 65, 0, 0, 0, 0),
            byteArrayOf(83, 65, 73, 0, 0, 0),
            byteArrayOf(83, 65, 78, 0, 0, 0),
            byteArrayOf(83, 65, 78, 71, 0, 0),
            byteArrayOf(83, 65, 79, 0, 0, 0),
            byteArrayOf(83, 69, 0, 0, 0, 0),
            byteArrayOf(83, 69, 78, 0, 0, 0),
            byteArrayOf(83, 69, 78, 71, 0, 0),
            byteArrayOf(83, 72, 65, 0, 0, 0),
            byteArrayOf(83, 72, 65, 73, 0, 0),
            byteArrayOf(83, 72, 65, 78, 0, 0),
            byteArrayOf(83, 72, 65, 78, 71, 0),
            byteArrayOf(83, 72, 65, 79, 0, 0),
            byteArrayOf(83, 72, 69, 0, 0, 0),
            byteArrayOf(83, 72, 69, 78, 0, 0),
            byteArrayOf(88, 73, 78, 0, 0, 0),
            byteArrayOf(83, 72, 69, 78, 0, 0),
            byteArrayOf(83, 72, 69, 78, 71, 0),
            byteArrayOf(83, 72, 73, 0, 0, 0),
            byteArrayOf(83, 72, 79, 85, 0, 0),
            byteArrayOf(83, 72, 85, 0, 0, 0),
            byteArrayOf(83, 72, 85, 65, 0, 0),
            byteArrayOf(83, 72, 85, 65, 73, 0),
            byteArrayOf(83, 72, 85, 65, 78, 0),
            byteArrayOf(83, 72, 85, 65, 78, 71),
            byteArrayOf(83, 72, 85, 73, 0, 0),
            byteArrayOf(83, 72, 85, 78, 0, 0),
            byteArrayOf(83, 72, 85, 79, 0, 0),
            byteArrayOf(83, 73, 0, 0, 0, 0),
            byteArrayOf(83, 79, 78, 71, 0, 0),
            byteArrayOf(83, 79, 85, 0, 0, 0),
            byteArrayOf(83, 85, 0, 0, 0, 0),
            byteArrayOf(83, 85, 65, 78, 0, 0),
            byteArrayOf(83, 85, 73, 0, 0, 0),
            byteArrayOf(83, 85, 78, 0, 0, 0),
            byteArrayOf(83, 85, 79, 0, 0, 0),
            byteArrayOf(84, 65, 0, 0, 0, 0),
            byteArrayOf(84, 65, 73, 0, 0, 0),
            byteArrayOf(84, 65, 78, 0, 0, 0),
            byteArrayOf(84, 65, 78, 71, 0, 0),
            byteArrayOf(84, 65, 79, 0, 0, 0),
            byteArrayOf(84, 69, 0, 0, 0, 0),
            byteArrayOf(84, 69, 78, 71, 0, 0),
            byteArrayOf(84, 73, 0, 0, 0, 0),
            byteArrayOf(84, 73, 65, 78, 0, 0),
            byteArrayOf(84, 73, 65, 79, 0, 0),
            byteArrayOf(84, 73, 69, 0, 0, 0),
            byteArrayOf(84, 73, 78, 71, 0, 0),
            byteArrayOf(84, 79, 78, 71, 0, 0),
            byteArrayOf(84, 79, 85, 0, 0, 0),
            byteArrayOf(84, 85, 0, 0, 0, 0),
            byteArrayOf(84, 85, 65, 78, 0, 0),
            byteArrayOf(84, 85, 73, 0, 0, 0),
            byteArrayOf(84, 85, 78, 0, 0, 0),
            byteArrayOf(84, 85, 79, 0, 0, 0),
            byteArrayOf(87, 65, 0, 0, 0, 0),
            byteArrayOf(87, 65, 73, 0, 0, 0),
            byteArrayOf(87, 65, 78, 0, 0, 0),
            byteArrayOf(87, 65, 78, 71, 0, 0),
            byteArrayOf(87, 69, 73, 0, 0, 0),
            byteArrayOf(87, 69, 78, 0, 0, 0),
            byteArrayOf(87, 69, 78, 71, 0, 0),
            byteArrayOf(87, 79, 0, 0, 0, 0),
            byteArrayOf(87, 85, 0, 0, 0, 0),
            byteArrayOf(88, 73, 0, 0, 0, 0),
            byteArrayOf(88, 73, 65, 0, 0, 0),
            byteArrayOf(88, 73, 65, 78, 0, 0),
            byteArrayOf(88, 73, 65, 78, 71, 0),
            byteArrayOf(88, 73, 65, 79, 0, 0),
            byteArrayOf(88, 73, 69, 0, 0, 0),
            byteArrayOf(88, 73, 78, 0, 0, 0),
            byteArrayOf(88, 73, 78, 71, 0, 0),
            byteArrayOf(88, 73, 79, 78, 71, 0),
            byteArrayOf(88, 73, 85, 0, 0, 0),
            byteArrayOf(88, 85, 0, 0, 0, 0),
            byteArrayOf(88, 85, 65, 78, 0, 0),
            byteArrayOf(88, 85, 69, 0, 0, 0),
            byteArrayOf(88, 85, 78, 0, 0, 0),
            byteArrayOf(89, 65, 0, 0, 0, 0),
            byteArrayOf(89, 65, 78, 0, 0, 0),
            byteArrayOf(89, 65, 78, 71, 0, 0),
            byteArrayOf(89, 65, 79, 0, 0, 0),
            byteArrayOf(89, 69, 0, 0, 0, 0),
            byteArrayOf(89, 73, 0, 0, 0, 0),
            byteArrayOf(89, 73, 78, 0, 0, 0),
            byteArrayOf(89, 73, 78, 71, 0, 0),
            byteArrayOf(89, 79, 0, 0, 0, 0),
            byteArrayOf(89, 79, 78, 71, 0, 0),
            byteArrayOf(89, 79, 85, 0, 0, 0),
            byteArrayOf(89, 85, 0, 0, 0, 0),
            byteArrayOf(89, 85, 65, 78, 0, 0),
            byteArrayOf(89, 85, 69, 0, 0, 0),
            byteArrayOf(89, 85, 78, 0, 0, 0),
            byteArrayOf(74, 85, 78, 0, 0, 0),
            byteArrayOf(89, 85, 78, 0, 0, 0),
            byteArrayOf(90, 65, 0, 0, 0, 0),
            byteArrayOf(90, 65, 73, 0, 0, 0),
            byteArrayOf(90, 65, 78, 0, 0, 0),
            byteArrayOf(90, 65, 78, 71, 0, 0),
            byteArrayOf(90, 65, 79, 0, 0, 0),
            byteArrayOf(90, 69, 0, 0, 0, 0),
            byteArrayOf(90, 69, 73, 0, 0, 0),
            byteArrayOf(90, 69, 78, 0, 0, 0),
            byteArrayOf(90, 69, 78, 71, 0, 0),
            byteArrayOf(90, 72, 65, 0, 0, 0),
            byteArrayOf(90, 72, 65, 73, 0, 0),
            byteArrayOf(90, 72, 65, 78, 0, 0),
            byteArrayOf(90, 72, 65, 78, 71, 0),
            byteArrayOf(67, 72, 65, 78, 71, 0),
            byteArrayOf(90, 72, 65, 78, 71, 0),
            byteArrayOf(90, 72, 65, 79, 0, 0),
            byteArrayOf(90, 72, 69, 0, 0, 0),
            byteArrayOf(90, 72, 69, 78, 0, 0),
            byteArrayOf(90, 72, 69, 78, 71, 0),
            byteArrayOf(90, 72, 73, 0, 0, 0),
            byteArrayOf(83, 72, 73, 0, 0, 0),
            byteArrayOf(90, 72, 73, 0, 0, 0),
            byteArrayOf(90, 72, 79, 78, 71, 0),
            byteArrayOf(90, 72, 79, 85, 0, 0),
            byteArrayOf(90, 72, 85, 0, 0, 0),
            byteArrayOf(90, 72, 85, 65, 0, 0),
            byteArrayOf(90, 72, 85, 65, 73, 0),
            byteArrayOf(90, 72, 85, 65, 78, 0),
            byteArrayOf(90, 72, 85, 65, 78, 71),
            byteArrayOf(90, 72, 85, 73, 0, 0),
            byteArrayOf(90, 72, 85, 78, 0, 0),
            byteArrayOf(90, 72, 85, 79, 0, 0),
            byteArrayOf(90, 73, 0, 0, 0, 0),
            byteArrayOf(90, 79, 78, 71, 0, 0),
            byteArrayOf(90, 79, 85, 0, 0, 0),
            byteArrayOf(90, 85, 0, 0, 0, 0),
            byteArrayOf(90, 85, 65, 78, 0, 0),
            byteArrayOf(90, 85, 73, 0, 0, 0),
            byteArrayOf(90, 85, 78, 0, 0, 0),
            byteArrayOf(90, 85, 79, 0, 0, 0),
            byteArrayOf(0, 0, 0, 0, 0, 0),
            byteArrayOf(83, 72, 65, 78, 0, 0),
            byteArrayOf(0, 0, 0, 0, 0, 0)
        )

        /**
         * 第一个和最后一个汉字,根据zh已知拼音
         */
        private const val FIRST_PINYIN_UNI_HAN = "\u963F"
        private const val LAST_PINYIN_UNI_HAN = "\u9FFF"
        private val COLLATOR = Collator.getInstance(Locale.CHINA)

        @JvmStatic
        var instance: HanZiToPinYin? = null
            get() {
                synchronized(HanZiToPinYin::class.java) {
                    if (field != null) {
                        return field
                    }
                    // 检查排序规则数据是否可用zh_CN
                    val locale = Collator.getAvailableLocales()
                    // 增加的代码,增强。
                    val chinaAddition = Locale("zh")
                    for (value in locale) {
                        if (value == Locale.CHINA || value == chinaAddition) {
                            // 只做一次自我验证.
                            field = HanZiToPinYin(true)
                            return field
                        }
                    }
                    field = HanZiToPinYin(false)
                    return field
                }
            }
            private set
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值