美文网首页
pinyin4j解析

pinyin4j解析

作者: 常强儿 | 来源:发表于2016-06-18 01:09 被阅读1508次

pinyin4j

以前写WEB的时候简单使用过pinyin4j, 感觉叼叼的, 也没有细看, 最近就在面试中被问到相关问题, 回来仔细看一下, 没想到如此简单, 悔当初没有花上一个小时看看这东西.

pinyin4j使用

pinyin4j使用极其简单

String[] pinyinArray =PinyinHelper.toHanyuPinyinStringArray('常');

就可以拿到汉字的拼音, 拼音的格式可以通过下列的参数进行配置

    /**
     * The option indicates that hanyu pinyin is outputted with tone numbers
     */
    public static final HanyuPinyinToneType WITH_TONE_NUMBER = new HanyuPinyinToneType("WITH_TONE_NUMBER");

    /**
     * The option indicates that hanyu pinyin is outputted without tone numbers
     * or tone marks
     */
    public static final HanyuPinyinToneType WITHOUT_TONE = new HanyuPinyinToneType("WITHOUT_TONE");

    /**
     * The option indicates that hanyu pinyin is outputted with tone marks
     */
    public static final HanyuPinyinToneType WITH_TONE_MARK = new HanyuPinyinToneType("WITH_TONE_MARK");

pinyin4j简单分析

pinyin4j使用再简单不过了, 接下来我们再看看他的实现, 就从我们使用的PinyinHelper开始看:

//用来查询拼音的方法
static public String[] toHanyuPinyinStringArray(char ch) {
        return getUnformattedHanyuPinyinStringArray(ch);
}
//直接使用了ChineseToPinyinResourceHolder.theInstance中的方法, 这个instance就是在接下来的代码中进行初始化的
private static String[] getUnformattedHanyuPinyinStringArray(char ch) {
        return ChineseToPinyinResource.getInstance().getHanyuPinyinStringArray(ch);
}
static ChineseToPinyinResource getInstance() {
        return ChineseToPinyinResourceHolder.theInstance;
}
//就是在这里, 写在了静态成员中, 代表着这东西不管你用或者不用, 它就在那里, 占着内存, 在WEB的时候问题还不太明显, 可是到了手机这边, 这几乎上是不能忍的
private static class ChineseToPinyinResourceHolder{
        static final ChineseToPinyinResource theInstance = new ChineseToPinyinResource();
}
private ChineseToPinyinResource() {
        initializeResource();
}
//在这里进行了真下的初始化, 就是用一个Properties把文件中的数据放到内存中, 真是简单到不能再简单了, 同时也说明了可以优化的空间还是有很大的
private void initializeResource() {
        try {
            final String resourceName = "/pinyindb/unicode_to_hanyu_pinyin.txt";
            setUnicodeToHanyuPinyinTable(new Properties());
            getUnicodeToHanyuPinyinTable().load(ResourceHelper.getResourceInputStream(resourceName));
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

具体文件中的内容见下图


unicode_to_hanyu_pinyin.txt的内容

第一列就是将每一个汉字的char转换成16进制的形式. 第二列是此字对应的所有拼音.
在ChineseToPinyinResource中真正进行查找的方法是

String[] getHanyuPinyinStringArray(char ch) {
//真正进行查找的
        String pinyinRecord = getHanyuPinyinRecordFromChar(ch);
        if (null != pinyinRecord) {
            int indexOfLeftBracket = pinyinRecord.indexOf(Field.LEFT_BRACKET);
            int indexOfRightBracket = pinyinRecord.lastIndexOf(Field.RIGHT_BRACKET);
            String stripedString = pinyinRecord.substring(indexOfLeftBracket
                    + Field.LEFT_BRACKET.length(), indexOfRightBracket);
            return stripedString.split(Field.COMMA);
        } else
            return null; // no record found or mal-formatted record
}

private String getHanyuPinyinRecordFromChar(char ch) {
        // convert Chinese character to code point (integer)
        // please refer to http://www.unicode.org/glossary/#code_point
        // Another reference: http://en.wikipedia.org/wiki/Unicode
        int codePointOfChar = ch;
//直接变成16进制进行查找
        String codepointHexStr = Integer.toHexString(codePointOfChar).toUpperCase();
        // fetch from hashtable, 直接拿着16进制的字去找.简直不能再简单
        String foundRecord = getUnicodeToHanyuPinyinTable().getProperty(codepointHexStr);
        return isValidRecord(foundRecord) ? foundRecord : null;
}

应该怎么用

pinyin4j是有一些优化空间的, 尤其是使用在手机上的时候

  • 首先应该在合适的地进行初始化数据, 不能一进来就进行
  • 看上面的图中, 有大量的重重拼音的存储, 这个是很耗费空间的, 一个"chang"就是10个字节, 而每个"chang"又对应数十个字, 所以会将"chang"存数十份, 极浪费内存, 这在数据库中就是不符合范式, 就多加一个表进行二次查找
  • 别外, 我们只对表进行查找, 应该找到更合适的存储方式, 而不是去使用Properties, 因为..
class Properties extends Hashtable<Object,Object> {
      ....
}

相关文章

网友评论

      本文标题:pinyin4j解析

      本文链接:https://www.haomeiwen.com/subject/wffvdttx.html