Java Code Examples for net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination#printStackTrace()

The following examples show how to use net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination#printStackTrace() . 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: 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 2
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 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: 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 5
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 6
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 7
Source File: Pinyin4jUtil.java    From dpCms with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param inputString 赵宝东
 * @return zhaobd
 */
public static String toHanyuPinyin(String inputString){
	StringBuffer outputString = new StringBuffer();
	try {
		char[] cs = inputString.toCharArray();
		for(byte i=0;i<cs.length;i++){

			String[] hanyuPinyin = PinyinHelper.toHanyuPinyinStringArray(cs[i], hanyuPinyinOutputFormat);
			String s = hanyuPinyin[0];
	
			if(i==0){	
				outputString.append(s);
			}else if(i>0){
				outputString.append(s.substring(0, 1));
			}


		}
	}
	catch (BadHanyuPinyinOutputFormatCombination e) {
		e.printStackTrace();
	}

	return outputString.toString();
}
 
Example 8
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 9
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 10
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 11
Source File: PinyinUtil.java    From PinyinSearchLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * judge chr is kanji
 * 
 * @param chr
 * @return Is kanji return true,otherwise return false.
 */
public static boolean isKanji(char chr){
	String[] pinyinStr = null;
	try {
		pinyinStr = PinyinHelper.toHanyuPinyinStringArray(chr, format);
	} catch (BadHanyuPinyinOutputFormatCombination e) {
		e.printStackTrace();
	}
	
	return (null==pinyinStr)?(false):(true);
}
 
Example 12
Source File: PinyinUtil.java    From FancyListIndexer with Apache License 2.0 5 votes vote down vote up
/**
 * 根据汉字获取对应的拼音
 * @param str
 * @return
 */
public static String getPinyin(String str) {
	// 黑马 -> HEIMA
	// 设置输出配置
	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 > 128 || c < -127) {
			// 可能是汉字
			try {
				// 根据字符获取对应的拼音. 黑 -> HEI , 单 -> DAN , SHAN
				String s = PinyinHelper.toHanyuPinyinStringArray(c, format)[0];
				sb.append(s);

			} catch (BadHanyuPinyinOutputFormatCombination e) {
				e.printStackTrace();
			}
		} else {
			// *&$^*@654654LHKHJ
			// 不需要转换, 直接添加
			sb.append(c);
		}
	}

	return sb.toString();
}
 
Example 13
Source File: HanyuPinyin.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public String getHanyuPinYin(char c) {
    try {
        pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format);
    }
    catch(BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
    }

    // 如果c不是汉字,toHanyuPinyinStringArray会返回null
    if(pinyin == null) return null;

     //todo 多音字 取第一个
    return pinyin[0];
}
 
Example 14
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 15
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 16
Source File: PinYinHelper.java    From es-service-parent with Apache License 2.0 5 votes vote down vote up
/**
 * 将汉字解析成拼音,取首字母,英文和数字不变
 * 
 * @param word
 * @return
 */
public static String getPinYinPrefix(String word) {
    char[] ch = word.trim().toCharArray();
    StringBuilder rs = new StringBuilder();
    try {
        String s_ch;
        String[] temp;
        for (int i = 0; i < ch.length; i++) {
            s_ch = Character.toString(ch[i]);
            if (s_ch.matches("[\u4e00-\u9fa5]+")) {
                // 汉字
                temp = PinyinHelper.toHanyuPinyinStringArray(ch[i], format);
                if (null != temp && temp.length > 0) {
                    rs.append(temp[0].charAt(0));
                }
            } else if (s_ch.matches("[\u0030-\u0039]+")) {
                // 0-9
                rs.append(s_ch);
            } else if (s_ch.matches("[\u0041-\u005a]+") || s_ch.matches("[\u0061-\u007a]+")) {
                // a-zA-Z
                rs.append(s_ch);
            }
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
    }
    return rs.toString();
}
 
Example 17
Source File: PinyinUtil.java    From wetech-cms with MIT License 5 votes vote down vote up
public static String str2Pinyin(String str,String fill) {
	try {
		StringBuffer sb = new StringBuffer();
		if(fill==null) fill="";
		boolean isCn = true;
		for(int i=0;i<str.length();i++) {
			char c = str.charAt(i);
			if(i>0&&isCn) {
				sb.append(fill);
			}
			if(c==' ') {
				sb.append(fill);
			}
			//1、判断c是不是中文
			if(c>='\u4e00'&&c<='\u9fa5') {
				isCn = true;
				sb.append(PinyinHelper.toHanyuPinyinStringArray(c, format)[0]);
			} else {
				//不是中文
				if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) {
					sb.append(c);
				}
				isCn = false;
			}
		}
		return sb.toString();
	} catch (BadHanyuPinyinOutputFormatCombination e) {
		e.printStackTrace();
	}
	return null;
}
 
Example 18
Source File: ChineseUtil.java    From vscrawler with Apache License 2.0 5 votes vote down vote up
/**
 * 汉字转换位汉语拼音,英文字符不变
 * 
 * @param chines
 *            汉字
 * @return 拼音
 */
public static String converterToSpell(String chines) {
    // long start = System.currentTimeMillis();
    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]);
                } else {
                    pinyinName.append(nameChar[i]);
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        } else {
            pinyinName.append(nameChar[i]);
        }
    }
    // long end = System.currentTimeMillis();
    // System.out.println("cost time : " + (end - start));
    if (pinyinName.toString().contains("u:")) {
        return pinyinName.toString().replaceAll("u:", "v");
    } else {
        return pinyinName.toString();
    }
}
 
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 3 votes vote down vote up
/**
 * 
 * 将汉字转换成拼音
 * 
 * @param hanzi
 * 
 * @param separator
 * 
 * @return
 */

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

	// 创建汉语拼音处理类

	HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();

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

	defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

	defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

	String pinyingStr = "";

	try {

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

	} catch (BadHanyuPinyinOutputFormatCombination e) {

		e.printStackTrace();

	}

	return pinyingStr;

}