给定一个汉字句子,可以输出句子的读音。可以借鉴第三方库:pinyin4j 。这个是网址:https://mvnrepository.com/artifact/com.belerweb/pinyin4j 。要求工程是Maven项目。
第一次使用创建maven项目,在eclipse的配置上就花费了不少的时间。
依赖:
<!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j --> <dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency>
测试类:
package it.qijian.cn; import net.sourceforge.pinyin4j.*; import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; public class chinasetopinyin { public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination { String chineseString = "中国加油,武汉加油!"; HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); /* * HanyuPinyinVCharType := WITH_U_AND_COLON HanyuPinyinCaseType := LOWERCASE HanyuPinyinToneType := WITH_TONE_NUMBER */ format.setCaseType(HanyuPinyinCaseType.LOWERCASE); /* Options Output WITH_TONE_NUMBER da3 WITHOUT_TONE da WITH_TONE_MARK d菐 */ format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK); /* Options Output WITH_U_AND_COLON u: WITH_V v WITH_U_UNICODE 眉 */ format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); /* * A class provides several utility functions to convert Chinese characters * (both Simplified and Tranditional) into various Chinese Romanizationrepresentations */ String pinyinString = PinyinHelper.toHanyuPinyinString(chineseString, format," "); System.out.println(chineseString); System.out.println(pinyinString); } }
运行结果:
很明显没有完美的完成这个题目,继续努力。