国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

android實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能 帶多音字識(shí)別

2019-10-23 19:44:51
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

android 漢字轉(zhuǎn)拼音帶多音字識(shí)別功能,供大家參考,具體內(nèi)容如下

問(wèn)題來(lái)源

在做地名按首字母排序的時(shí)候出現(xiàn)了這樣一個(gè)bug。長(zhǎng)沙會(huì)被翻譯拼音成zhangsha,重慶會(huì)被翻譯拼音成zhong qing。于是排序出了問(wèn)題。

漢字轉(zhuǎn)拼音庫(kù)和多音字識(shí)別庫(kù)
1.多音字對(duì)應(yīng)的詞匯庫(kù)
2.文字的二進(jìn)制大小對(duì)應(yīng)的拼音庫(kù)

關(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è)漢字獲取的拼音再和多音字庫(kù)的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è)字,例如 [長(zhǎng)]沙     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è)字,如 羋月[傳],列車(chē)長(zhǎng)    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è)字,如 [長(zhǎng)]孫無(wú)忌    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í)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 来宾市| 财经| 呼伦贝尔市| 玉林市| 宜州市| 铜陵市| 延津县| 双流县| 鹰潭市| 望都县| 东兴市| 谷城县| 仲巴县| 开原市| 中山市| 哈巴河县| 浦城县| 泊头市| 新泰市| 全州县| 铜陵市| 城市| 定西市| 兴仁县| 嫩江县| 杂多县| 新化县| 奉化市| 佛坪县| 舞阳县| 大关县| 厦门市| 西乡县| 延川县| 阿拉善盟| 卫辉市| 虎林市| 四子王旗| 马边| 尼玛县| 肇庆市|