java 汉字转拼音(解决多音字问题)

时间:2025-04-25 07:54:25
  • package ;  
  •   
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  •   
  • import .;  
  •   
  • import .;  
  • import .;  
  • import .;  
  • import .;  
  • import .;  
  • import .;  
  •   
  • public class PinyinUtils {  
  •   
  •     private static final Logger logger = ("devLog");  
  •       
  •     public static Map<String,String> dictionary = new HashMap<String,String>();  
  •   
  •     //加载多音字词典  
  •     static{  
  •           
  •         BufferedReader br = null;  
  •         try {  
  •             File file = new File("./config/duoyinzi_pinyin.txt");  
  •             br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));  
  •               
  •             String line = null;  
  •             while((line=())!=null){  
  •                   
  •                 String[] arr = ("#");  
  •                   
  •                 if((arr[1])){  
  •                     String[] sems = arr[1].split(" ");  
  •                     for (String sem : sems) {  
  •                           
  •                         if((sem)){  
  •                             (sem , arr[0]);  
  •                         }  
  •                     }  
  •                 }  
  •             }  
  •               
  •         } catch (UnsupportedEncodingException e) {  
  •             ();  
  •         } catch (FileNotFoundException e) {  
  •             ();  
  •         } catch (IOException e) {  
  •             ();  
  •         }finally{  
  •             if(br!=null){  
  •                 try {  
  •                     ();  
  •                 } catch (IOException e) {  
  •                     ();  
  •                 }  
  •             }  
  •         }  
  •   
  •     }  
  •       
  •     public static String[] chineseToPinYin(char chineseCharacter) throws BadHanyuPinyinOutputFormatCombination{  
  •         HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();  
  •         (HanyuPinyinToneType.WITHOUT_TONE);  
  •         ();  
  •         (HanyuPinyinVCharType.WITH_V);  
  •           
  •         if(chineseCharacter>=32 && chineseCharacter<=125){    //ASCII >=33 ASCII<=125的直接返回 ,ASCII码表:/  
  •             return new String[]{(chineseCharacter)};  
  •         }  
  •           
  •         return (chineseCharacter, outputFormat);  
  •     }  
  •       
  •     /** 
  •      * 获取汉字拼音的全拼 
  •      * @param chineseCharacter 
  •      * @return 
  •      * @throws BadHanyuPinyinOutputFormatCombination 
  •      */  
  •     public static String chineseToPinYinF(String chineseCharacter) throws BadHanyuPinyinOutputFormatCombination{  
  •         if((chineseCharacter)){  
  •             return null;  
  •         }  
  •           
  •         char[] chs = ();  
  •           
  •         StringBuilder result = new StringBuilder();  
  •           
  •         for(int i=0;i<;i++){  
  •             String[] arr = chineseToPinYin(chs[i]);  
  •             if(arr==null){  
  •                 ("");  
  •             }else if(==1){  
  •                 (arr[0]);  
  •             }else if(arr[0].equals(arr[1])){  
  •                 (arr[0]);  
  •             }else{  
  •                   
  •                 String prim = (i, i+1);  
  • //              ("prim="+prim+"**i="+i);  
  •                   
  •                 String lst = null,rst = null;  
  •                   
  •                 if(i<=()-2){  
  •                     rst = (i,i+2);  
  •                 }  
  •                 if(i>=1 && i+1<=()){  
  •                     lst = (i-1,i+1);  
  •                 }  
  •                   
  • //              ("lst="+lst+"**rst="+rst);  
  •                   
  •                 String answer = null;  
  •                 for (String py : arr) {  
  •                       
  •                     if((py)){  
  •                         continue;  
  •                     }  
  •                       
  •                     if((lst!=null && ((lst))) ||  
  •                             (rst!=null && ((rst)))){  
  •                         answer = py;  
  • //                      ("get it,answer="+answer+",i="+i+"**break");  
  •                         break;  
  •                     }  
  •                       
  •                     if(((prim))){  
  •                         answer = py;  
  • //                      ("get it,answer="+answer+",i="+i+"**prim="+prim);  
  •                     }  
  •                 }  
  •                 if(answer!=null){  
  •                     (answer);  
  •                 }else{  
  •                     ("no answer ch="+chs[i]);  
  •                 }  
  •             }  
  •         }  
  •           
  •         return ().toLowerCase();  
  •     }  
  •       
  •     public static String chineseToPinYinS(String chineseCharacter) throws BadHanyuPinyinOutputFormatCombination{  
  •         if((chineseCharacter)){  
  •             return null;  
  •         }  
  •           
  •         char[] chs = ();  
  •           
  •         StringBuilder result = new StringBuilder();  
  •           
  •         for(int i=0;i<;i++){  
  •             String[] arr = chineseToPinYin(chs[i]);  
  •             if(arr==null){  
  •                 ("");  
  •             }else if(==1){  
  •                 (arr[0].charAt(0));  
  •             }else if(arr[0].equals(arr[1])){  
  •                 (arr[0].charAt(0));  
  •             }else{  
  •                   
  •                 String prim = (i, i+1);  
  • //              ("prim="+prim+"**i="+i);  
  •                   
  •                 String lst = null,rst = null;  
  •                   
  •                 if(i<=()-2){  
  •                     rst = (i,i+2);  
  •                 }  
  •                 if(i>=1 && i+1<=()){  
  •                     lst = (i-1,i+1);  
  •                 }  
  •                   
  • //              ("lst="+lst+"**rst="+rst);  
  •                   
  •                 String answer = null;  
  •                 for (String py : arr) {  
  •                       
  •                     if((py)){  
  •                         continue;  
  •                     }  
  •                       
  •                     if((lst!=null && ((lst))) ||  
  •                             (rst!=null && ((rst)))){  
  •                         answer = py;  
  • //                      ("get it,answer="+answer+",i="+i+"**break");  
  •                         break;  
  •                     }  
  •                       
  •                     if(((prim))){  
  •                         answer = py;  
  • //                      ("get it,answer="+answer+",i="+i+"**prim="+prim);  
  •                     }  
  •                 }  
  •                 if(answer!=null){  
  •                     ((0));  
  •                 }else{  
  •                     ("no answer ch="+chs[i]);  
  •                 }  
  •             }  
  •         }  
  •           
  •         return ().toLowerCase();  
  •     }  
  •           
  • }