各个国家语言上差异较大,本世纪英语特别的流行
<!-- more -->
各个国家语言上差异较大,本世纪英语特别的流行,至少在程序员上编程就是属于外国的。是英文编写的。那么我们的汉字就是一个特例了。下面说说汉字是如何转拼音的。
如果你是maven那就方便了。
<!-- 汉字转拼音jar -->
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
HanyuPinyinOutputFormat hypy = new HanyuPinyinOutputFormat();
/**
* Restore default variable values for this class
*
* Default values are listed below:
*
* <p>
* HanyuPinyinVCharType := WITH_U_AND_COLON <br>
* HanyuPinyinCaseType := LOWERCASE <br>
* HanyuPinyinToneType := WITH_TONE_NUMBER <br>
*/
public void restoreDefault() {
vCharType = HanyuPinyinVCharType.WITH_U_AND_COLON;
caseType = HanyuPinyinCaseType.LOWERCASE;
toneType = HanyuPinyinToneType.WITH_TONE_NUMBER;
}
| LOWERCASE | |||
|---|---|---|---|
| Combination | WITH_U_AND_COLON | WITH_V | WITH_U_UNICODE |
| WITH_TONE_NUMBER | lu:3 | lv3 | lü3 |
| WITHOUT_TONE | lu: | lv | lü |
| WITH_TONE_MARK | <font color="red">throw exception</font> | <font color="red">throw exception</font> | lǚ |
| UPPERCASE | |||
|---|---|---|---|
| Combination | WITH_U_AND_COLON | WITH_V | WITH_U_UNICODE |
| WITH_TONE_NUMBER | LU:3 | LV3 | LÜ3 |
| WITHOUT_TONE | LU: | LV | LÜ |
| WITH_TONE_MARK | <font color="red">throw exception</font> | <font color="red">throw exception</font> | LǙ |
* @see HanyuPinyinVCharType * @see HanyuPinyinCaseType * @see HanyuPinyinToneType hypy.setCaseType(HanyuPinyinCaseType.LOWERCASE); hypy.setToneType(HanyuPinyinToneType.WITH_TONE_NUMBER); hypy.setVCharType(HanyuPinyinVCharType.WITH_V);
setToneType是指定我们带声调的拼音的显示。这里有三种选择
- HanyuPinyinToneType.WITH_TONE_NUMBER 通过数字标注声调 zhang1 zhang2 zhang3 zhang4 分别是一二三四声、
-HanyuPinyinToneType.WITHOUT_TONE:不带声调,就是没有声调 HanyuPinyinToneType.WITH_TONE_MARK:通过符号标注,和我们平时书写一样,像u 和 ü 就通过符号区别
PinyinHelper.toHanyuPinyinStringArray("张新华".charAt(2), hypy)[0]
就这样拼音就出来了,这个就返回出来我们汉字的拼音了。对于使用者到这里就结束了。但是出于好奇我们继续往下看看。
PinyinHelper这个单列中toHanyuPinyinStringArray
static public String[] toHanyuPinyinStringArray(char ch, HanyuPinyinOutputFormat outputFormat)
throws BadHanyuPinyinOutputFormatCombination {
return getFormattedHanyuPinyinStringArray(ch, outputFormat);
}
意思就是获取格式化后的ch的拼音
而在getFormtedHanyuPinyinStringArray方法里首先是获取未格式化的拼音,然后在对拼音格式化。
String[] pinyinStrArray = getUnformattedHanyuPinyinStringArray(ch);
如何获取未格式化的拼音是重点我们就侧重这部分
private static String[] getUnformattedHanyuPinyinStringArray(char ch) {
return ChineseToPinyinResource.getInstance().getHanyuPinyinStringArray(ch);
}
/**
* Initialize a hash-table contains <Unicode, HanyuPinyin> pairs
*/
private void initializeResource() {
try {
final String resourceName = "/pinyindb/unicode_to_hanyu_pinyin.txt";
setUnicodeToHanyuPinyinTable(new Properties());
getUnicodeToHanyuPinyinTable().load(ResourceHelper.getResourceInputStream(resourceName));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
这里面究竟是什么呢,打开你会发现其实就是拼音和Unicode的对应关系。因为在我们获取拼音的时候是首先获取他的Unicode码的getHanyuPinyinRecordFromChar(ch);,然后通过Unicode码去unicode_to_hanyu_pinyin.txt找对应关系的,至于其他文件是处理其他的语种的,时间能力有限,暂不深究!
获取未格式化的拼音之后就是处理格式了,
PinyinFormatter.formatHanyuPinyin
到这里就是根据上面的三个值进行格式化处理了,这个就是格式的问题,本文不深究。
不断学习!不断进步!