美文网首页Android笔记
轻巧的汉字转拼音库 TinyPinyin 在Android上的使

轻巧的汉字转拼音库 TinyPinyin 在Android上的使

作者: Cedric_h | 来源:发表于2019-07-25 01:34 被阅读0次

    原文:https://blog.csdn.net/uyy203/article/details/54632495

    最近发现一个相当轻巧,运行速度很快的汉字转拼音库——TinyPinyin,这个汉字转拼音库比上一篇讲述列表按照A-Z的规则排序的文章所使用的汉字转拼音库运行速度还要快10倍以上。

    主要特性
    生成的拼音不包含声调和方言,均为大写;
    支持自定义词典;
    执行效率很高(Pinyin4J 的 4~16 倍);
    很低的内存占用(不添加词典时小于 30KB)。

    主导入Gradle

    buildscript {
      repositories {
        jcenter()
      }
     
      dependencies {
        compile 'com.github.promeg:tinypinyin:2.0.1' // TinyPinyin 核心包,约 80KB
     
        compile 'com.github.promeg:tinypinyin-lexicons-android-cncity:2.0.1' // 可选,适用于 Android 的中国地区词典
     
        compile 'com.github.promeg:tinypinyin-lexicons-java-cncity:2.0.1' // 可选,适用于 Java 的中国地区词典
      }
    }
    

    使用方法

    /**
     * 如果 c 为汉字,则返回大写拼音;如果 c 不是汉字,则返回 String.valueOf(c)
     */
    String Pinyin.toPinyin(char c)
     
    /**
     * c 为汉字,则返回 true,否则返回 false
     */
    boolean Pinyin.isChinese(char c)
     
    /**
     * 将输入字符串转为拼音,转换过程中会使用之前设置的用户词典,以字符为单位插入分隔符
     */
    String toPinyin(String str, String separator)
    

    词典API

    // 添加中文城市词典
    Pinyin.init(Pinyin.newConfig().with(CnCityDict.getInstance());
     
    // 添加自定义词典
    Pinyin.init(Pinyin.newConfig()
                .with(new PinyinMapDict() {
                    @Override
                    public Map<String, String[]> mapping() {
                        HashMap<String, String[]> map = new HashMap<String, String[]>();
                        map.put("重庆",  new String[]{"CHONG", "QING"});
                        return map;
                    }
                }));
    

    github: https://github.com/Cedric-Xuan/Sort

    相关文章

      网友评论

        本文标题:轻巧的汉字转拼音库 TinyPinyin 在Android上的使

        本文链接:https://www.haomeiwen.com/subject/kjgqrctx.html