给定一个汉字句子,可以输出句子的读音。借鉴第三方库:pinyin4j 。

给定一个汉字句子,可以输出句子的读音。可以借鉴第三方库: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); 
	}
}

运行结果:
在这里插入图片描述

很明显没有完美的完成这个题目,继续努力。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值