android项目中如何将汉字转换为拼音

android项目中如何将汉字转换为拼音?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

创新互联建站2013年开创至今,先为新华等服务建站,新华等地企业,进行企业商务咨询服务。为新华企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

代码如下

 /** * 汉字转成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 initDictionary(){
  String fileName = "py4j.dic";
  InputStreamReader inputReader = null;
  BufferedReader bufferedReader = null;
  HashMap polyphoneMap = new HashMap();
  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项目中如何将汉字转换为拼音的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


本文名称:android项目中如何将汉字转换为拼音
转载来于:http://azwzsj.com/article/joseeg.html