Chinese and Pinyin conversion

Sometimes we need to convert Chinese into Pinyin, or convert between simplified and traditional Chinese. The jpinyin toolkit provides this function. Its implementation is actually to put the pronunciation of the word and the simplified and traditional characters into a file, and then read it. Come out, here's how to use it:

The first is that the pom file needs to be added:

        <!-- 引入拼音包 -->
  		<dependency>
		    <groupId>com.github.stuxuhai</groupId>
		    <artifactId>jpinyin</artifactId>
		    <version>1.1.8</version>
		</dependency>

Then there is the specific application in the code:

/**
 * @author panmingshuai
 * @description 
 * @Time 2018年3月15日  下午5:18:12
 *
 */
public class PinyinTest {
	public static void main(String[] args) throws PinyinException {
		/**
		 * 把字的读音转出来:
		 * PinyinFormat.WITH_TONE_MARK为[zhòng, chóng]的形式
		 * PinyinFormat.WITH_TONE_NUMBER为[zhong4, chong2]的形式
		 * PinyinFormat.WITHOUT_TONE为[zhong, chong]
		 */
		System.out.println(Arrays.toString(PinyinHelper.convertToPinyinArray('重', PinyinFormat.WITH_TONE_MARK)));
		System.out.println(Arrays.toString(PinyinHelper.convertToPinyinArray('重', PinyinFormat.WITH_TONE_NUMBER)));
		System.out.println(Arrays.toString(PinyinHelper.convertToPinyinArray('重', PinyinFormat.WITHOUT_TONE)));
		
		/**
		 * 将一段话转成拼音,“,”是每个字的分隔符,最后一个参数和上面的一样
		 */
		System.out.println(PinyinHelper.convertToPinyinString("我是你大爷", ",", PinyinFormat.WITH_TONE_MARK));
		/**
		 * 判断一个字是否是多音字
		 */
		System.out.println(PinyinHelper.hasMultiPinyin('重'));
		/**
		 * 输出一段话的首字母,例如这里的结果是:zl
		 */
		System.out.println(PinyinHelper.getShortPinyin("重量"));
		/**
		 * 将一段话中的繁体字转为简体字,这里的结果是:义义
		 */
		System.out.println(ChineseHelper.convertToSimplifiedChinese("義义"));
		/**
		 * 将一段话中的简体字转为繁体字,这里的结果是:義義
		 */
		System.out.println(ChineseHelper.convertToTraditionalChinese("義义"));
		/**
		 * 判断依据话里是否有汉字
		 */
		System.out.println(ChineseHelper.containsChinese("123我234asfs12"));
		
		
	}
}

How to use it is indicated in the comments.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325408394&siteId=291194637