阅读背景:

solr入门之pinyin4j源码改写动态添加扩展词及整合进war项目中

来源:互联网 

1.初始化时加载用户定义的字典
package net.sourceforge.pinyin4j;

import net.sourceforge.pinyin4j.multipinyin.Trie;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.gome.mx.plus.pinyin.ext.PYWriterUtils;

/**
 * Manage all external resources required in PinyinHelper class.
 *
 * @author Li Min ([email protected])
 */
public class ChineseToPinyinResource {
    /**
     * A hash table contains <Unicode, HanyuPinyin> pairs
     */
    private Trie unicodeToHanyuPinyinTable = null;

    /**
     * @param unicodeToHanyuPinyinTable The unicodeToHanyuPinyinTable to set.
     */
    private void setUnicodeToHanyuPinyinTable(Trie unicodeToHanyuPinyinTable) {
        this.unicodeToHanyuPinyinTable = unicodeToHanyuPinyinTable;
    }

    /**
     * @return Returns the unicodeToHanyuPinyinTable.
     */
   public  Trie getUnicodeToHanyuPinyinTable() {
        return unicodeToHanyuPinyinTable;
    }

    /**
     * Private constructor as part of the singleton pattern.
     */
    private ChineseToPinyinResource() {
        initializeResource();
    }

    /**
     * Initialize a hash-table contains <Unicode, HanyuPinyin> pairs
     */
    private void initializeResource() {
        try {
            final String resourceName = "/pinyindb/unicode_to_hanyu_pinyin.txt";
            final String resourceMultiName = "/pinyindb/multi_pinyin.txt";
            final String userResourceName  = PYWriterUtils.getPath();

            setUnicodeToHanyuPinyinTable(new Trie());
            getUnicodeToHanyuPinyinTable().load(ResourceHelper.getResourceInputStream(resourceName));

            getUnicodeToHanyuPinyinTable().loadMultiPinyin(ResourceHelper.getResourceInputStream(resourceMultiName));

            getUnicodeToHanyuPinyinTable().loadMultiPinyinExtend();
            //加载用户自定义词库
            if (userResourceName != null) {
                File userMultiPinyinFile = new File(userResourceName);
                FileInputStream is = new FileInputStream(userMultiPinyinFile);
                getUnicodeToHanyuPinyinTable().load(is);
            }

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    Trie getHanyuPinyinTrie(char ch) {

        String codepointHexStr = Integer.toHexString((int) ch).toUpperCase();

        // fetch from hashtable
        return getUnicodeToHanyuPinyinTable().get(codepointHexStr);
    }

    /**
     * Get the unformatted Hanyu Pinyin representations of the given Chinese
     * character in array format.
     *
     * @param ch given Chinese character in Unicode
     * @return The Hanyu Pinyin strings of the given Chinese character in array
     * format; return null if there is no corresponding Pinyin string.
     */
    String[] getHanyuPinyinStringArray(char ch) {
        String pinyinRecord = getHanyuPinyinRecordFromChar(ch);
        return parsePinyinString(pinyinRecord);
    }

    String[] parsePinyinString(String pinyinRecord) {

        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
    }

    /**
     * @param record given record string of Hanyu Pinyin
     * @return return true if record is not null and record is not "none0" and
     * record is not mal-formatted, else return false
     */
    private boolean isValidRecord(String record) {
        final String noneStr = "(none0)";

        return (null != record) && !record.equals(noneStr) && record.startsWith(Field.LEFT_BRACKET)
                && record.endsWith(Field.RIGHT_BRACKET);
    }

    /**
     * @param ch given Chinese character in Unicode
     * @return corresponding Hanyu Pinyin Record in Properties file; null if no
     * record found
     */
    private String getHanyuPinyinRecordFromChar(char ch) {
        // convert Chinese character to code point (integer)
        // please refer to https://www.unicode.org/glossary/#code_point
        // Another reference: https://en.wikipedia.org/wiki/Unicode
        int codePointOfChar = ch;

        String codepointHexStr = Integer.toHexString(codePointOfChar).toUpperCase();

        // fetch from hashtable
        Trie trie = getUnicodeToHanyuPinyinTable().get(codepointHexStr);
        String foundRecord = null;
        if (trie != null)
            foundRecord = trie.getPinyin();

        return isValidRecord(foundRecord) ? foundRecord : null;
    }

    /**
     * Singleton factory method.
     *
     * @return the one and only MySingleton.
     */
    public static ChineseToPinyinResource getInstance() {

        return ChineseToPinyinResourceHolder.theInstance;
    }


    /**
     * Singleton implementation helper.
     */
    private static class ChineseToPinyinResourceHolder {
        static final ChineseToPinyinResource theInstance = new ChineseToPinyinResource();
    }

    /**
     * A class encloses common string constants used in Properties files
     *
     * @author Li Min ([email protected])
     */
    class Field {
        static final String LEFT_BRACKET = "(";

        static final String RIGHT_BRACKET = ")";

        static final String COMMA = ",";
    }
}
1.初始化时加载用户定义的字典
package net.sourceforge.pinyin4j;




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: