/**
  * 转换输入汉字的首字母,多个汉字是只反回第一个汉字的首字母
  *
  * @param chinese
  * @return
  */
 public static String getFirstLetter(String chines) {
   chines = replaceString(chines);
   String pinyinName = "";
   char[] nameChar = chines.toCharArray();
   HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
   defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
   defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
   // 如果传入的第一个不是汉字 则取第二个汉字的首字母 否则只反回第个汉字
   if (nameChar[0] > 128) {
     try {
       String[] pys = PinyinHelper.toHanyuPinyinStringArray(nameChar[0], defaultFormat);
       if (pys != null) {
         char nchar = pys[0].charAt(0);
         pinyinName += nchar;
       }
     } catch (BadHanyuPinyinOutputFormatCombination e) {
       e.printStackTrace();
     }
   } else if (nameChar[1] > 128) {
     try {
       String[] pys = PinyinHelper.toHanyuPinyinStringArray(nameChar[1], defaultFormat);
       if (pys != null) {
         char nchar = pys[0].charAt(0);
         pinyinName += nchar;
       }
     } catch (BadHanyuPinyinOutputFormatCombination e) {
       e.printStackTrace();
     }
   } else {
     pinyinName += "#";
   }
   return pinyinName;
 }
 /**
  * 获取拼音
  *
  * @param chinese 含中文的字符串,若不含中文,原样输出
  * @return 转换后的文本
  * @throws BadHanyuPinyinOutputFormatCombination
  */
 private Collection<String> GetPyString(String chinese)
     throws BadHanyuPinyinOutputFormatCombination {
   List<String[]> pinyinList = new ArrayList<>();
   for (int i = 0; i < chinese.length(); i++) {
     String[] pinyinArray =
         PinyinHelper.toHanyuPinyinStringArray(chinese.charAt(i), this.outputFormat);
     if (pinyinArray != null && pinyinArray.length > 0) {
       pinyinList.add(pinyinArray);
     }
   }
   Set<String> pinyins = null;
   for (String[] array : pinyinList) {
     if (pinyins == null || pinyins.isEmpty()) {
       pinyins = new HashSet<>();
       Collections.addAll(pinyins, array);
     } else {
       Set<String> pres = pinyins;
       pinyins = new HashSet<>();
       for (String pre : pres) {
         for (String charPinyin : array) {
           pinyins.add(pre + charPinyin);
         }
       }
     }
   }
   return pinyins;
 }
Exemple #3
0
 /**
  * 汉字转拼音的方法
  *
  * @param name 汉字
  * @return 拼音
  */
 public static String HanyuToPinyin(String name) {
   if (name == null || name.equals("")) {
     return "";
   }
   String pinyinName = "";
   /** 解决英文名字或首字母为英文的名字问题 如果名字首字母位英文,则直接返回首字母,不再进行转码 add by ChenPengzhen */
   String az = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
   if (az.contains(String.valueOf(name.charAt(0)))) {
     return (name.charAt(0) + "").toUpperCase();
   } else {
     char[] nameChar = name.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 (Exception e) {
           e.printStackTrace();
         }
       }
     }
     if (pinyinName.equals("")) {
       return "#";
     }
     return (pinyinName.charAt(0) + "").toUpperCase();
   }
 }
Exemple #4
0
 /**
  * 汉字转换为汉语拼音首字母,英文字符不�?
  *
  * @author gzs
  * @param chines 汉字
  * @return 拼音
  */
 public static String getPinyinFrt(String chines) {
   String pinyinName = "";
   // 转化为字�?
   char[] nameChar = chines.toCharArray();
   // 汉语拼音格式输出�?
   HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
   // 输出设置,大小�?音标方式�?
   defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
   defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
   for (int i = 0; i < nameChar.length; i++) {
     // 如果是中�?
     if (nameChar[i] > 128) {
       try {
         String[] temp = PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat);
         if (temp != null && temp.length > 0 && temp[0].length() > 0) {
           pinyinName += temp[0].charAt(0);
         }
       } catch (BadHanyuPinyinOutputFormatCombination e) {
         e.printStackTrace();
       }
     } else { // 为英文字�?
       pinyinName += nameChar[i];
     }
   }
   return pinyinName;
 }
  public String toPinYin(String name) {

    HanyuPinyinOutputFormat hanyuPinyin = new HanyuPinyinOutputFormat();
    /** *大写 */
    hanyuPinyin.setCaseType(HanyuPinyinCaseType.UPPERCASE);
    /** *无音调 */
    hanyuPinyin.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
    /** *'¨¹' is "v" */
    hanyuPinyin.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

    String code = "";
    String[] pinyinArray = null;
    for (int i = 0; i < name.length(); i++) {
      char hanzi = name.charAt(i);
      try {
        /** *是否在汉字范围内 */
        if (hanzi >= 0x4e00 && hanzi <= 0x9fa5) {
          pinyinArray = PinyinHelper.toHanyuPinyinStringArray(hanzi, hanyuPinyin);
          code += pinyinArray[0].substring(0, 1);
        } else if (hanzi >= 0x61 && hanzi <= 0x7a) {
          code += String.valueOf((char) (hanzi - 32));
        } else if (hanzi >= 0x41 && hanzi <= 0x5a) {
          code += hanzi;
        } else {
          code += "#";
        }
      } catch (BadHanyuPinyinOutputFormatCombination e) {
        e.printStackTrace();
      }
    }

    return code;
  }
 /**
  * 转换输入汉字的全拼(小写) 多音字只反回第一个拼音,拼音首字母大写其它小写
  *
  * @param chines
  * @return
  */
 public static String converterToPingYingHeadUppercase(String chines) {
   chines = replaceString(chines);
   StringBuffer pinyinName = new StringBuffer();
   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[] pys = PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat);
         String headPy = pys == null ? nameChar[i] + "" : pys[0];
         pinyinName.append(
             conversionHeadUppercase(headPy.charAt(0))
                 + headPy.substring(1, headPy.length())
                 + "-");
       } catch (BadHanyuPinyinOutputFormatCombination e) {
         e.printStackTrace();
       }
     } else {
       pinyinName.append(nameChar[i]);
     }
   }
   return pinyinName.toString();
 }
Exemple #7
0
 /**
  * 将汉字转换为全拼
  *
  * @param src
  * @return String
  */
 public static String getPinYin(String src) {
   char[] t1 = null;
   t1 = src.toCharArray();
   // System.out.println(t1.length);
   String[] t2 = new String[t1.length];
   // System.out.println(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++) {
       // 判断是否为汉字字符
       // System.out.println(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) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   return t4;
 }
Exemple #8
0
  /**
   * 把参数名转换为字段名,把中文转换为拼音,特殊字符转换为下划线
   *
   * @param string
   * @return
   */
  public static String pinyin(String string) {
    Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
    Pattern pattern2 = Pattern.compile("^\\w+$");
    char ch[] = string.toCharArray();
    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < ch.length; i++) {
      String t = String.valueOf(ch[i]);
      Matcher matcher = pattern.matcher(t);
      Matcher matcher2 = pattern2.matcher(t);
      if (matcher.find()) { // 匹配中文
        String pinyin =
            PinyinHelper.toHanyuPinyinStringArray(matcher.group().toCharArray()[0])[0]
                .replaceAll("\\d+", "")
                .replace("u:", "v");
        sb.append(pinyin);
      } else {
        if (matcher2.find()) { // 匹配字母数字下划线

        } else {
          t = "_";
        }
        sb.append(t);
      }
    }

    return sb.toString();
  }
Exemple #9
0
 /**
  * 得到 全拼
  *
  * @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 (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;
 }
Exemple #10
0
  public static String[] toDiffHanyuPinyinStringArray(char ch, HanyuPinyinOutputFormat format)
      throws BadHanyuPinyinOutputFormatCombination {
    String[] pinyinStrArray = null;
    if ('\u9fa5' < ch || '\u4e00' > ch) { // 不是汉字
      return new String[] {("" + ch).toUpperCase()};
    }
    pinyinStrArray = PinyinHelper.toHanyuPinyinStringArray(ch, format);
    int count = 0;
    for (int i = 0; i < pinyinStrArray.length; i++) {
      for (int j = i + 1; j < pinyinStrArray.length; j++) {
        if (pinyinStrArray[i].equals(pinyinStrArray[j])) {
          pinyinStrArray[j] = "null";
        }
      }
      if (pinyinStrArray[i] != "null") {
        ++count;
      }
    }
    String[] reStringArray = new String[count];
    int j = 0;
    for (int i = 0; i < pinyinStrArray.length; i++) {
      if (!pinyinStrArray[i].equals("null")) {
        reStringArray[j++] = pinyinStrArray[i];
      }
    }

    return reStringArray;
  }
Exemple #11
0
 @Test
 public void testPinyinHelper() throws BadHanyuPinyinOutputFormatCombination {
   //		System.out.println(
   //		PinyinHelper.toHanyuPinyinString(test, new HanyuPinyinOutputFormat(), " ")
   //		);
   HanyuPinyinOutputFormat FORMAT_DEFAULT = new HanyuPinyinOutputFormat();
   FORMAT_DEFAULT.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
   LogUtil.show(PinyinHelper.toHanyuPinyinStringArray('中', FORMAT_DEFAULT));
 }
Exemple #12
0
 /**
  * 获取字符串的首字符拼音
  *
  * @param str
  * @return
  */
 public static String getPinYinHeadChar(String str) {
   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;
 }
  private String[] pinyinSynonymArr(char c) throws BadHanyuPinyinOutputFormatCombination {
    String[] pinyinArr = PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat);
    if (pinyinArr == null) {
      return null;
    }

    String[] flattenPinyinArr = flattenPinyinArr(pinyinArr);
    String[] withOrgCharPinyinArr = new String[flattenPinyinArr.length + 1];
    withOrgCharPinyinArr[0] = Character.toString(c);
    System.arraycopy(flattenPinyinArr, 0, withOrgCharPinyinArr, 1, flattenPinyinArr.length);
    return withOrgCharPinyinArr;
  }
Exemple #14
0
 public static String generateKey(String seq) {
   StringBuffer letters = new StringBuffer(seq.length());
   char[] chars = seq.toCharArray();
   for (int i = 0; i < chars.length; i++) {
     String[] pinYin = PinyinHelper.toHanyuPinyinStringArray(chars[i]);
     if (null == pinYin || "none0".equals(pinYin[0])) {
       letters.append(chars[i]);
     } else {
       letters.append(pinYin[0].charAt(0));
     }
   }
   return letters.toString().toLowerCase();
 }
Exemple #15
0
  /**
   * 得到中文首字母
   *
   * @param str
   * @return
   */
  public static String getPinYinHeadChar(String str) {

    String convert = "";
    for (int j = 0; j < str.length(); j++) {
      char word = str.charAt(j);
      String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
      if (pinyinArray != null) {
        convert += pinyinArray[0].charAt(0);
      } else {
        convert += word;
      }
    }
    return convert;
  }
Exemple #16
0
  /**
   * 汉字转换位汉语拼音,英文字符不变
   *
   * @param chines 汉字
   * @return 拼音
   */
  @SuppressWarnings("deprecation")
  public static String getPinyin(String chines) {
    String pinyinName = "";
    HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
    defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
    defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

    try {
      pinyinName = PinyinHelper.toHanyuPinyinString(chines, defaultFormat, "");
    } catch (BadHanyuPinyinOutputFormatCombination e) {

    }
    return pinyinName;
  }
Exemple #17
0
 public String getCharacterPinYin(char c) {
   HanyuPinyinOutputFormat format = null;
   format = new HanyuPinyinOutputFormat();
   format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
   String[] pinyin = null;
   try {
     pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format);
   } catch (BadHanyuPinyinOutputFormatCombination e) {
     e.printStackTrace();
   }
   // 如果c不是汉字,toHanyuPinyinStringArray会返回null
   if (pinyin == null) return null;
   // 只取一个发音,如果是多音字,仅取第一个发音
   return pinyin[0];
 }
Exemple #18
0
 /**
  * 获取指定汉字的拼音数组<br>
  * 注:对声调支持不是很好,有些会显示乱码,例如“很”
  *
  * @param ch 汉字字符
  * @param isShowMark 是否显示声调
  * @return 返回该汉字拼音字符串数组,非汉字返回null
  */
 public static String[] getPinyin(char ch, boolean isShowMark) {
   HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
   // 为true则设置输出格式带声调
   if (isShowMark) {
     format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
     format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
   } else {
     format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
   }
   String[] pinyins = null;
   try {
     pinyins = PinyinHelper.toHanyuPinyinStringArray(ch, format);
   } catch (BadHanyuPinyinOutputFormatCombination e) {
     e.printStackTrace();
   }
   return null == pinyins ? null : pinyins;
 }
 public static String getFirstSpellByString(String string) {
   HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
   format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
   char[] chars = string.toCharArray();
   StringBuffer stringBuffer = new StringBuffer();
   for (char c : chars) {
     try {
       String[] strings = PinyinHelper.toHanyuPinyinStringArray(c, format);
       if (strings != null && strings.length > 0) {
         stringBuffer.append(strings[0].charAt(0));
       }
     } catch (BadHanyuPinyinOutputFormatCombination e) {
       e.printStackTrace();
     }
   }
   return stringBuffer.toString();
 }
 /**
  * 汉字转换位汉语拼音,英文字符不变
  *
  * @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;
 }
Exemple #21
0
 private static String getFullSpell(String chinese) {
   StringBuffer pybf = new StringBuffer();
   char[] arr = chinese.toCharArray();
   HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
   defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
   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();
 }
Exemple #22
0
 /**
  * 获取指定字符串的拼音字符串<br>
  * 注:对声调支持不是很好,有些会显示乱码,例如“很”
  *
  * @param str 字符串
  * @param isShowMark 是否显示声调
  * @return 返回拼音字符串,非汉字则照原样输出
  */
 public static String getPinyins(String str, boolean isShowMark) {
   StringBuffer pinyinStr = new StringBuffer();
   HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
   // 为true则设置输出格式带声调
   if (isShowMark) {
     format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
     format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
   } else {
     format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
   }
   for (int i = 0; i < str.length(); i++) {
     try {
       String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(str.charAt(i), format);
       if (null == pinyins) pinyinStr.append(str.charAt(i));
       else pinyinStr.append(pinyins[0]);
     } catch (BadHanyuPinyinOutputFormatCombination e) {
       e.printStackTrace();
     }
   }
   return pinyinStr.toString();
 }
Exemple #23
0
  /**
   * 将字符串中的中文转化为拼音,其他字符不变
   *
   * @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;
  }
  public static String getPinYin(String text) {
    if (!text.equalsIgnoreCase("")) {
      // check if English string
      boolean valid = text.matches("^[\u0000-\u0080]+$");

      if (!valid) {
        String[] pinyinArray;
        List<String> pinyin = new ArrayList<String>();
        for (int i = 0; i < text.length(); i++) {
          pinyin.add(PinyinHelper.toHanyuPinyinStringArray(text.charAt(i))[0]);
        }

        text = pinyin.get(0);
        for (int i = 1; i < pinyin.size(); i++) {
          text += (" " + pinyin.get(i));
        }
        return text;
      } else return "";
    }
    return null;
  }
  /**
   * 获取拼音集合
   *
   * @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)
            || (int) c <= '9' && (int) c >= '0') {
          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;
  }
Exemple #26
0
 private static String getFirstSpell(String chinese) {
   StringBuffer pybf = new StringBuffer();
   char[] arr = chinese.toCharArray();
   HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
   defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
   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();
 }
 public static String getPinYin4J(String input) {
   if (mFormat == null) {
     mFormat = new HanyuPinyinOutputFormat();
     mFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
     mFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
   }
   StringBuilder builder = new StringBuilder();
   int size = input.length();
   for (int i = 0; i < size; i++) {
     String[] array;
     try {
       char c = input.charAt(i);
       array = PinyinHelper.toHanyuPinyinStringArray(c, mFormat);
       if (array == null) {
         builder.append(c);
       } else {
         builder.append(array[0] != null ? array[0] : c + "");
       }
     } catch (BadHanyuPinyinOutputFormatCombination e) {
     }
   }
   return builder.toString();
 }
 /**
  * 反回输入字符串的所有首字母(小写)并去除非汉字
  *
  * @param chines
  * @return
  */
 public static String converterToAllFirstSpellsLowercase(String chines) {
   chines = replaceString(chines);
   StringBuffer pinyinName = new StringBuffer();
   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[] pys = PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat);
         if (pys != null) {
           char nchar = pys[0].charAt(0);
           pinyinName.append(nchar);
         }
       } catch (BadHanyuPinyinOutputFormatCombination e) {
         e.printStackTrace();
       }
     } else {
       pinyinName.append(nameChar[i]);
     }
   }
   return pinyinName.toString();
 }
Exemple #29
0
  // 通用方法有了。。不过太耗内存。
  public static String Name2Pinyin(String name) throws BadHanyuPinyinOutputFormatCombination {
    // 注意在调用此方法是不能有空格。在前面去掉..这里就不去了。。看着费劲
    if (name == null || name.length() == 0 || name.length() > 4) {
      return null;
    }
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    format.setVCharType(HanyuPinyinVCharType.WITH_V);
    char fourChar = 0;
    if (name.length() == 4) {
      fourChar = name.charAt(3);
      name = name.substring(0, 3);
    }
    char[] eachs = name.toCharArray();
    StringBuffer buffer = null;

    if (eachs.length == 1) {
      buffer = new StringBuffer();
      String[] s = PinYinUtil.toDiffHanyuPinyinStringArray(eachs[0], format);
      for (int i = 0; i < s.length; i++) {
        buffer.append(s[i]);
        if (s.length != 1 && i != s.length - 1) {
          buffer.append(";");
        }
      }
      return buffer.toString().replaceAll(" ", "");
    } else if (eachs.length == 2) {
      buffer = new StringBuffer();
      String[] firstNameArrays = PinYinUtil.toDiffHanyuPinyinStringArray(eachs[0], format);
      String[] secondNameArrays = PinYinUtil.toDiffHanyuPinyinStringArray(eachs[1], format);
      for (int i = 0; i < firstNameArrays.length; ) {
        for (int j = 0; j < secondNameArrays.length; j++) {
          buffer.append(firstNameArrays[i] + " ");
          buffer.append(secondNameArrays[j] + ";");
        }
        i++;
      }
      if (buffer.lastIndexOf(";") == buffer.length() - 1) {
        return buffer.substring(0, buffer.length() - 1).replaceAll(" ", "");
      }
    } else if (eachs.length == 3) {
      buffer = new StringBuffer();
      String[] firstNameArrays = PinYinUtil.toDiffHanyuPinyinStringArray(eachs[0], format);
      String[] secondNameArrays = PinYinUtil.toDiffHanyuPinyinStringArray(eachs[1], format);
      String[] thirdNameArrays = PinYinUtil.toDiffHanyuPinyinStringArray(eachs[2], format);
      for (int i = 0; i < firstNameArrays.length; ) {
        for (int j = 0; j < secondNameArrays.length; ) {
          for (int k = 0; k < thirdNameArrays.length; k++) {
            buffer.append(firstNameArrays[i] + " ");
            buffer.append(secondNameArrays[j] + " ");
            buffer.append(thirdNameArrays[k]);
            if (fourChar != 0) {
              if (("" + fourChar).matches("[a-zA-Z]")) {
                buffer.append(("" + fourChar).toUpperCase());
              } else if ('\u9fa5' < fourChar || '\u4e00' > fourChar) {
                buffer.append(("" + fourChar));
              } else {
                buffer.append(
                    PinyinHelper.toHanyuPinyinStringArray(fourChar)[0]
                        .toUpperCase()
                        .replaceAll("[^a-zA-Z]", ""));
              }
            }
            buffer.append(";");
          }
          j++;
        }
        i++;
      }
      if (buffer.lastIndexOf(";") == buffer.length() - 1) {
        return buffer.substring(0, buffer.length() - 1).replaceAll(" ", "");
      }
    }
    return null;
  }
Exemple #30
0
 /**
  * 使用PinYin4j.jar将汉字转换为拼音
  *
  * @param chineseStr
  * @return
  * @throws Exception
  */
 @SuppressWarnings("deprecation")
 public String chineneToSpell(String chineseStr) throws Exception {
   return PinyinHelper.toHanyuPinyinString(chineseStr, spellFormat, "");
 }