forked from shijiebei2009/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChineseToSpelling.java
49 lines (46 loc) · 1.45 KB
/
ChineseToSpelling.java
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
package cn.codepub.algorithms.spelling;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
/**
* <p>
* Created with IntelliJ IDEA. 2015/12/12 23:47
* </p>
* <p>
* ClassName:ChineseToSpelling
* </p>
* <p>
* Description:使用JPinyin组件将汉字转成拼音
* </P>
*
* @author Wang Xu
* @version V1.0.0
* @since V1.0.0
*/
public class ChineseToSpelling {
/**
*
*/
public static String convertChineseToSpelling(String chinese, SpellingFormat spellingFormat) {
String res = null;
switch (spellingFormat) {
case WITH_TONE_MARK:
res = PinyinHelper.convertToPinyinString(chinese, ",", PinyinFormat.WITH_TONE_MARK); // nǐ,hǎo,shì,jiè
break;
case WITH_TONE_NUMBER:
res = PinyinHelper.convertToPinyinString(chinese, ",", PinyinFormat.WITH_TONE_NUMBER); // ni3,hao3,shi4,jie4
break;
case WITHOUT_TONE:
res = PinyinHelper.convertToPinyinString(chinese, ",", PinyinFormat.WITHOUT_TONE); // ni,hao,shi,jie
break;
case SHORT_PINYIN:
res = PinyinHelper.getShortPinyin(chinese); // nhsj
break;
default:
res = "ERROR";
}
return res;
}
}
enum SpellingFormat {
WITH_TONE_MARK, WITH_TONE_NUMBER, WITHOUT_TONE, SHORT_PINYIN;
}