Java Code Examples for net.sourceforge.pinyin4j.PinyinHelper#toHanyuPinyinString()

The following examples show how to use net.sourceforge.pinyin4j.PinyinHelper#toHanyuPinyinString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UDFChineseToPinYin.java    From hive-third-functions with Apache License 2.0 5 votes vote down vote up
public String ConvertToPinyin(String name) {
    HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
    pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

    String result = null;
    try {
        result = PinyinHelper.toHanyuPinyinString(name, pyFormat, "");
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        return null;
    }

    return result;
}
 
Example 2
Source File: PinYinHelper.java    From es-service-parent with Apache License 2.0 5 votes vote down vote up
/**
 * 传入要分析的上行词,全拼解析,只解析汉字,英文,数字
 * 
 * @param word
 * @return
 */
public static String getPinYin(String word) {
    char[] ch = word.trim().toCharArray();
    StringBuilder rs = new StringBuilder();
    try {
        if (ch.length > 40) {
            return PinyinHelper.toHanyuPinyinString(word, format, " ");
        }
        // 解析
        String s_ch;
        String[] temp;
        for (int i = 0; i < ch.length; i++) {
            s_ch = Character.toString(ch[i]);
            if (s_ch.matches("[\u4e00-\u9fa5]+")) {
                // 汉字
                temp = PinyinHelper.toHanyuPinyinStringArray(ch[i], format);
                if (null != temp && temp.length > 0) {
                    rs.append(temp[0]);
                }
            } else if (s_ch.matches("[\u0030-\u0039]+")) {
                // 0-9
                rs.append(s_ch);
            } else if (s_ch.matches("[\u0041-\u005a]+") || s_ch.matches("[\u0061-\u007a]+")) {
                // a-zA-Z
                rs.append(s_ch);
            }
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
    }
    return rs.toString();
}
 
Example 3
Source File: PinyinDictBenchmark2.java    From TinyPinyin with Apache License 2.0 4 votes vote down vote up
@Benchmark
public void Pinyin4j_StringToPinyin() throws BadHanyuPinyinOutputFormatCombination {
    PinyinHelper.toHanyuPinyinString(inputStr, format, ",");
}
 
Example 4
Source File: PinyinUtil.java    From jeewx with Apache License 2.0 3 votes vote down vote up
/**
 * 
 * 将汉字转换成拼音
 * 
 * @param hanzi
 * 
 * @param separator
 * 
 * @return
 */

@SuppressWarnings("deprecation")
public static String hanziToPinyin(String hanzi, String separator) {

	// 创建汉语拼音处理类

	HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();

	// 输出设置,大小写,音标方式

	defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

	defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

	String pinyingStr = "";

	try {

		pinyingStr = PinyinHelper.toHanyuPinyinString(hanzi, defaultFormat,
				separator);

	} catch (BadHanyuPinyinOutputFormatCombination e) {

		e.printStackTrace();

	}

	return pinyingStr;

}