您好,登錄后才能下訂單哦!
android項目中如何將漢字轉換為拼音?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
代碼如下
/** * 漢字轉成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.將單個漢字獲取的拼音再和多音字庫的hashMap進行比較,代碼如下:
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; //向右多取一個字,例如 [長]沙 if(i<=chs.length()-2){ right = chs.substring(i,i+2); if(polyphoneMap.containsKey(right)){ resultPy = polyphoneMap.get(right); } } // } String middle = null; //左右各多取一個字,例如 龍[爪]槐 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個字,如 羋月[傳],列車長 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個字,如 [長]孫無忌 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文件內容解析生成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; }
看完上述內容,你們掌握android項目中如何將漢字轉換為拼音的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。