1. Introduction
Project address: https://github.com/stuxuhai/jpinyin
JPinyin is a Java open source class library for converting Chinese characters to Pinyin. It has made some improvements based on the functions of PinYin4j.
[Main features of JPinyin]
1. Accurate and perfect font library;
Unicode code ranges from 4E00-9FA5 and 20903 Chinese characters in 3007(〇), JPinyin can convert all Chinese characters except 46 variant characters (there is no standard pinyin for variant characters) ;
2. The Pinyin conversion speed is fast;
after testing, it takes about 100 milliseconds for JPinyin to convert 20902 Chinese characters in the range of Unicode encoding from 4E00-9FA5.
3. Multiple pinyin format output support;
JPinyin supports multiple pinyin output formats: with phonetic symbols, without phonetic symbols, numbers indicating phonetic symbols and pinyin initials output format;
4. Common polyphonic word recognition;
JPinyin supports the recognition of common polyphonic words, including Phrases, idioms, place names, etc.;
5. Conversion between simplified and traditional Chinese
There are four classes in Jpinyin:
- ChineseHelper.java Chinese character conversion class
- PinyinFormat.java Pinyin format class
- PinyinHelper.java Chinese character to pinyin class
- PinyinResource.java resource file loading class
Second, the main method introduction
2.1 convertToPinyinString(String str, String separator)
/** * Convert the string to Pinyin in the corresponding format * @param str String to be converted * @param separator 拼音分隔符 * @return 字符串的拼音 */ public static String convertToPinyinString(String str, String separator, PinyinFormat pinyinFormat)
结果:
String words = "和气生财";final String separator = " "; // hé qì shēng cái (默认格式) PinyinHelper.convertToPinyinString(words, separator);
2.2 convertToPinyinString(String str, String separator, PinyinFormat pinyinFormat)
/**
* 将字符串转换成相应格式的拼音
* @param str 需要转换的字符串
* @param separator 拼音分隔符
* @param pinyinFormat 拼音格式:WITH_TONE_NUMBER--数字代表声调,WITHOUT_TONE--不带声调,WITH_TONE_MARK--带声调
* @return 字符串的拼音
*/
public static String convertToPinyinString(String str, String separator, PinyinFormat pinyinFormat)
结果:
String str = "你好世界";
PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITH_TONE_MARK); // nǐ,hǎo,shì,jiè PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITH_TONE_NUMBER); // ni3,hao3,shi4,jie4 PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITHOUT_TONE); // ni,hao,shi,jie
2.3 getShortPinyin(String str)
/** * 获取字符串对应拼音的首字母 * @param str 需要转换的字符串 * @return 对应拼音的首字母 */ public static String getShortPinyin(String str)
结果:
String str = "你好世界"; PinyinHelper.getShortPinyin(str); // nhsj
/** * 将单个汉字转换为相应格式的拼音 * @param c 需要转换成拼音的汉字 * @return 汉字的拼音 */ public static String[] convertToPinyinArray(char c)
结果:
String words = "和气生财";
// hé hè huó huò hú pinyins = PinyinHelper.convertToPinyinArray(words.toCharArray()[0]);
2.5 convertToPinyinArray(char c, PinyinFormat pinyinFormat)
/** * 将单个汉字转换为相应格式的拼音 * @param c 需要转换成拼音的汉字 * @param pinyinFormat 拼音格式:WITH_TONE_NUMBER--数字代表声调,WITHOUT_TONE--不带声调,WITH_TONE_MARK--带声调 * @return 汉字的拼音 */ public static String[] convertToPinyinArray(char c, PinyinFormat pinyinFormat)
结果:
String words = "和气生财";
// hé hè huó huò hú pinyins = PinyinHelper.convertToPinyinArray(words.toCharArray()[0], PinyinFormat.WITH_TONE_MARK);
2.6 hasMultiPinyin(char c)
/** * 判断一个汉字是否为多音字 * @param c Chinese characters * @return Judgment result, if it returns true, otherwise return false */ public static boolean hasMultiPinyin( char c)
result:
// false println(PinyinHelper.hasMultiPinyin('李'));
Source code download:
Most of the content is referenced from: http://blog.csdn.net/ekeuy/article/details/40079475?utm_source=tuicool
http://www.cnblogs.com/tianzhijiexian/p/4296896.html