Java Code Examples for net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat#setVCharType()

The following examples show how to use net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat#setVCharType() . 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: StringHelper.java    From wind-im with Apache License 2.0 6 votes vote down vote up
public static String toLatinPinYin(String text) {
	// format for string
	HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
	format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 小写
	format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

	char[] input = StringUtils.trimToEmpty(text).toCharArray();
	StringBuffer result = new StringBuffer();
	try {
		for (char ch : input) {
			if (Character.toString(ch).matches("[\\u4E00-\\u9FA5]+")) {
				String[] temp = PinyinHelper.toHanyuPinyinStringArray(ch, format);
				result.append(temp[0]);
			} else
				result.append(Character.toString(ch));
		}
	} catch (BadHanyuPinyinOutputFormatCombination e) {
		logger.error("string to latin pinyin error", e);
	}
	return result.toString();
}
 
Example 2
Source File: ChineseToEnglish.java    From MeetMusic with Apache License 2.0 6 votes vote down vote up
/**
 * 返回一个字的拼音
 */
public static String toPinYin(char hanzi) {
    HanyuPinyinOutputFormat hanyuPinyin = new HanyuPinyinOutputFormat();
    hanyuPinyin.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    hanyuPinyin.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    hanyuPinyin.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
    String[] pinyinArray = null;
    try {
        //是否在汉字范围内
        if (hanzi >= 0x4e00 && hanzi <= 0x9fa5) {
            pinyinArray = PinyinHelper.toHanyuPinyinStringArray(hanzi, hanyuPinyin);
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
        Log.e(TAG, "toPinYin: hanzi = "+hanzi );
        Log.e(TAG, "toPinYin: pinyinArray.toString() = "+pinyinArray.toString() );
    }
    //将获取到的拼音返回
    if (pinyinArray != null && pinyinArray.length > 0) {
        return pinyinArray[0];
    } else {
        Log.e(TAG, "toPinYin: hanzi = "+hanzi );
        return "#";
    }
}
 
Example 3
Source File: StringHelper.java    From openzaly with Apache License 2.0 6 votes vote down vote up
public static String toLatinPinYin(String text) {
	// format for string
	HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
	format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 小写
	format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

	char[] input = StringUtils.trimToEmpty(text).toCharArray();
	StringBuffer result = new StringBuffer();
	try {
		for (char ch : input) {
			if (Character.toString(ch).matches("[\\u4E00-\\u9FA5]+")) {
				String[] temp = PinyinHelper.toHanyuPinyinStringArray(ch, format);
				result.append(temp[0]);
			} else
				result.append(Character.toString(ch));
		}
	} catch (BadHanyuPinyinOutputFormatCombination e) {
		logger.error("string to latin pinyin error", e);
	}
	return result.toString();
}
 
Example 4
Source File: StringHelper.java    From openzaly with Apache License 2.0 6 votes vote down vote up
public static String toLatinPinYin(String text) {
	// format for string
	HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
	format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 小写
	format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

	char[] input = StringUtils.trimToEmpty(text).toCharArray();
	StringBuffer result = new StringBuffer();
	try {
		for (char ch : input) {
			if (Character.toString(ch).matches("[\\u4E00-\\u9FA5]+")) {
				String[] temp = PinyinHelper.toHanyuPinyinStringArray(ch, format);
				result.append(temp[0]);
			} else
				result.append(Character.toString(ch));
		}
	} catch (BadHanyuPinyinOutputFormatCombination e) {
		logger.error("string to latin pinyin error", e);
	}
	return result.toString();
}
 
Example 5
Source File: ChineseToEnglish.java    From AndroidDemo with MIT License 6 votes vote down vote up
/**
 * 返回一个字的拼音
 */
public static String toPinYin(char hanzi) {
    HanyuPinyinOutputFormat hanyuPinyin = new HanyuPinyinOutputFormat();
    hanyuPinyin.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    hanyuPinyin.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    hanyuPinyin.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
    String[] pinyinArray = null;
    try {
        //是否在汉字范围内
        if (hanzi >= 0x4e00 && hanzi <= 0x9fa5) {
            pinyinArray = PinyinHelper.toHanyuPinyinStringArray(hanzi, hanyuPinyin);
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
        Log.e(TAG, "toPinYin: hanzi = "+hanzi );
        Log.e(TAG, "toPinYin: pinyinArray.toString() = "+pinyinArray.toString() );
    }
    //将获取到的拼音返回
    if (pinyinArray != null && pinyinArray.length > 0) {
        return pinyinArray[0];
    } else {
        Log.e(TAG, "toPinYin: hanzi = "+hanzi );
        return "#";
    }
}
 
Example 6
Source File: ChinesUtil.java    From utils with Apache License 2.0 6 votes vote down vote up
/**
 * 将字符串中的中文转化为拼音,其他字符不变.
 *
 * @param inputString
 * @return
 */
public static String getPingYin(String inputString) {
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    format.setVCharType(HanyuPinyinVCharType.WITH_V);

    char[] input = inputString.trim().toCharArray();
    StringBuffer output = new StringBuffer();

    try {
        for (int i = 0; i < input.length; i++) {
            if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {
                String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
                output.append(temp[0]);
            } else {
                output.append(Character.toString(input[i]));
            }
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
    }
    return output.toString();
}
 
Example 7
Source File: PinyinConverter.java    From py4j with Apache License 2.0 6 votes vote down vote up
@Override
public String[] getPinyin(char ch) throws IllegalPinyinException {
	try{
		HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
		outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

		if(ch>=32 && ch<=125){	//ASCII >=33 ASCII<=125的直接返回 ,ASCII码表:http://www.asciitable.com/
			return new String[]{String.valueOf(ch)};
		}
		return ArrayUtils.distinct(PinyinHelper.toHanyuPinyinStringArray(ch, outputFormat));
	} catch (BadHanyuPinyinOutputFormatCombination e) {
		throw new IllegalPinyinException(e);
	}

}
 
Example 8
Source File: PinyinTest.java    From TinyPinyin with Apache License 2.0 6 votes vote down vote up
@Test
public void testIsChinese() throws BadHanyuPinyinOutputFormatCombination {
    char[] allChars = allChars();
    final int allCharsLength = allChars.length;
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
    format.setVCharType(HanyuPinyinVCharType.WITH_V);

    for (int i = 0; i < allCharsLength; i++) {
        char targetChar = allChars[i];
        String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(targetChar, format);
        if (pinyins != null && pinyins.length > 0) {
            // is chinese
            assertThat(Pinyin.isChinese(targetChar), is(true));
        } else {
            // not chinese
            assertThat(Pinyin.isChinese(targetChar), is(false));
        }
    }
}
 
Example 9
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 10
Source File: PinyinTest.java    From TinyPinyin with Apache License 2.0 5 votes vote down vote up
@Test
public void testToPinyin_char() throws BadHanyuPinyinOutputFormatCombination {
    char[] allChars = allChars();
    final int allCharsLength = allChars.length;
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
    format.setVCharType(HanyuPinyinVCharType.WITH_V);

    int chineseCount = 0;
    for (int i = 0; i < allCharsLength; i++) {
        char targetChar = allChars[i];
        String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(targetChar, format);
        if (pinyins != null && pinyins.length > 0) {
            // is chinese
            chineseCount++;
            assertThat(Pinyin.toPinyin(targetChar), equalTo(pinyins[0]));
        } else {
            // not chinese
            assertThat(Pinyin.toPinyin(targetChar), equalTo(String.valueOf(targetChar)));
        }
    }

    //CHECKSTYLE:OFF
    int expectedChineseCount = 20378;
    //CHECKSTYLE:ON

    assertThat(chineseCount, is(expectedChineseCount));
}
 
Example 11
Source File: PinYin.java    From FamilyChat with Apache License 2.0 5 votes vote down vote up
/**
 * 获取汉字串拼音,英文字符不变(均返回大写)
 *
 * @return 汉语拼音
 */
public static String getFull(String src)
{
    char[] t1;
    t1 = src.toCharArray();
    String[] t2 = new String[t1.length];
    HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
    t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    t3.setVCharType(HanyuPinyinVCharType.WITH_V);
    String t4 = "";
    int t0 = t1.length;
    try
    {
        for (int i = 0; i < t0; i++)
        {
            // 判断是否为汉字字符
            if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+"))
            {
                t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                t4 += t2[0];
            } else
            {
                t4 += Character.toString(t1[i]);
            }
        }
        return t4.toUpperCase();
    } catch (Exception e1)
    {
        e1.printStackTrace();
        t4 = "";
    }
    return t4.toUpperCase();
}
 
Example 12
Source File: FileUtil.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
/**
 * 汉字转拼音
 *
 * @param src
 * @return
 */
public static String getPingYin(String src) {
    char[] t1 = null;
    t1 = src.toCharArray();
    String[] t2 = new String[t1.length];
    HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
    t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    t3.setVCharType(HanyuPinyinVCharType.WITH_V);
    String t4 = "";
    int t0 = t1.length;
    try {
        for (int i = 0; i < t0; i++) {
            // 判断是否为汉字字符
            if (java.lang.Character.toString(t1[i]).matches(
                    "[\\u4E00-\\u9FA5]+")) {
                t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                t4 += t2[0];
            } else
                t4 += java.lang.Character.toString(t1[i]);
        }
        // System.out.println(t4);
        return t4;
    } catch (BadHanyuPinyinOutputFormatCombination e1) {
        e1.printStackTrace();
    }
    return t4;
}
 
Example 13
Source File: ShortTextSearcher.java    From short-text-search with Apache License 2.0 5 votes vote down vote up
public String getPinYin(String words, boolean acronym) {
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
    char[] chars = words.trim().toCharArray();
    StringBuilder result = new StringBuilder();
    int ignoredCount = 0;
    try {
        for (char c : chars) {
            if(!isChinese(c)){
                ignoredCount++;
                result.append(c);
                continue;
            }
            String[] pinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
            if(acronym){
                result.append(pinyinStringArray[0].charAt(0));
            }else {
                String pinYin = pinyinStringArray[0].toLowerCase().replace("ü", "v");
                if(StringUtils.isBlank(pinYin)){
                    continue;
                }
                result.append(pinYin);
                charPinYin.add(pinYin);
                if(pinYin.length() > charMaxPinYinLength){
                    charMaxPinYinLength = pinYin.length();
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("获取拼音失败: "+words, e);
    }
    if(ignoredCount == chars.length){
        return null;
    }
    return result.toString().toLowerCase();
}
 
Example 14
Source File: PinyinUtil.java    From util with Apache License 2.0 5 votes vote down vote up
/**
 * 将汉字转为拼音。例如:中国转为zhongguo。
 * @param 源汉字
 * @return 转换后的拼音,如果转换异常返回null。
 */
public static String getPingYin(String src) {
	char[] t1 = null;
	t1 = src.toCharArray();
	String[] t2 = new String[t1.length];
	HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
	t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
	t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	t3.setVCharType(HanyuPinyinVCharType.WITH_V);
	String t4 = "";
	int t0 = t1.length;
	try {
		for (int i = 0; i < t0; i++) {
			// 判断是否为汉字字符
			if (java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
				t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
				t4 += t2[0];
			} else{
				t4 += java.lang.Character.toString(t1[i]);
			}
		}
		return t4;
	} catch (BadHanyuPinyinOutputFormatCombination e1) {
		e1.printStackTrace();
		return null;
	}
}
 
Example 15
Source File: PinyinUtil.java    From apkextractor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 将字符串中的中文转化为拼音,其他字符不变
 * @param inputString
 * @return
 */
public static String getPinYin(String inputString) {
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    format.setVCharType(HanyuPinyinVCharType.WITH_V);

    char[] input = inputString.trim().toCharArray();
    StringBuilder output = new StringBuilder();

    try {
        for (int i = 0; i < input.length; i++) {
            if (java.lang.Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {
                String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
                output.append(temp[0]);
            } else
                output.append(input[i]);
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
    }
    catch(java.lang.NullPointerException npex){
        npex.printStackTrace();
    }
    catch(Exception ex){
        ex.printStackTrace();
    }
    return output.toString();
}
 
Example 16
Source File: PinyinUtil.java    From hdw-dubbo with Apache License 2.0 5 votes vote down vote up
/**
 * 将汉字转换为全拼
 *
 * @param src
 * @return String
 */
public static final String getCamelPinYin(String src) {
    char[] t1 = null;
    t1 = src.toCharArray();
    String[] t2 = new String[t1.length];
    // 设置汉字拼音输出的格式
    HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
    t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    t3.setVCharType(HanyuPinyinVCharType.WITH_V);
    String t4 = "", t = "";
    int t0 = t1.length;
    try {
        for (int i = 0; i < t0; i++) {
            // 判断是否为汉字字符
            if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
                t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中
                t = t2[0];// 取出该汉字全拼的第一种读音并连接到字符串t4后
            } else {
                // 如果不是汉字字符,直接取出字符并连接到字符串t4后
                t = Character.toString(t1[i]);
            }
            t = t.substring(0, 1).toUpperCase() + t.substring(1);
            t4 += t;
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        logger.error("", e);
    }
    return t4;
}
 
Example 17
Source File: PinyinUtil.java    From hdw-dubbo with Apache License 2.0 5 votes vote down vote up
/**
 * 将汉字转换为全拼
 *
 * @param src
 * @return String
 */
public static final String getPinYin(String src) {
    if (src == null) {
        return "";
    }
    char[] t1 = null;
    t1 = src.toCharArray();
    String[] t2 = new String[t1.length];
    // 设置汉字拼音输出的格式
    HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
    t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    t3.setVCharType(HanyuPinyinVCharType.WITH_V);
    String t4 = "";
    int t0 = t1.length;
    try {
        for (int i = 0; i < t0; i++) {
            // 判断是否为汉字字符
            if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
                t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中
                t4 += t2[0];// 取出该汉字全拼的第一种读音并连接到字符串t4后
            } else {
                // 如果不是汉字字符,直接取出字符并连接到字符串t4后
                t4 += Character.toString(t1[i]);
            }
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        logger.error("", e);
    }
    return t4;
}
 
Example 18
Source File: PingYinUtils.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
/**
 * 将字符串中的中文转化为拼音,其他字符不变
 *
 * @param inputString
 * @return
 */
public static String getPingYin(String inputString) {
    HanyuPinyinOutputFormat format = new
            HanyuPinyinOutputFormat();
    format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    format.setVCharType(HanyuPinyinVCharType.WITH_V);

    char[] input = inputString.trim().toCharArray();
    String output = "";

    try {
        for (int i = 0; i < input.length; i++) {
            if (java.lang.Character.toString(input[i]).
                    matches("[\\u4E00-\\u9FA5]+")) {
                String[] temp = PinyinHelper.
                        toHanyuPinyinStringArray(input[i],
                                format);
                output += temp[0];
            } else
                output += java.lang.Character.toString(
                        input[i]);
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
    }
    return output;
}
 
Example 19
Source File: StringUtils.java    From flash-waimai with MIT License 5 votes vote down vote up
public static String getPingYin(String inputString) {
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    format.setVCharType(HanyuPinyinVCharType.WITH_V);
    String output = "";
    if (inputString != null && inputString.length() > 0
            && !"null".equals(inputString)) {
        char[] input = inputString.trim().toCharArray();
        try {
            for (int i = 0; i < input.length; i++) {
                if (java.lang.Character.toString(input[i]).matches(
                        "[\\u4E00-\\u9FA5]+")) {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(
                            input[i], format);
                    output += temp[0];
                } else {
                    output += java.lang.Character.toString(input[i]);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
    } else {
        return "*";
    }
    return output;
}
 
Example 20
Source File: PinYinUtil.java    From code with Apache License 2.0 3 votes vote down vote up
/**
 * 获取汉语拼音格式化器
 * @param caseType 大小写
 *        @see HanyuPinyinCaseType
 *         UPPERCASE:大写  (ZHONG)
 *         LOWERCASE:小写  (zhong)
 * @param toneType 音标格式
 *        @see HanyuPinyinToneType
 *         WITHOUT_TONE:无音标  (zhong)
 *         WITH_TONE_NUMBER:1-4数字表示英标  (zhong4)
 *         WITH_TONE_MARK:直接用音标符(必须WITH_U_UNICODE否则异常)  (zhòng)
 * @param charType charType
 *        @see HanyuPinyinVCharType
 *         WITH_V:用v表示ü  (nv)
 *         WITH_U_AND_COLON:用"u:"表示ü  (nu:)
 *         WITH_U_UNICODE:直接用ü (nü)
 * @return net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat
 */
public static HanyuPinyinOutputFormat getPinyinFormat(HanyuPinyinCaseType caseType, HanyuPinyinToneType toneType, HanyuPinyinVCharType charType) {
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setCaseType(caseType);
    format.setToneType(toneType);
    format.setVCharType(charType);
    return format;
}