All Projects → promeG → Tinypinyin

promeG / Tinypinyin

Licence: apache-2.0
适用于Java和Android的快速、低内存占用的汉字转拼音库。

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Tinypinyin

ToolGood.Words.Core
一款高性能敏感词(非法词/脏字)检测过滤组件,附带繁体简体互换,支持全角半角互换,汉字转拼音,模糊搜索等功能。支持.net standard 2.0
Stars: ✭ 66 (-98.03%)
Mutual labels:  pinyin
Adafruit CircuitPython CharLCD
Library code for character LCD interfacing
Stars: ✭ 54 (-98.39%)
Mutual labels:  character
Duik-15
Duduf IK & Animation Tools for Adobe After Effects
Stars: ✭ 156 (-95.34%)
Mutual labels:  character
syng
A free, open source, cross-platform, Chinese-To-English dictionary for desktops.
Stars: ✭ 108 (-96.77%)
Mutual labels:  pinyin
hanzi-tools
Converts from Chinese characters to pinyin, between simplified and traditional, and does word segmentation.
Stars: ✭ 69 (-97.94%)
Mutual labels:  pinyin
mw2fcitx
Fcitx 5 pinyin dictionary generator for MediaWiki instances. (Releases for demo dict of zh.moegirl.org.cn)
Stars: ✭ 80 (-97.61%)
Mutual labels:  pinyin
ProjectKaya
Project Kaya for mobile game platform
Stars: ✭ 324 (-90.32%)
Mutual labels:  character
Go Password Validator
Validate the Strength of a Password in Go
Stars: ✭ 263 (-92.14%)
Mutual labels:  character
HideDroid
HideDroid is an Android app that allows the per-app anonymization of collected personal data according to a privacy level chosen by the user.
Stars: ✭ 85 (-97.46%)
Mutual labels:  java-android
PHPChineseToPinyin
请移步:
Stars: ✭ 40 (-98.81%)
Mutual labels:  pinyin
ThirdPersonController
Simple 3rd person controller demonstrating camera-relative movement and the new Cinemachine 3rd Person Follow / Aim system
Stars: ✭ 15 (-99.55%)
Mutual labels:  character
urdu-characters
📄 Complete collection of Urdu language characters & unicode code points.
Stars: ✭ 24 (-99.28%)
Mutual labels:  character
Pinyin4NET
c# 拼音汉字/姓相互转换工具库 (这只是镜像仓库,源仓库见 https://gitee.com/hyjiacan/Pinyin4Net)
Stars: ✭ 38 (-98.86%)
Mutual labels:  pinyin
Video-Trimmer-Android
Trim the video by adjusting starting point and ending point in Android
Stars: ✭ 49 (-98.54%)
Mutual labels:  java-android
rust-pinyin
汉字转拼音
Stars: ✭ 111 (-96.68%)
Mutual labels:  pinyin
Cinelights
Example project using Lighting tools package and Cine lights package for Unity.
Stars: ✭ 23 (-99.31%)
Mutual labels:  character
pinyin
javascript实现输入汉字获取汉字拼音或者汉字拼音首字母
Stars: ✭ 27 (-99.19%)
Mutual labels:  pinyin
Chinesepinyin Codecompletionhelper
让你的 JetBrains 系 IDE ( IDEA ,PyCharm,PhpStorm,WebStorm,AndroidStudio,DevEco等 )支持中文标识符以拼音输入方式完成代码补全,享受和英文环境一致的中文智能编码体验,为代码表达提供更多选择,一种值得考虑的折中解决方案
Stars: ✭ 262 (-92.17%)
Mutual labels:  pinyin
Phrase Pinyin Data
词语拼音数据
Stars: ✭ 257 (-92.32%)
Mutual labels:  pinyin
AmimeWatch
Telegram bot made in Python 3 using the @pyrogram framework.
Stars: ✭ 19 (-99.43%)
Mutual labels:  character

TinyPinyin

Build Status

适用于Java和Android的快速、低内存占用的汉字转拼音库。

当前稳定版本:2.0.3

特性

  1. 生成的拼音不包含声调,均为大写;
  2. 支持自定义词典,支持简体中文、繁体中文;
  3. 执行效率很高(Pinyin4J的4~16倍);
  4. 很低的内存占用(不添加词典时小于30KB)。

原理介绍

打造最好的Java拼音库TinyPinyin(一):单字符转拼音的极致优化

打造最好的Java拼音库TinyPinyin(二):多音字快速处理方案

打造最好的Java拼音库TinyPinyin(三):API设计和测试实践

使用

汉字转拼音API

/**
 * 如果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;
                }
            }));

添加到工程

buildscript {
  repositories {
    jcenter()
  }

  dependencies {
    compile 'com.github.promeg:tinypinyin:2.0.3' // TinyPinyin核心包,约80KB

    compile 'com.github.promeg:tinypinyin-lexicons-android-cncity:2.0.3' // 可选,适用于Android的中国地区词典

    compile 'com.github.promeg:tinypinyin-lexicons-java-cncity:2.0.3' // 可选,适用于Java的中国地区词典
  }
}

详细说明

1. 设计目标

Pinyin4J的问题

  1. Jar文件较大,205KB;
  2. Pinyin4J的PinyinHelper.toHanyuPinyinStringArray 在第一次调用时耗时非常长(~2000ms);
  3. 功能臃肿,许多情况下我们不需要声调、方言;
  4. 无法添加自定义词典,进而无法有效处理多音字
  5. 内存占用太高;

TinyPinyin特性

  1. 转换后的结果包含声调和方言;
  2. 支持自定义词典,方便处理多音字;
  3. 尽可能低的内存占用;
  4. 比Pinyin4J更快的转换速度;

2. Correctness

以Pinyin4J作为基准,确保对所有的字符(Character.MAX_VALUE ~ Character.MIN_VALUE),TinyPinyin与Pinyin4J有相同的返回结果。

(Pinyin4J采用无声调的输出,多音字取第一个拼音进行对比)

该部分请见PinyinTest.java

繁体中文的测试请见:PinyinTest.testToPinyin_traditional_chars()

采用以下命令运行test:

./gradlew clean build :lib:test :tinypinyin-lexicons-android-cncity:test :tinypinyin-android-asset-lexicons:test :android-sample:connectedAndroidTest

3. Effectiveness

速度

使用JMH工具得到bechmark,对比TinyPinyin和Pinyin4J的运行速度。

具体测例请见lib/src/jmh/中的性能测试代码。

采用以下命令运行benchmark:

./gradlew jmh

生成的报告在 pinyinhelper/build/reports/jmh/ 中。

性能测试结果简要说明:单个字符转拼音的速度是Pinyin4j的四倍,添加字典后字符串转拼音的速度是Pinyin4j的16倍

详细测试结果:

Benchmark Mode Samples Score Unit
TinyPinyin_Init_With_Large_Dict(初始化大词典) thrpt 200 66.131 ops/s
TinyPinyin_Init_With_Small_Dict(初始化小词典) thrpt 200 35408.045 ops/s
TinyPinyin_StringToPinyin_With_Large_Dict(添加大词典后进行String转拼音) thrpt 200 16.268 ops/ms
Pinyin4j_StringToPinyin(Pinyin4j的String转拼音) thrpt 200 1.033 ops/ms
TinyPinyin_CharToPinyin(字符转拼音) thrpt 200 14.285 ops/us
Pinyin4j_CharToPinyin(Pinyin4j的字符转拼音) thrpt 200 4.460 ops/us
TinyPinyin_IsChinese(字符是否为汉字) thrpt 200 15.552 ops/us
Pinyin4j_IsChinese(Pinyin4j的字符是否为汉字) thrpt 200 4.432 ops/us

内存占用

1. 不添加词典时
  • 3个static byte[7000] 存储所有汉字的拼音的低8位,占用7000 1 3 = 21KB 内存;
  • 3个static byte[7000/8] 存储所有汉字的拼音的第9位(最高位),占用7000 / 8 1 3 = 3KB 内存;
  • 一个String[408] 存储所有可能的拼音,占用 1.7KB 内存;

共占用 < 30KB.

2. 添加词典时

使用‘com.github.promeg:tinypinyin-lexicons-java-cncity:2.0.0’时,额外消耗约43KB内存。

Todo

  • 支持繁体中文
  • 支持姓氏拼音
  • 压缩词库
  • 词库生成工具
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].