net.sourceforge.pinyin4j.format.HanyuPinyinToneType Java Examples

The following examples show how to use net.sourceforge.pinyin4j.format.HanyuPinyinToneType. 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: 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 #2
Source File: PinYinUtil.java    From MicroCommunity with Apache License 2.0 6 votes vote down vote up
/**
 * 获取汉字串拼音首字母,英文字符不变
 * @param chinese 汉字串
 * @return 汉语拼音首字母
 */
public static String getFirstSpell(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 {
                String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
                if (temp != null) {
                    pybf.append(temp[0].charAt(0));
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        } else {
            pybf.append(arr[i]);
        }
    }
    return pybf.toString().replaceAll("\\W", "").trim();
}
 
Example #3
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 #4
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 #5
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 #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: 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 #8
Source File: CommonUtils.java    From TestChat 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 #9
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 #10
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 #11
Source File: ChinesUtil.java    From utils with Apache License 2.0 6 votes vote down vote up
/**
 * 获取汉字串拼音首字母,英文字符不变.
 *
 * @param chinese 汉字串
 * @return 汉语拼音首字母
 */
public static String getFirstSpell(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 {
                String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
                if (temp != null) {
                    pybf.append(temp[0].charAt(0));
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        } else {
            pybf.append(arr[i]);
        }
    }
    return pybf.toString().replaceAll("\\W", "").trim();
}
 
Example #12
Source File: ChinesUtil.java    From utils 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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
Source File: PinYinUtil.java    From sloth with Apache License 2.0 6 votes vote down vote up
public static String getFirstSpell(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 {
				String[] temp = PinyinHelper.toHanyuPinyinStringArray(
						arr[i], defaultFormat);
				if (temp != null) {
					pybf.append(temp[0].charAt(0));
				}
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				e.printStackTrace();
			}
		} else {
			pybf.append(arr[i]);
		}
	}
	return pybf.toString().replaceAll("\\W", "").trim();
}
 
Example #22
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 #23
Source File: PinyinUtil.java    From jeecg 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 #24
Source File: PinyinUtil.java    From jeecg 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 #25
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 #26
Source File: HanyuPinyinHelper.java    From opscenter with Apache License 2.0 6 votes vote down vote up
/**
 * 取第一个汉字的第一个字符 @Title: getFirstLetter @Description: TODO @return String @throws
 */
public static String getFirstLetter(String ChineseLanguage) {
    char[] cl_chars = ChineseLanguage.trim().toCharArray();
    String hanyupinyin = "";
    HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
    defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);// 输出拼音全部大写
    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 if (str.matches("[0-9]+")) {// 如果字符是数字,取数字
            hanyupinyin += cl_chars[0];
        } else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母

            hanyupinyin += cl_chars[0];
        } else {// 否则不转换

        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        System.out.println("字符不能转成汉语拼音");
    }
    return hanyupinyin;
}
 
Example #27
Source File: HanyuPinyinHelper.java    From opscenter with Apache License 2.0 6 votes vote down vote up
public static String getPinyinString(String ChineseLanguage) {
    char[] cl_chars = ChineseLanguage.trim().toCharArray();
    String hanyupinyin = "";
    HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
    defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部大写
    defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
    try {
        for (int i = 0; i < cl_chars.length; i++) {
            String str = String.valueOf(cl_chars[i]);
            if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
                hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];
            } else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字
                hanyupinyin += cl_chars[i];
            } else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母

                hanyupinyin += cl_chars[i];
            } else {// 否则不转换
            }
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        System.out.println("字符不能转成汉语拼音");
    }
    return hanyupinyin;
}
 
Example #28
Source File: HanyuPinyinHelper.java    From opscenter with Apache License 2.0 6 votes vote down vote up
public static String getFirstLetters(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 {
        for (int i = 0; i < cl_chars.length; i++) {
            String str = String.valueOf(cl_chars[i]);
            if (str.matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
                hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0].substring(0, 1);
            } else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字
                hanyupinyin += cl_chars[i];
            } else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母
                hanyupinyin += cl_chars[i];
            } else {// 否则不转换
                hanyupinyin += cl_chars[i];// 如果是标点符号的话,带着
            }
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        System.out.println("字符不能转成汉语拼音");
    }
    return hanyupinyin;
}
 
Example #29
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 #30
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();
}