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

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

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值