android實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能 帶多音字識(shí)別
android 漢字轉(zhuǎn)拼音帶多音字識(shí)別功能,供大家參考,具體內(nèi)容如下
問題來源
在做地名按首字母排序的時(shí)候出現(xiàn)了這樣一個(gè)bug。長沙會(huì)被翻譯拼音成zhangsha,重慶會(huì)被翻譯拼音成zhong qing。于是排序出了問題。
漢字轉(zhuǎn)拼音庫和多音字識(shí)別庫
1.多音字對(duì)應(yīng)的詞匯庫
2.文字的二進(jìn)制大小對(duì)應(yīng)的拼音庫
關(guān)鍵代碼
1.我在這里首先將要轉(zhuǎn)化的文字轉(zhuǎn)化成對(duì)應(yīng)的”gb2312”編碼。漢字轉(zhuǎn)化成二進(jìn)制編碼一般占兩個(gè)字節(jié),如果一個(gè)字節(jié)返回字符,如果是兩個(gè)字節(jié)算一下偏移量。代碼如下
/** * 漢字轉(zhuǎn)成ASCII碼 * * @param chs * @return */
private int getChsAscii(String chs) {
int asc = 0;
try {
byte[] bytes = chs.getBytes("gb2312");
if (bytes == null || bytes.length > 2 || bytes.length <= 0) {
throw new RuntimeException("illegal resource string");
}
if (bytes.length == 1) {
asc = bytes[0];
}
if (bytes.length == 2) {
int hightByte = 256 + bytes[0];
int lowByte = 256 + bytes[1];
asc = (256 * hightByte + lowByte) - 256 * 256;
}
} catch (Exception e) {
System.out.println("ERROR:ChineseSpelling.class-getChsAscii(String chs)" + e);
}
return asc;
}
2.將單個(gè)漢字獲取的拼音再和多音字庫的hashMap進(jìn)行比較,代碼如下:
public String getSellingWithPolyphone(String chs){
if(polyphoneMap != null && polyphoneMap.isEmpty()){
polyphoneMap = initDictionary();
}
String key, value, resultPy = null;
buffer = new StringBuilder();
for (int i = 0; i < chs.length(); i++) {
key = chs.substring(i, i + 1);
if (key.getBytes().length >= 2) {
value = (String) convert(key);
if (value == null) {
value = "unknown";
}
} else {
value = key;
}
resultPy = value;
String left = null;
if(i>=1 && i+1 <= chs.length()){
left = chs.substring(i-1,i+1);
if(polyphoneMap.containsKey(value) && polyphoneMap.get(value).contains(left)){
resultPy = value;
}
}
// if(chs.contains("重慶")){
String right = null; //向右多取一個(gè)字,例如 [長]沙
if(i<=chs.length()-2){
right = chs.substring(i,i+2);
if(polyphoneMap.containsKey(right)){
resultPy = polyphoneMap.get(right);
}
}
// }
String middle = null; //左右各多取一個(gè)字,例如 龍[爪]槐
if(i>=1 && i+2<=chs.length()){
middle = chs.substring(i-1,i+2);
if(polyphoneMap.containsKey(value) && polyphoneMap.get(value).contains(middle)){
resultPy = value;
}
}
String left3 = null; //向左多取2個(gè)字,如 羋月[傳],列車長
if(i>=2 && i+1<=chs.length()){
left3 = chs.substring(i-2,i+1);
if(polyphoneMap.containsKey(value) && polyphoneMap.get(value).contains(left3)){
resultPy = value;
}
}
String right3 = null; //向右多取2個(gè)字,如 [長]孫無忌
if(i<=chs.length()-3){
right3 = chs.substring(i,i+3);
if(polyphoneMap.containsKey(value) && polyphoneMap.get(value).contains(right3)){
resultPy = value;
}
}
buffer.append(resultPy);
}
return buffer.toString();
}
3.將asserts文件內(nèi)容解析生成HashMap列表.
public HashMap<String, String> initDictionary(){
String fileName = "py4j.dic";
InputStreamReader inputReader = null;
BufferedReader bufferedReader = null;
HashMap<String, String> polyphoneMap = new HashMap<String, String>();
try{
inputReader = new InputStreamReader(MyApplication.mContext.getResources().getAssets().open(fileName),"UTF-8");
bufferedReader = new BufferedReader(inputReader);
String line = null;
while((line = bufferedReader.readLine()) != null){
String[] arr = line.split(PINYIN_SEPARATOR);
if(isNotEmpty(arr[1])){
String[] dyzs = arr[1].split(WORD_SEPARATOR);
for(String dyz: dyzs){
if(isNotEmpty(dyz)){
polyphoneMap.put(dyz.trim(),arr[0]);
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(inputReader != null){
try {
inputReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return polyphoneMap;
}
github源碼下載:https://github.com/loveburce/ChinesePolyphone.git
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)中文按拼音排序方法
- Android實(shí)現(xiàn)ListView的A-Z字母排序和過濾搜索功能 實(shí)現(xiàn)漢字轉(zhuǎn)成拼音
- android仿微信通訊錄搜索示例(匹配拼音,字母,索引位置)
- Android開發(fā)實(shí)現(xiàn)的IntentUtil跳轉(zhuǎn)多功能工具類【包含視頻、音頻、圖片、攝像頭等操作功能】
- android實(shí)用工具類分享(獲取內(nèi)存/檢查網(wǎng)絡(luò)/屏幕高度/手機(jī)分辨率)
- android開發(fā)教程之實(shí)現(xiàn)toast工具類
- 19個(gè)Android常用工具類匯總
- android 一些工具類匯總
- Android7.0 工具類:DiffUtil詳解
- 非常實(shí)用的Android圖片工具類
- Android開發(fā)之拼音轉(zhuǎn)換工具類PinyinUtils示例
相關(guān)文章
android與asp.net服務(wù)端共享session的方法詳解
這篇文章主要給大家介紹了關(guān)于android與asp.net服務(wù)端如何共享session的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)下吧。2017-09-09
AndroidStudio3 支持 Java8 了請(qǐng)問你敢用嗎
Google 發(fā)布了 AS 3.0,以及一系列的 Support 包,有意思的新東西挺多,AS3里面有一個(gè)亮眼的特性就是支持J8。接下來通過本文給大家分享AndroidStudio3 支持 Java8 的相關(guān)內(nèi)容,感興趣的朋友一起看看吧2017-11-11
Android開發(fā) 旋轉(zhuǎn)屏幕導(dǎo)致Activity重建解決方法
Android開發(fā)文檔上專門有一小節(jié)解釋這個(gè)問題。簡(jiǎn)單來說,Activity是負(fù)責(zé)與用戶交互的最主要機(jī)制,接下來為您詳細(xì)介紹2012-11-11
Android中ViewPager的PagerTabStrip與PagerTitleStrip用法實(shí)例
這篇文章主要介紹了Android中ViewPager的PagerTabStrip與PagerTitleStrip用法實(shí)例,這兩個(gè)子控件一般被用作添加標(biāo)題,在實(shí)際效果上并不是那么好控制,使用的時(shí)候需要謹(jǐn)慎,需要的朋友可以參考下2016-06-06
OpenGL?Shader實(shí)現(xiàn)陰影遮罩效果
這篇文章主要介紹了如何利用OpenGL?Shader實(shí)現(xiàn)陰影遮罩效果,文中的示例代碼簡(jiǎn)潔易懂,對(duì)我們學(xué)習(xí)OpenGL有一定幫助,需要的可以參考一下2022-02-02

