原文:https://blog.csdn.net/uyy203/article/details/54632495
最近發(fā)現(xiàn)一個(gè)相當(dāng)輕巧鲸沮,運(yùn)行速度很快的漢字轉(zhuǎn)拼音庫(kù)——TinyPinyin蝇刀,這個(gè)漢字轉(zhuǎn)拼音庫(kù)比上一篇講述列表按照A-Z的規(guī)則排序的文章所使用的漢字轉(zhuǎn)拼音庫(kù)運(yùn)行速度還要快10倍以上。
主要特性
生成的拼音不包含聲調(diào)和方言慕爬,均為大寫漓帚;
支持自定義詞典飘弧;
執(zhí)行效率很高(Pinyin4J 的 4~16 倍)携栋;
很低的內(nèi)存占用(不添加詞典時(shí)小于 30KB)。
主導(dǎo)入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 的中國(guó)地區(qū)詞典
compile 'com.github.promeg:tinypinyin-lexicons-java-cncity:2.0.1' // 可選,適用于 Java 的中國(guó)地區(qū)詞典
}
}
使用方法
/**
* 如果 c 為漢字痢毒,則返回大寫拼音送矩;如果 c 不是漢字,則返回 String.valueOf(c)
*/
String Pinyin.toPinyin(char c)
/**
* c 為漢字哪替,則返回 true栋荸,否則返回 false
*/
boolean Pinyin.isChinese(char c)
/**
* 將輸入字符串轉(zhuǎn)為拼音,轉(zhuǎn)換過(guò)程中會(huì)使用之前設(shè)置的用戶詞典凭舶,以字符為單位插入分隔符
*/
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;
}
}));