TinyPinyin Documentation

repository·master·Indexed 26 days ago

https://github.com/promeg/tinypinyin

A fast, low-memory footprint Java and Android library for converting Chinese characters to uppercase Pinyin without tone marks. Features include efficient dictionary support, built-in Chinese city lexicons, and the ability to define custom dictionaries via PinyinMapDict.

Tokens
1K
Snippets
3
Records
3
Agent score
37%

What's inside TinyPinyin

  1. Add TinyPinyin to your project

    master

    To use TinyPinyin in a Java or Android project, add the core dependency and any optional lexicon dependencies to your build.gradle file.

    Note: Ensure you have mavenCentral() and the Aliyun Maven repository configured in your repositories block.

    buildscript {
      repositories {
        maven {
          url 'https://maven.aliyun.com/repository/public'
        }
        mavenCentral()
      }
    
      dependencies {
        compile 'com.github.promeg:tinypinyin:2.0.3' // TinyPinyin core (~80KB)
    
        compile 'com.github.promeg:tinypinyin-lexicons-android-cncity:2.0.3' // Optional: Android Chinese city lexicon
    
        compile 'com.github.promeg:tinypinyin-lexicons-java-cncity:2.0.3' // Optional: Java Chinese city lexicon
      }
    }
  2. Convert characters and strings to Pinyin

    master

    TinyPinyin provides several methods for converting Chinese characters and strings into uppercase Pinyin without tone marks.

    • Pinyin.toPinyin(char c): Returns the uppercase Pinyin for a Chinese character. If the character is not Chinese, it returns the character as a String.
    • Pinyin.isChinese(char c): Returns true if the character is a Chinese character, otherwise false.
    • toPinyin(String str, String separator): Converts an entire string to Pinyin, inserting the specified separator between characters. This method uses any user-defined dictionaries configured via Pinyin.init().
    /**
     * 如果c为汉字,则返回大写拼音;如果c不是汉字,则返回String.valueOf(c)
     */
    String Pinyin.toPinyin(char c)
    
    /**
     * c为汉字,则返回true,否则返回false
     */
    boolean Pinyin.isChinese(char c)
    
    /**
     * 将输入字符串转为拼音,转换过程中会使用之前设置的用户词典,以字符为单位插入分隔符
     */
    String toPinyin(String str, String separator)
  3. Configure custom or city dictionaries

    master

    You can initialize TinyPinyin with specific lexicons to improve accuracy (especially for polyphonic characters/多音字). Use Pinyin.init() with a configuration object created via Pinyin.newConfig().

    To use the built-in Chinese city lexicon:

    Pinyin.init(Pinyin.newConfig().with(CnCityDict.getInstance()));

    To define a custom dictionary by implementing PinyinMapDict:

    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;
                    }
                }));
    // 添加中文城市词典
    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;
                    }
                }));