pinyin4j使用示例(支持多音字)

pinyin4j的主页:http://pinyin4j.sourceforge.net/
pinyin4j能够根据中文字符获取其对应的拼音,而且拼音的格式可以定制。
pinyin4j是一个支持将中文转换到拼音的Java开源类库。

  1. 支持简体中文和繁体中文字符;

  2. 支持转换到汉语拼音,通用拼音, 威妥玛拼音(威玛拼法), 注音符号第二式, 耶鲁拼法和国语罗马字;

  3. 支持多音字,即可以获取一个中文字符的多种发音;

  4. 支持多种字符串输出格式,比如支持Unicode格式的字符ü和声调符号(阴平 "ˉ",阳平"�@",上声"ˇ",去声"�A")的输出。

示例代码:

 

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
public class Pinyin4jUtil { 
   
     /**
      * 汉字转换位汉语拼音首字母,英文字符不变,特殊字符丢失 支持多音字,生成方式如(长沙市长:cssc,zssz,zssc,cssz)
     
      * @param chines
      *            汉字
      * @return 拼音
      */ 
     public static String converterToFirstSpell(String chines) { 
         StringBuffer pinyinName = new StringBuffer(); 
         char [] nameChar = chines.toCharArray(); 
         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); 
         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); 
         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 
         for ( int i = 0 ; i < nameChar.length; i++) { 
             if (nameChar[i] > 128 ) { 
                 try
                     // 取得当前汉字的所有全拼 
                     String[] strs = PinyinHelper.toHanyuPinyinStringArray( 
                             nameChar[i], defaultFormat); 
                     if (strs != null ) { 
                         for ( int j = 0 ; j < strs.length; j++) { 
                             // 取首字母 
                             pinyinName.append(strs[j].charAt( 0 )); 
                             if (j != strs.length - 1 ) { 
                                 pinyinName.append( "," ); 
                            
                        
                    
                     // else { 
                     // pinyinName.append(nameChar[i]); 
                     // } 
                 } catch (BadHanyuPinyinOutputFormatCombination e) { 
                     e.printStackTrace(); 
                
             } else
                 pinyinName.append(nameChar[i]); 
            
             pinyinName.append( " " ); 
        
         // return pinyinName.toString(); 
         return parseTheChineseByObject(discountTheChinese(pinyinName.toString())); 
    
   
     /**
      * 汉字转换位汉语全拼,英文字符不变,特殊字符丢失
      * 支持多音字,生成方式如(重当参:zhongdangcen,zhongdangcan,chongdangcen
      * ,chongdangshen,zhongdangshen,chongdangcan)
     
      * @param chines
      *            汉字
      * @return 拼音
      */ 
     public static String converterToSpell(String chines) { 
         StringBuffer pinyinName = new StringBuffer(); 
         char [] nameChar = chines.toCharArray(); 
         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); 
         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); 
         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 
         for ( int i = 0 ; i < nameChar.length; i++) { 
             if (nameChar[i] > 128 ) { 
                 try
                     // 取得当前汉字的所有全拼 
                     String[] strs = PinyinHelper.toHanyuPinyinStringArray( 
                             nameChar[i], defaultFormat); 
                     if (strs != null ) { 
                         for ( int j = 0 ; j < strs.length; j++) { 
                             pinyinName.append(strs[j]); 
                             if (j != strs.length - 1 ) { 
                                 pinyinName.append( "," ); 
                            
                        
                    
                 } catch (BadHanyuPinyinOutputFormatCombination e) { 
                     e.printStackTrace(); 
                
             } else
                 pinyinName.append(nameChar[i]); 
            
             pinyinName.append( " " ); 
        
         // return pinyinName.toString(); 
         return parseTheChineseByObject(discountTheChinese(pinyinName.toString())); 
    
   
     /**
      * 去除多音字重复数据
     
      * @param theStr
      * @return
      */ 
     private static List<Map<String, Integer>> discountTheChinese(String theStr) { 
         // 去除重复拼音后的拼音列表 
         List<Map<String, Integer>> mapList = new ArrayList<Map<String, Integer>>(); 
         // 用于处理每个字的多音字,去掉重复 
         Map<String, Integer> onlyOne = null
         String[] firsts = theStr.split( " " ); 
         // 读出每个汉字的拼音 
         for (String str : firsts) { 
             onlyOne = new Hashtable<String, Integer>(); 
             String[] china = str.split( "," ); 
             // 多音字处理 
             for (String s : china) { 
                 Integer count = onlyOne.get(s); 
                 if (count == null ) { 
                     onlyOne.put(s, new Integer( 1 )); 
                 } else
                     onlyOne.remove(s); 
                     count++; 
                     onlyOne.put(s, count); 
                
            
             mapList.add(onlyOne); 
        
         return mapList; 
    
   
     /**
      * 解析并组合拼音,对象合并方案(推荐使用)
     
      * @return
      */ 
     private static String parseTheChineseByObject( 
             List<Map<String, Integer>> list) { 
         Map<String, Integer> first = null ; // 用于统计每一次,集合组合数据 
         // 遍历每一组集合 
         for ( int i = 0 ; i < list.size(); i++) { 
             // 每一组集合与上一次组合的Map 
             Map<String, Integer> temp = new Hashtable<String, Integer>(); 
             // 第一次循环,first为空 
             if (first != null ) { 
                 // 取出上次组合与此次集合的字符,并保存 
                 for (String s : first.keySet()) { 
                     for (String s1 : list.get(i).keySet()) { 
                         String str = s + s1; 
                         temp.put(str, 1 ); 
                    
                
                 // 清理上一次组合数据 
                 if (temp != null && temp.size() > 0 ) { 
                     first.clear(); 
                
             } else
                 for (String s : list.get(i).keySet()) { 
                     String str = s; 
                     temp.put(str, 1 ); 
                
            
             // 保存组合数据以便下次循环使用 
             if (temp != null && temp.size() > 0 ) { 
                 first = temp; 
            
        
         String returnStr = ""
         if (first != null ) { 
             // 遍历取出组合字符串 
             for (String str : first.keySet()) { 
                 returnStr += (str + "," ); 
            
        
         if (returnStr.length() > 0 ) { 
             returnStr = returnStr.substring( 0 , returnStr.length() - 1 ); 
        
         return returnStr; 
    
   
}


测试代码

 

?

1
2
3
4
5
6
7
String str = "长沙市长"
           
         String pinyin = Pinyin4jUtil.converterToSpell(str); 
         System.out.println(str+ " pin yin :" +pinyin); 
           
         pinyin = Pinyin4jUtil.converterToFirstSpell(str); 
         System.out.println(str+ " short pin yin :" +pinyin);


运行结果:

长沙市长 pin yin :zhangshashichang,changshashichang,zhangshashizhang,changshashizhang
长沙市长 short pin yin :cssc,zssz,zssc,cssz

你可能感兴趣的:(多音字,中文转换,简体中文,汉语拼音,繁体中文)