當前位置: 首頁>>代碼示例>>Java>>正文


Java PinyinHelper.toHanyuPinyinStringArray方法代碼示例

本文整理匯總了Java中net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray方法的典型用法代碼示例。如果您正苦於以下問題:Java PinyinHelper.toHanyuPinyinStringArray方法的具體用法?Java PinyinHelper.toHanyuPinyinStringArray怎麽用?Java PinyinHelper.toHanyuPinyinStringArray使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net.sourceforge.pinyin4j.PinyinHelper的用法示例。


在下文中一共展示了PinyinHelper.toHanyuPinyinStringArray方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getAllPinYin

import net.sourceforge.pinyin4j.PinyinHelper; //導入方法依賴的package包/類
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());
}
 
開發者ID:ysc,項目名稱:short-text-search,代碼行數:26,代碼來源:ShortTextSearcher.java

示例2: chineseToPinyin

import net.sourceforge.pinyin4j.PinyinHelper; //導入方法依賴的package包/類
public static String chineseToPinyin(String str) {
    if (str == null || StringUtils.isEmpty(str)) {
        return str;
    }

    StringBuilder sb = new StringBuilder();
    for (char c : str.toCharArray()) {
        try {
            String[] result = PinyinHelper.toHanyuPinyinStringArray(c,
                                                                    format);
            if (result != null) {
                sb.append(result[0]);
            }
        }
        catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }

    }
    return sb.toString();
}
 
開發者ID:melthaw,項目名稱:spring-backend-boilerplate,代碼行數:22,代碼來源:I18nUtils.java

示例3: getPinYin

import net.sourceforge.pinyin4j.PinyinHelper; //導入方法依賴的package包/類
/**
 * 將漢字轉換為全拚
 *
 * @param src 源漢字
 * @return String pin yin
 */
public static String getPinYin(String src) {
    char[] 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();
    try {
        for (char aT1 : t1) {
            // 判斷是否為漢字字符
            if (Character.toString(aT1).matches("[\\u4E00-\\u9FA5]+")) {
                t2 = PinyinHelper.toHanyuPinyinStringArray(aT1, t3);// 將漢字的幾種全拚都存到t2數組中
                t4.append(t2[0]);// 取出該漢字全拚的第一種讀音並連接到字符串t4後
            } else {
                // 如果不是漢字字符,直接取出字符並連接到字符串t4後
                t4.append(Character.toString(aT1));
            }
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        LOGGER.error("", e);
    }
    return t4.toString();
}
 
開發者ID:ruyangit,項目名稱:angit,代碼行數:32,代碼來源:PinyinUtil.java

示例4: getSinglePinyin

import net.sourceforge.pinyin4j.PinyinHelper; //導入方法依賴的package包/類
/**
 * 字到拚音
 * @param zhWord
 * @return
 */
public static String getSinglePinyin(char zhWord){
    String pinyin[] = new String[0];
    if (zhWord=='\0'){  // 判定字符是否為空
        return "";
    }
    if (zhWord=='(' || zhWord==')' || zhWord=='/'){
        return "";
    }
    HanyuPinyinOutputFormat hanyuPinyinOutputFormat = new HanyuPinyinOutputFormat();
    hanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    hanyuPinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
    try {
        pinyin= PinyinHelper.toHanyuPinyinStringArray(zhWord,hanyuPinyinOutputFormat);
    } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
        badHanyuPinyinOutputFormatCombination.printStackTrace();
    }
    return  pinyin[0];
}
 
開發者ID:wellCh4n,項目名稱:Anti-Evil,代碼行數:24,代碼來源:PinyinQuery.java

示例5: getFirstLetters

import net.sourceforge.pinyin4j.PinyinHelper; //導入方法依賴的package包/類
public static String getFirstLetters(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();
}
 
開發者ID:huang303513,項目名稱:Coding-Android,代碼行數:23,代碼來源:UserObject.java

示例6: getPingYin

import net.sourceforge.pinyin4j.PinyinHelper; //導入方法依賴的package包/類
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 t4;
}
 
開發者ID:chengxp3,項目名稱:galaxy,代碼行數:27,代碼來源:StockChineseToPinyin.java

示例7: toHanyuPinyin

import net.sourceforge.pinyin4j.PinyinHelper; //導入方法依賴的package包/類
/**
 * 
 * @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();
}
 
開發者ID:summerDp,項目名稱:dpCms,代碼行數:30,代碼來源:Pinyin4jUtil.java

示例8: converterToSpell

import net.sourceforge.pinyin4j.PinyinHelper; //導入方法依賴的package包/類
/**
 * 漢字轉換位漢語拚音,英文字符不變
 * 
 * @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;
}
 
開發者ID:haizicq,項目名稱:osframe,代碼行數:28,代碼來源:PinyinUtil.java

示例9: getPinYin

import net.sourceforge.pinyin4j.PinyinHelper; //導入方法依賴的package包/類
public static String getPinYin(String strs) {

        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); // WITH_TONE_NUMBER/WITHOUT_TONE/WITH_TONE_MARK
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        char[] ch = strs.trim().toCharArray();
        StringBuffer buffer = new StringBuffer("");

        try {
            for (int i = 0; i < ch.length; i++) {
                // unicode,bytes應該也可以.
                if (Character.toString(ch[i]).matches("[\u4e00-\u9fa5]+")) {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(ch[i], format);
                    buffer.append(temp[0]);
                    //buffer.append(" ");
                } else {
                    buffer.append(Character.toString(ch[i]));
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }
 
開發者ID:xuxueli,項目名稱:xxl-incubator,代碼行數:26,代碼來源:PinyinUtil.java

示例10: getFirstLetter

import net.sourceforge.pinyin4j.PinyinHelper; //導入方法依賴的package包/類
/**
 * 獲取大寫首字母
 *
 * @param str
 * @return
 */
public static String getFirstLetter(String str) {
    // trim
    if (str == null) {
        return "";
    }
    str = str.trim();

    String convert = "";

    char word = str.charAt(0);
    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);

    if (pinyinArray != null) {
        convert += pinyinArray[0].charAt(0);
    }
    else {
        convert += word;
    }

    return convert.toUpperCase();
}
 
開發者ID:xuxueli,項目名稱:xxl-incubator,代碼行數:28,代碼來源:PinyinUtil.java

示例11: cn2FirstEn

import net.sourceforge.pinyin4j.PinyinHelper; //導入方法依賴的package包/類
/** 
 * 獲取漢字串拚音首字母,英文字符不變 
 * 
 * @param chinese 漢字串 
 * @return 漢語拚音首字母 
 */ 
public static String cn2FirstEn(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[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat); 
                                if (_t != null) { 
                                        pybf.append(_t[0].charAt(0)); 
                                } 
                        } catch (BadHanyuPinyinOutputFormatCombination e) { 
                                Exceptions.printException(e);; 
                        } 
                } else { 
                        pybf.append(arr[i]); 
                } 
        } 
        return pybf.toString().replaceAll("\\W", "").trim(); 
}
 
開發者ID:simbest,項目名稱:simbest-cores,代碼行數:29,代碼來源:AppCodeGenerator.java

示例12: testIsChinese

import net.sourceforge.pinyin4j.PinyinHelper; //導入方法依賴的package包/類
@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));
        }
    }
}
 
開發者ID:promeG,項目名稱:TinyPinyin,代碼行數:22,代碼來源:PinyinTest.java

示例13: getPinyinOfHanyu

import net.sourceforge.pinyin4j.PinyinHelper; //導入方法依賴的package包/類
/**
 * 傳入中文,獲得該中文的漢語拚音
 * 
 * @param chineseStr 需要轉換的中文字符串
 * @return 拚音字符串,且都是大寫。或者是長度為0的字符串
 * @throws BadHanyuPinyinOutputFormatCombination
 */
public static String getPinyinOfHanyu(String chineseStr) throws BadHanyuPinyinOutputFormatCombination {
	StringBuilder zhongWenPinYin = new StringBuilder();   
       char[] inputArray = chineseStr.toCharArray();   
 
       for (int i = 0; i < inputArray.length; i++) {   
           String[] pinYin = PinyinHelper.toHanyuPinyinStringArray(inputArray[i], getDefaultOutputFormat());   

           if (pinYin != null)  // 有轉換出拚音    
           	zhongWenPinYin.append(pinYin[0]);   
           else {  // 沒有轉換出拚音,說明是非中文字符,比如英文、特殊符號等  
               zhongWenPinYin.append(inputArray[i]);
           }
       }
       
       return zhongWenPinYin.toString();
}
 
開發者ID:weijianfeng,項目名稱:StarSwipeListDemo-with-Name-Index,代碼行數:24,代碼來源:PinyinUtils.java

示例14: getFirstSpell

import net.sourceforge.pinyin4j.PinyinHelper; //導入方法依賴的package包/類
/**
 * ����ת��Ϊ����ƴ������ĸ��Ӣ���ַ�����
 * ��������->hhds
 * @param chines
 *            ����
 * @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 (char curchar : arr) {  
                   if (curchar > 128) {  
                           try {  
                                   String[] temp = PinyinHelper.toHanyuPinyinStringArray(curchar, defaultFormat);  
                                   if (temp != null) {  
                                           pybf.append(temp[0].charAt(0));  
                                   }  
                           } catch (BadHanyuPinyinOutputFormatCombination e) {  
                                   e.printStackTrace();  
                           }  
                   } else {  
                           pybf.append(curchar);  
                   }  
           }  
            return pybf.toString().replaceAll("\\W", "").trim();  
    }
 
開發者ID:nighthary,項目名稱:phoneContact,代碼行數:30,代碼來源:PinyinUtils.java

示例15: getPingYin

import net.sourceforge.pinyin4j.PinyinHelper; //導入方法依賴的package包/類
/**
 * 將字符串中的中文轉化為拚音,其他字符不變
 * 
 * @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;
}
 
開發者ID:ImKarl,項目名稱:ccshop,代碼行數:29,代碼來源:PinYinUtil.java


注:本文中的net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。