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

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

  • 源码 CharacterElement.java
  • 源码 Dict.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 map = new HashMap<>();

    /**
     * 载入数据
     * */
    static {
        try {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            Dict.class.getResourceAsStream("dict.db")
                    )
            );
            String l;
            while ((l = br.readLine()) != null) {
                CharacterElement ele = new CharacterElement(l);
                map.put(ele.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 getMap(final String str) {
        final Map 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 all = getMap("Hello world! 全部");
        System.out.println("全部: " + all.toString());
        //全部: {wubi=Hello world! wgfukbh, pinyin=Hello world! quanbu}
        

字典下载地址


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

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

你可能感兴趣的:(java中文转拼音,java汉字转拼音,java汉字转五笔,java中文转五笔,下载转码字典,Spring,Boot,Java)