net.sourceforge.pinyin4j.format.HanyuPinyinVCharType Java Examples

The following examples show how to use net.sourceforge.pinyin4j.format.HanyuPinyinVCharType. 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: ShortTextSearcher.java    From short-text-search with Apache License 2.0 6 votes vote down vote up
public List<String> getAllPinYin(char c){
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
    Set<String> set = new HashSet<>();
    try {
        String[] pinYinStringArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
        for(String pinYin : pinYinStringArray){
            pinYin = pinYin.toLowerCase().replace("ü", "v");
            if(StringUtils.isBlank(pinYin)){
                continue;
            }
            set.add(pinYin);
            set.add(String.valueOf(pinYin.charAt(0)));
            charPinYin.add(pinYin);
            if(pinYin.length() > charMaxPinYinLength){
                charMaxPinYinLength = pinYin.length();
            }
        }
    }catch (Exception e){
        LOGGER.error("获取拼音失败", e);
    }
    return set.stream().sorted().collect(Collectors.toList());
}
 
Example #2
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 #3
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 #4
Source File: PinyinUtils.java    From springboot-admin with Apache License 2.0 6 votes vote down vote up
/**
 * 将文字转为汉语拼音
 * @param ChineseLanguage 要转成拼音的中文
 */
private static String toHanyuPinyin(String ChineseLanguage, HanyuPinyinCaseType caseType){
    char[] cl_chars = ChineseLanguage.trim().toCharArray();
    String hanyupinyin = "";
    HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
    defaultFormat.setCaseType(caseType);
    defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
    defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V) ;
    try {
        for (int i=0; i<cl_chars.length; i++){
            if (String.valueOf(cl_chars[i]).matches("[\u4e00-\u9fa5]+")){// 如果字符是中文,则将中文转为汉语拼音
                hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];
            } else {// 如果字符不是中文,则不转换
                hanyupinyin += cl_chars[i];
            }
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
    }
    return hanyupinyin;
}
 
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: 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 #8
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 #9
Source File: PinYinUtil.java    From MicroCommunity 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();
    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 #10
Source File: PinyinTagging.java    From word with Apache License 2.0 6 votes vote down vote up
private static 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();
    try {
        for (char c : chars) {
            if (Character.toString(c).matches("[\u4e00-\u9fa5]+")) {
                String[] pinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
                if(acronym){
                    result.append(pinyinStringArray[0].charAt(0));
                }else {
                    result.append(pinyinStringArray[0]);
                }
            }else{
                return null;
            }
        }
    } catch (Exception e) {
        LOGGER.error("拼音标注失败", e);
    }
    return result.toString();
}
 
Example #11
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 #12
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 #13
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 #14
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 #15
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 #16
Source File: PinyinUtil.java    From jeewx with Apache License 2.0 5 votes vote down vote up
/**
 * 获取拼音集合
 * 
 * @author wyh
 * @param src
 * @return Set<String>
 */
public static Set<String> getPinyin(String src) {
	if (src != null && !src.trim().equalsIgnoreCase("")) {
		char[] srcChar;
		srcChar = src.toCharArray();
		// 汉语拼音格式输出类
		HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();

		// 输出设置,大小写,音标方式等
		hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

		String[][] temp = new String[src.length()][];
		for (int i = 0; i < srcChar.length; i++) {
			char c = srcChar[i];
			// 是中文或者a-z或者A-Z转换拼音(我的需求,是保留中文或者a-z或者A-Z)
			if (String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")) {
				try {
					temp[i] = PinyinHelper.toHanyuPinyinStringArray(
							srcChar[i], hanYuPinOutputFormat);
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else if (((int) c >= 65 && (int) c <= 90)
					|| ((int) c >= 97 && (int) c <= 122)) {
				temp[i] = new String[] { String.valueOf(srcChar[i]) };
			} else {
				temp[i] = new String[] { "" };
			}
		}
		String[] pingyinArray = Exchange(temp);
		Set<String> pinyinSet = new HashSet<String>();
		for (int i = 0; i < pingyinArray.length; i++) {
			pinyinSet.add(pingyinArray[i]);
		}
		return pinyinSet;
	}
	return null;
}
 
Example #17
Source File: PinyinUtil.java    From jeecg with Apache License 2.0 5 votes vote down vote up
/**
 * 获取拼音集合
 * 
 * @author wyh
 * @param src
 * @return Set<String>
 */
public static Set<String> getPinyin(String src) {
	if (src != null && !src.trim().equalsIgnoreCase("")) {
		char[] srcChar;
		srcChar = src.toCharArray();
		// 汉语拼音格式输出类
		HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();

		// 输出设置,大小写,音标方式等
		hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

		String[][] temp = new String[src.length()][];
		for (int i = 0; i < srcChar.length; i++) {
			char c = srcChar[i];
			// 是中文或者a-z或者A-Z转换拼音(我的需求,是保留中文或者a-z或者A-Z)
			if (String.valueOf(c).matches("[\\u4E00-\\u9FA5]+")) {
				try {
					temp[i] = PinyinHelper.toHanyuPinyinStringArray(
							srcChar[i], hanYuPinOutputFormat);
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else if (((int) c >= 65 && (int) c <= 90)
					|| ((int) c >= 97 && (int) c <= 122)) {
				temp[i] = new String[] { String.valueOf(srcChar[i]) };
			} else {
				temp[i] = new String[] { "" };
			}
		}
		String[] pingyinArray = Exchange(temp);
		Set<String> pinyinSet = new HashSet<String>();
		for (int i = 0; i < pingyinArray.length; i++) {
			pinyinSet.add(pingyinArray[i]);
		}
		return pinyinSet;
	}
	return null;
}
 
Example #18
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 #19
Source File: CharacterParser.java    From LoveTalkClient with Apache License 2.0 5 votes vote down vote up
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 #20
Source File: PinyinUtil.java    From jeecg 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 #21
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 #22
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 #23
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 #24
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 #25
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 #26
Source File: Utils.java    From WeSync with MIT License 5 votes vote down vote up
/**
 * 将汉字转换为全拼
 *
 * @param src
 * @return
 */
public static String getPingYin(String src) {

    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);
    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;
    } catch (BadHanyuPinyinOutputFormatCombination e1) {
        e1.printStackTrace();
    }
    return t4;
}
 
Example #27
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 #28
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 #29
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 #30
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;
}