Java拼音转换一例

阅读更多

      工作中有时碰到将中文名转换成首字母拼音书写。以前工作电脑故障,资料尽失,这里补充回一点,

参考网站:http://pinyin4j.sourceforge.net/,当前版本: pinyin4j 2.5.0

关键是知道有这么个开源jar包可以使用,没有什么特别的,使用起来很简单,代码中使用一点小算法。

 

package com.pinying.demo;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import net.sourceforge.pinyin4j.PinyinHelper;

/**
 * Chinese character convert to Pinyin(include tongying character combine), return String Array
 * Description: Previously documented, computer failure, all data is lost, now slowly accumulated
 * @author zhaoxiaobo Dennis Email: [email protected]
 * @createdTime:2012-11-27
 */
public class PinyinDemo {

    private static Set set = new HashSet(0);
    private static int NUMNER = 0;
    private static String CHINESE_CHAR = "给予你长久的力量";//test data
//    private static String CHINESE_CHAR = "黄长恩";//test data
//    private static String CHINESE_CHAR = "赵晓波";//test data
//    private static String CHINESE_CHAR = "胡胜利";//test data
//    private static String CHINESE_CHAR = "张三丰";//test data

    static {
        NUMNER = CHINESE_CHAR.length();
    }

    public static void main(String[] args) {
//        List> lists = new ArrayList>(0);
//        List list = new ArrayList(0);
//        list.add("a");
//        list.add("b");
//        lists.add(list);
//        List list1 = new ArrayList(0);
//        list1.add("c");
//        list1.add("d");
//        lists.add(list1);
//        List list2 = new ArrayList(0);
//        list2.add("e");
//        list2.add("f");
//        lists.add(list2);
//        List list3 = new ArrayList(0);
//        list3.add("g");
//        list3.add("h");
//        lists.add(list3);
        generateData(getFirstCharFromString(CHINESE_CHAR), new ArrayList(0));
        Iterator it = set.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }

    /**
     *  获取一段中文字符拼音首字母的(包括多音字的组合)数组
     * getFirstCharFromString
     * @param str
     * @return
     * @return the List>
     */
    public static List> getFirstCharFromString(String str) {
        List> rets = new ArrayList>(0);
        if (null != str && !"".equals(str.trim())) {
            int len = str.trim().length();
            char chr = ' ';
            for (int i = 0; i < len; i++) {
                Set set = new HashSet(0);
                chr = str.charAt(i);
                if (0x4E00 <= chr && chr <= 0x9FA5) {
                    String[] arr = PinyinHelper.toHanyuPinyinStringArray(chr);
                    for (String string : arr) {
                        set.add(string.toUpperCase().charAt(0) + "");
                    }
                } else {
                    set.add(chr + "");
                }
                List list = new ArrayList(0);
                Iterator it = set.iterator();
                while (it.hasNext()) {
                    list.add(it.next());
                }
                rets.add(list);
            }
        } else {
            System.out.println("null value ....");
        }
        return rets;
    }

    /**
     *  生成最终的数组组合数据根据排列组合算法
     * generateData
     * @param source
     * @param target
     * @return the void
     */
    public static void generateData(List> source, List target) {
        StringBuilder sb = new StringBuilder();
        if (target.size() == NUMNER) {
            for (String string : target) {
                sb.append(string);
            }
            set.add(sb.toString());
            return;
        }
        //{{'a','b'},{'c','d'},{'e','f'}}
        for (List list : source) {
            List> newSource = new ArrayList>(source);
            List newTarget = new ArrayList(target);
            newSource.remove(list);
            for (String str : list) {
                newTarget.add(str);
                generateData(newSource, newTarget);
                newTarget.remove(str);
            }
            break;
        }
    }
}

 

你可能感兴趣的:(Java拼音转换一例)