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

首頁 > 編程 > Java > 正文

java實(shí)現(xiàn)漢字轉(zhuǎn)拼音

2019-11-26 14:43:54
字體:
供稿:網(wǎng)友

一、問題描述
漢字轉(zhuǎn)化為對應(yīng)的拼音或者獲取漢字拼音的首字母,這些都是在開發(fā)中經(jīng)常遇到的問題,在獲取漢字的拼音或者拼音的首字母之后,我們在推薦或者搜索部門可以很大程度提高用戶的體驗(yàn),比如用戶輸入“NH”,我們就可以聯(lián)想出“你好”、“你會”、“年后”、“內(nèi)涵”等詞語。在Java中,pinyin4j.jar這個(gè)工具很好實(shí)現(xiàn)了將漢字轉(zhuǎn)化為對應(yīng)的拼音,下面我們就介紹下如何使用這個(gè)jar包。

二、資源下載
下載之后解壓,直接使用文件中的pinyin4j-2.5.0.jar即可。

三、提供方法
我們可以使用HanyuPinyinOutputFormat類來設(shè)置拼音的返回方式,比如設(shè)置拼音的大小寫、音標(biāo)方式以及拼音ü的顯示形式,具體如下圖:

直接使用PinyinHelper中的方法來對漢字做對應(yīng)的轉(zhuǎn)化,具體有如下三種,三種效果如何自己做下測試即可:

四、編寫代碼
針對我們平常可能用到的功能,我做了如下的封裝,提供的功能還有具體的實(shí)現(xiàn)步驟參照代碼中的注釋:

package com.lulei.util; import java.util.ArrayList; import java.util.List;  import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;   public class PinYinUtil {   private static HanyuPinyinOutputFormat format = null;   static {     format = new HanyuPinyinOutputFormat();     //拼音小寫     format.setCaseType(HanyuPinyinCaseType.LOWERCASE);     //無音標(biāo)方式;WITH_TONE_NUMBER:1-4數(shù)字表示英標(biāo);WITH_TONE_MARK:直接用音標(biāo)符(必須WITH_U_UNICODE否則異常     format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);     //用v表示ü     format.setVCharType(HanyuPinyinVCharType.WITH_V);   }      /**    * @param str    * @return    * @Description: 返回字符串的拼音    */   public static String[] getCharPinYinString(String str) {     if (str == null || str.length() < 1) {       return null;     }     List<String> result = new ArrayList<String>();     //對字符串中的記錄逐個(gè)分析     for (int i = 0; i < str.length(); i++) {       result = getCharPinYinString(str.charAt(i), result);     }     return result.toArray(new String[result.size()]);   }      /**    * @param c    * @param list    * @return    * @Description: 將字符c的拼音拼接到list中的記錄中    */   private static List<String> getCharPinYinString(char c, List<String> list) {     String[] strs = getCharPinYinString(c);     List<String> result = new ArrayList<String>();     //如果解析出的拼音為空,判斷字符C是否為英文字母,如果是英文字母則添加值拼音結(jié)果中     if (strs == null) {       if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {         c = c <= 91 ? (char)(c + 32) : c;         if (list == null || list.size() == 0) {           result.add(c + "");         } else {           for (String s : list) {             result.add(s + c);           }         }         return result;       }       return list;     }     //將字符C的拼音首和已存在的拼音首組合成新的記錄     for (String str : strs) {       if (list == null || list.size() == 0) {         result.add(str);       } else {         for (String s : list) {           result.add(s + str);         }       }     }     return result;   }      /**    * @param c    * @return    * @Description: 返回漢字的拼音    */   public static String[] getCharPinYinString(char c) {     try {       //返回字符C的拼音       return PinyinHelper.toHanyuPinyinStringArray(c, format);     } catch (Exception e) {       e.printStackTrace();     }     return null;   }      /**    * @param str    * @return    * @Description: 返回字符串的拼音的首字母    */   public static String[] getCharPinYinChar(String str) {     if (str == null || str.length() < 1) {       return null;     }     List<String> result = new ArrayList<String>();     //對字符串中的記錄逐個(gè)分析     for (int i = 0; i < str.length(); i++) {       result = getCharPinYinChar(str.charAt(i), result);     }     return result.toArray(new String[result.size()]);   }      /**    * @param c    * @param list    * @return    * @Description: 將字符c的拼音首字母拼接到list中的記錄中    */   private static List<String> getCharPinYinChar(char c, List<String> list) {     char[] chars = getCharPinYinChar(c);     List<String> result = new ArrayList<String>();     //如果解析出的拼音為空,判斷字符C是否為英文字母,如果是英文字母則添加值拼音結(jié)果中     if (chars == null) {       if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {         c = c < 91 ? (char)(c + 32) : c;         if (list == null || list.size() == 0) {           result.add(c + "");         } else {           for (String s : list) {             result.add(s + c);           }         }         return result;       }       return list;     }     //將字符C的拼音首字母和已存在的拼音首字母組合成新的記錄     for (char ch : chars) {       if (list == null || list.size() == 0) {         result.add(ch + "");       } else {         for (String s : list) {           result.add(s + ch);         }       }     }     return result;   }      /**    * @param c    * @return    * @Description:返回漢字拼音首字母    */   public static char[] getCharPinYinChar(char c) {     //字符C的拼音     String[] strs = getCharPinYinString(c);     if (strs != null) {       //截取拼音的首字母       char[] chars = new char[strs.length];       for(int i = 0; i <chars.length; i++) {         chars[i] = strs[i].charAt(0);       }       return chars;     }     return null;   }      public static void main(String[] args) {     // TODO Auto-generated method stub      char c = "重慶".charAt(0);     String[] str = PinYinUtil.getCharPinYinString(c);     for(String s : str) {       System.out.println(s);     }          char[] chars = PinYinUtil.getCharPinYinChar(c);     for(char c1 : chars) {       System.out.println(c1);     }          str = PinYinUtil.getCharPinYinString("重慶c");     for(String s : str) {       System.out.println(s);     }          str = PinYinUtil.getCharPinYinChar("重慶a");     for(String s : str) {       System.out.println(s);     }   }  } 

五、輸出結(jié)果

以上就是java實(shí)現(xiàn)漢字轉(zhuǎn)拼音的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 闽侯县| 攀枝花市| 宁陕县| 乌拉特后旗| 章丘市| 西峡县| 句容市| 永顺县| 黄冈市| 尼木县| 琼中| 肃北| 措勤县| 绵竹市| 鄯善县| 新津县| 河间市| 四川省| 阿尔山市| 额敏县| 济阳县| 平南县| 铁力市| 临沭县| 南漳县| 铅山县| 梧州市| 修文县| 丹棱县| 滕州市| 清新县| 云浮市| 曲阳县| 天长市| 肇州县| 安丘市| 绥芬河市| 江北区| 钟山县| 甘孜县| 四会市|