Java 中文转拼音/汉字转拼音, 中文转五笔/汉字转五笔, 下载字典!

Java 中文转拼音/汉字转拼音, 中文转五笔/汉字转五笔, 下载字典!

源码 CharacterElement.java


public class CharacterElement {
    private int unicode;

    private String pinyin;

    private String wubi;

    public CharacterElement() {}

    public CharacterElement(String str) {
        if (str != null) {
            String[] content = str.split(",");
            if (content.length == 3) {
                try {
                    this.unicode = Integer.parseInt(content[0]);
                } catch (Exception e) {
                    System.out.println("CharacterElement: " + e.getMessage());
                }
                this.pinyin = content[1];
                this.wubi = content[2].split("[\\|]")[0];
            }
        }
    }

    public int getUnicode() {
        return unicode;
    }

    public void setUnicode(int unicode) {
        this.unicode = unicode;
    }

    public String getPinyin() {
        return pinyin;
    }

    public void setPinyin(String pinyin) {
        this.pinyin = pinyin;
    }

    public String getWubi() {
        return wubi;
    }

    public void setWubi(String wubi) {
        this.wubi = wubi;
    }

}

源码 Dict.java


public class Dict {
    private static Map<Integer, CharacterElement> map = new HashMap<>(20993);

    /**
     * 载入数据
     * */
    static {
        try {
            /**
            * DB存放目录/home/jenkins/project/dict.db
			*/
            InputStreamReader reader = new InputStreamReader(
                new FileInputStream(
                    new File(File.separator + "home" + File.separator + "jenkins" + File.separator + "project" + File.separator + "dict.db")
                )
            );
            BufferedReader br = new BufferedReader(reader);
            String l;
            while ((l = br.readLine()) != null) {
                CharacterElement e = new CharacterElement(l);
                map.put(e.getUnicode(), new CharacterElement(l));
            }
            br.close();
        } catch (Exception e) {
            System.out.print("Dict: " + e.getMessage());
        }
    }

    public static String getPinYin(final String str) {
        final StringBuffer sb = new StringBuffer();
        for (Character ch : str.toCharArray()) {
            sb.append(getPinYin(ch));
        }

        return sb.toString();
    }

    public static String getPinYin(final Character ch) {
        if (ch != null) {
            if (map.containsKey(ch.hashCode())) {
                return map.get(ch.hashCode()).getPinyin();
            } else {
                return ch.toString();
            }
        }
        return "";
    }

    public static String getWubi(final String str) {
        final StringBuffer sb = new StringBuffer();
        for (Character ch : str.toCharArray()) {
            sb.append(getWubi(ch));
        }

        return sb.toString();
    }

    public static String getWubi(final Character ch) {
        if (ch != null) {
            if (map.containsKey(ch.hashCode())) {
                return map.get(ch.hashCode()).getWubi();
            } else {
                return ch.toString();
            }
        }
        return "";
    }

    public static Map<String, String> getMap(final String str) {
        final Map<String, String> dic = new HashMap<>(2);
        final StringBuffer p = new StringBuffer();
        final StringBuffer w = new StringBuffer();
        for (Character ch : str.toCharArray()) {
            p.append(getPinYin(ch));
            w.append(getWubi(ch));
        }
        dic.put("pinyin", p.toString());
        dic.put("wubi", w.toString());
        return dic;
    }

}

输出拼音


        System.out.println("拼音: " + getPinYin("Hello world! 拼音"));
        //拼音: Hello world! pinyin
        

输出五笔


        System.out.println("五笔: " + getWubi("Hello world! 五笔"));
        //五笔: Hello world! gghgttfn
        

输出全部


        final Map<String, String> all = getMap("Hello world! 全部");
        System.out.println("全部: " + all.toString());
        //全部: {wubi=Hello world! wgfukbh, pinyin=Hello world! quanbu}
        

字典下载地址


https://download.csdn.net/download/qcl108/11777317

如果您觉得有帮助,欢迎点赞哦 ~ 谢谢!!

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页