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

The following examples show how to use net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat#setToneType() . 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: PinyinUtils.java    From springboot-admin with Apache License 2.0 6 votes vote down vote up
/**
 * 取第一个汉字的第一个字符
 * @param ChineseLanguage 要转成拼音的中文
 * @param caseType 大写还是小写
 * @return
 */
private static String getFirstLetter(String ChineseLanguage, HanyuPinyinCaseType caseType){
    char[] cl_chars = ChineseLanguage.trim().toCharArray();
    String hanyupinyin = "";
    HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
    defaultFormat.setCaseType(caseType);
    defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
    try {
        String str = String.valueOf(cl_chars[0]);
        if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
            hanyupinyin = PinyinHelper.toHanyuPinyinStringArray(cl_chars[0], defaultFormat)[0].substring(0, 1);
        } else {// 如果字符不是中文,则不转换
            hanyupinyin += cl_chars[0];
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
    }
    return hanyupinyin;
}
 
Example 2
Source File: PinYinUtil.java    From MicroCommunity with Apache License 2.0 6 votes vote down vote up
/**
 * 获取汉字串拼音,英文字符不变
 * @param chinese 汉字串
 * @return 汉语拼音
 */
public static String getFullSpell(String chinese) {
    StringBuffer pybf = new StringBuffer();
    char[] arr = chinese.toCharArray();
    HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
    defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] > 128) {
            try {
                pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        } else {
            pybf.append(arr[i]);
        }
    }
    return pybf.toString();
}
 
Example 3
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 4
Source File: PinYinUtil.java    From sloth with Apache License 2.0 6 votes vote down vote up
public static String getFullSpell(String chinese) {
	StringBuffer pybf = new StringBuffer();
	char[] arr = chinese.toCharArray();
	HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
	defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
	defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	for (int i = 0; i < arr.length; i++) {
		if (arr[i] > 128) {
			try {
				pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i],
						defaultFormat)[0]);
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				e.printStackTrace();
			}
		} else {
			pybf.append(arr[i]);
		}
	}
	return pybf.toString();
}
 
Example 5
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 6
Source File: PinyinUtil.java    From jeewx with Apache License 2.0 6 votes vote down vote up
/**
 * 汉字转换位汉语拼音,英文字符不变
 * 
 * @param chines
 *            汉字
 * @return 拼音
 */
public static String converterToSpell(String chines) {
	String pinyinName = "";
	char[] nameChar = chines.toCharArray();
	HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
	defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
	defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	for (int i = 0; i < nameChar.length; i++) {
		if (nameChar[i] > 128) {
			try {
				pinyinName += PinyinHelper.toHanyuPinyinStringArray(
						nameChar[i], defaultFormat)[0];
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				e.printStackTrace();
			}
		} else {
			pinyinName += nameChar[i];
		}
	}
	return pinyinName;
}
 
Example 7
Source File: ToolPinYin.java    From protools with Apache License 2.0 6 votes vote down vote up
/**
 * 得到 全拼
 *
 * @param src
 *
 * @return
 */
public static String getPinYin(String src) throws BadHanyuPinyinOutputFormatCombination {
    char[] t1;
    t1 = src.toCharArray();
    String[] t2;
    HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
    t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    t3.setVCharType(HanyuPinyinVCharType.WITH_V);
    StringBuilder t4 = new StringBuilder();
    for (char aT1 : t1) {
        // 判断是否为汉字字符
        if (Character.toString(aT1).matches("[\\u4E00-\\u9FA5]+")) {
            t2 = PinyinHelper.toHanyuPinyinStringArray(aT1, t3);
            t4.append(t2[0]);
        } else {
            t4.append(aT1);
        }
    }
    return t4.toString();
}
 
Example 8
Source File: HanyuPinyinHelper.java    From opscenter with Apache License 2.0 6 votes vote down vote up
/**
 * 将文字转为汉语拼音
 * 
 * @param chineselanguage 要转成拼音的中文
 */
public String toHanyuPinyin(String ChineseLanguage) {
    char[] cl_chars = ChineseLanguage.trim().toCharArray();
    String hanyupinyin = "";
    HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
    defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部小写
    defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
    defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
    try {
        for (int i = 0; i < cl_chars.length; i++) {
            if (i == 0) {
                if (String.valueOf(cl_chars[i]).matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音
                    hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];
                } else {// 如果字符不是中文,则不转换
                    hanyupinyin += cl_chars[i];
                }
            } else {
                hanyupinyin += getPinYinHeadChar(String.valueOf(cl_chars[i]));
            }

        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        System.out.println("字符不能转成汉语拼音");
    }
    return hanyupinyin;
}
 
Example 9
Source File: AppUtil.java    From NewFastFrame with Apache License 2.0 6 votes vote down vote up
private static String getSinglePinYing(char c) {
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    try {
        String[] results = PinyinHelper.toHanyuPinyinStringArray(c, format);
        if (results == null) {
            //                                不是汉字返回空
            return null;
        } else {
            //                                因为有可能是多音字
            return results[0];
        }
    } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
        badHanyuPinyinOutputFormatCombination.printStackTrace();
        return null;
    }
}
 
Example 10
Source File: PinyinUtil.java    From jeewx with Apache License 2.0 6 votes vote down vote up
/**
 * 汉字转换位汉语拼音首字母,英文字符不变
 * 
 * @param chines
 *            汉字
 * @return 拼音
 */
public static String converterToFirstSpell(String chines) {
	String pinyinName = "";
	char[] nameChar = chines.toCharArray();
	HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
	defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
	defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	for (int i = 0; i < nameChar.length; i++) {
		if (nameChar[i] > 128) {
			try {
				pinyinName += PinyinHelper.toHanyuPinyinStringArray(
						nameChar[i], defaultFormat)[0].charAt(0);
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				e.printStackTrace();
			}
		} else {
			pinyinName += nameChar[i];
		}
	}
	return pinyinName;
}
 
Example 11
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 12
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 13
Source File: PinyinUtils.java    From MaterialDesignDemo with MIT License 5 votes vote down vote up
/**
 * 根据传入的字符串(包含汉字),得到拼音
 * 沧晓 -> CANGXIAO
 * 沧  晓*& -> CANGXIAO
 * 沧晓f5 -> CANGXIAO
 *
 * @param str 字符串
 * @return
 */
public static String getPinyin(String str) {

    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

    StringBuilder sb = new StringBuilder();

    char[] charArray = str.toCharArray();
    for (int i = 0; i < charArray.length; i++) {
        char c = charArray[i];
        // 如果是空格, 跳过
        if (Character.isWhitespace(c)) {
            continue;
        }
        if (c >= -127 && c < 128 || !(c >= 0x4E00 && c <= 0x9FA5)) {
            // 肯定不是汉字
            sb.append(c);
        } else {
            String s = "";
            try {
                // 通过char得到拼音集合. 单 -> dan, shan 
                s = PinyinHelper.toHanyuPinyinStringArray(c, format)[0];
                sb.append(s);
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
                sb.append(s);
            }
        }
    }

    return sb.toString();
}
 
Example 14
Source File: PinyinUtil.java    From apkextractor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取汉字串拼音首字母,英文字符不变
 * @param chinese 汉字串
 * @return 汉语拼音首字母
 */
public static String getFirstSpell(String chinese) {
    StringBuilder pybf = new StringBuilder();
    char[] arr = chinese.toCharArray();
    HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
    defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    try{
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 128) {
                String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
                if (temp != null) {
                    pybf.append(temp[0].charAt(0));
                }

            } else {
                pybf.append(arr[i]);
            }
        }
    }
    catch (BadHanyuPinyinOutputFormatCombination bhpe) {
        bhpe.printStackTrace();
    }
    catch(java.lang.NullPointerException npex){
        npex.printStackTrace();
    }
    catch(Exception ex){
        ex.printStackTrace();
    }

    return pybf.toString().replaceAll("\\W", "").trim();
}
 
Example 15
Source File: PinyinUtil.java    From apkextractor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取汉字串拼音,英文字符不变
 * @param chinese 汉字串
 * @return 汉语拼音
 */
public static String getFullSpell(String chinese) {
    StringBuilder pybf = new StringBuilder();
    char[] arr = chinese.toCharArray();
    HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
    defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    try{
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 128) {
                pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
            } else {
                pybf.append(arr[i]);
            }
        }
    }
    catch (BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
    }
    catch(java.lang.NullPointerException npex){
        npex.printStackTrace();
    }
    catch(Exception ex){
        ex.printStackTrace();
    }
    return pybf.toString();
}
 
Example 16
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 17
Source File: ChineseUtil.java    From vscrawler with Apache License 2.0 5 votes vote down vote up
/**
 * 汉字转换位汉语拼音首字母,英文字符不变
 * 
 * @param chines
 *            汉字
 * @return 拼音
 */
public static String converterToFirstSpell(String chines) {
    StringBuilder pinyinName = new StringBuilder();
    char[] nameChar = chines.toCharArray();
    HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
    defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    for (int i = 0; i < nameChar.length; i++) {
        if (nameChar[i] > 128) {
            try {
                String[] strResult = PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat);
                if (strResult != null && strResult.length > 0) {
                    pinyinName.append(strResult[0].charAt(0));
                } else {
                    pinyinName.append(nameChar[i]);
                }

            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        } else {
            pinyinName.append(nameChar[i]);
        }
    }
    if (pinyinName.toString().contains("u:")) {
        return pinyinName.toString().replaceAll("u:", "v");
    } else {
        return pinyinName.toString();
    }
}
 
Example 18
Source File: MedicalRepositoryTest.java    From Doctor with Apache License 2.0 5 votes vote down vote up
private HanyuPinyinOutputFormat getHanyuPinyinOutputFormat() {
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    //拼音大写
    format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
    //无音标方式;WITH_TONE_NUMBER:1-4数字表示英标;WITH_TONE_MARK:直接用音标符(必须WITH_U_UNICODE否则异常
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    //用v表示ü
    format.setVCharType(HanyuPinyinVCharType.WITH_V);
    return format;
}
 
Example 19
Source File: PinyinUtil.java    From jeewx with Apache License 2.0 5 votes vote down vote up
/**
 * 将汉字转换为全拼
 * 
 * @param src
 * @return String
 */
public static String getPinYin(String src) {
	char[] t1 = null;
	t1 = src.toCharArray();
	// org.jeecgframework.core.util.LogUtil.info(t1.length);
	String[] t2 = new String[t1.length];
	// org.jeecgframework.core.util.LogUtil.info(t2.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++) {
			// 判断能否为汉字字符
			// org.jeecgframework.core.util.LogUtil.info(t1[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) {
		e.printStackTrace();
	}
	return t4;
}
 
Example 20
Source File: PinyinUtil.java    From jeewx with Apache License 2.0 2 votes vote down vote up
/**
 * 
 * 将单个字符转换成拼音
 * 
 * 
 * 
 * @param src
 * 
 * @return
 */

public static String charToPinyin(char src, boolean isPolyphone,

String separator) {

	// 创建汉语拼音处理类

	HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();

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

	defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

	defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

	StringBuffer tempPinying = new StringBuffer();

	// 如果是中文

	if (src > 128) {

		try {

			// 转换得出结果

			String[] strs = PinyinHelper.toHanyuPinyinStringArray(src,

			defaultFormat);

			// 是否查出多音字,默认是查出多音字的第一个字符

			if (isPolyphone && null != separator) {

				for (int i = 0; i < strs.length; i++) {

					tempPinying.append(strs[i]);

					if (strs.length != (i + 1)) {

						// 多音字之间用特殊符号间隔起来

						tempPinying.append(separator);

					}

				}

			} else {

				tempPinying.append(strs[0]);

			}

		} catch (BadHanyuPinyinOutputFormatCombination e) {

			e.printStackTrace();

		}

	} else {

		tempPinying.append(src);

	}

	return tempPinying.toString();

}