1. 程式人生 > >Pinyin4j之Pinyin4jUtils工具類

Pinyin4j之Pinyin4jUtils工具類

import org.junit.Test;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
/**
 * 漢字轉換成拼音方法
 * @author 蔡龍
 */

public class Pinyin4j {
	
HanyuPinyinOutputFormat format = null;  
	public static enum Type {  
		UPPERCASE,			  //全部大寫  
		LOWERCASE,			  //全部小寫  
		FIRSTUPPER            //首字母大寫
	}  
  
	public Pinyin4j(){  
		format = new HanyuPinyinOutputFormat();  
		format.setCaseType(HanyuPinyinCaseType.UPPERCASE);  
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
	}  
  
	/**
	 * 轉換全部大寫 
	 * @param str 字串
	 * @return str為頤和園 ,return獲取到的是YHY
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	public String toPinYinUppercase(String str) throws BadHanyuPinyinOutputFormatCombination{  
		return toPinYin(str, "", Type.UPPERCASE);  
	}  
  
	/**
	 * 轉換全部大寫
	 * @param str 字串
	 * @param spera 轉換字母間隔加的字串,如果不需要為""
	 * @return str為頤和園 ,spera為** return獲取到的是Y**H**Y
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	public String toPinYinUppercase(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{  
		return toPinYin(str, spera, Type.UPPERCASE);  
	} 
	
	/**
	 * 轉換全部小寫
	 * @param str 字串
	 * @return	str為頤和園 ,return獲取到的是yhy
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	public String toPinYinLowercase(String str) throws BadHanyuPinyinOutputFormatCombination{  
		return toPinYin(str, "", Type.LOWERCASE);  
	}  
  
	/**
	 * 轉換全部小寫
	 * @param str 字串
	 * @param spera 轉換字母間隔加的字串,如果不需要為""
	 * @return	str為頤和園 ,spera為** return獲取到的是y**h**y
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	public String toPinYinLowercase(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{  
		return toPinYin(str, spera, Type.LOWERCASE);  
	} 
	
	/** 
	 * 獲取拼音首字母(大寫)
	 * @param str 字串
	 * @return str為頤和園 ,return獲取到的是Y
	 * @throws BadHanyuPinyinOutputFormatCombination 異常資訊
	 */  
	public String toPinYinUppercaseInitials(String str) throws BadHanyuPinyinOutputFormatCombination {  
		String initials = null;
		String py = toPinYinUppercase(str); 
		if(py.length()>1){
			initials = py.substring(0, 1);
		}
		if(py.length()<=1){
			initials = py;
		}
		return initials.trim();  
	} 
	
	/** 
	 * 獲取拼音首字母(小寫)
	 * @param str 字串
	 * @return str為頤和園 ,return獲取到的是y
	 * @throws BadHanyuPinyinOutputFormatCombination 異常資訊
	 */  
	public String toPinYinLowercaseInitials(String str) throws BadHanyuPinyinOutputFormatCombination {  
		String initials = null;
		String py = toPinYinLowercase(str); 
		if(py.length()>1){
			initials = py.substring(0, 1);
		}
		if(py.length()<=1){
			initials = py;
		}
		return initials.trim();  
	} 
  
	/** 
	 * 將str轉換成拼音,如果不是漢字或者沒有對應的拼音,則不作轉換 
	 * @param str    字串
	 * @param spera  預設,可為""
	 * @param type   轉換格式
	 * @return 按照轉換格式轉換成字串
	 * @throws BadHanyuPinyinOutputFormatCombination 異常資訊 
	 */  
	public String toPinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination {  
		if(str == null || str.trim().length()==0) { 
			return ""; 
		}
		if(type == Type.UPPERCASE) { 
			format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		} else{  
			format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		}
		String py = "";  
		String temp = "";  
		String[] t;  
		for(int i=0;i<str.length();i++){  
			char c = str.charAt(i);  
			if((int)c <= 128)  {
				py += c; 
			}else{  
				t = PinyinHelper.toHanyuPinyinStringArray(c, format);  
				if(t == null) { 
					py += c;  
				}else{  
					temp = t[0];  
					if(type == Type.FIRSTUPPER) { 
						temp = t[0].toUpperCase().charAt(0)+temp.substring(1); 
					}
					if(temp.length()>=1){
					   temp = temp.substring(0, 1);
					}
						py += temp+(i==str.length()-1?"":spera);  
					}  
				}  
			}  
			return py.trim();  
		}  
}