All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.houbb.chinese.idiom.support.data.ChineseIdiomData Maven / Gradle / Ivy

There is a newer version: 0.0.2
Show newest version
package com.github.houbb.chinese.idiom.support.data;

import com.alibaba.fastjson.JSON;
import com.github.houbb.chinese.common.api.IChineseInfo;
import com.github.houbb.chinese.common.support.bean.ChineseInfo;
import com.github.houbb.chinese.common.support.data.IChineseData;
import com.github.houbb.chinese.idiom.constant.ChineseIdiomConst;
import com.github.houbb.chinese.idiom.model.ChineseIdiomBean;
import com.github.houbb.heaven.annotation.ThreadSafe;
import com.github.houbb.heaven.constant.PunctuationConst;
import com.github.houbb.heaven.support.tuple.impl.Pair;
import com.github.houbb.heaven.util.guava.Guavas;
import com.github.houbb.heaven.util.io.StreamUtil;
import com.github.houbb.heaven.util.lang.ObjectUtil;
import com.github.houbb.heaven.util.lang.StringUtil;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author binbin.hou
 * @since 0.0.1
 */
@ThreadSafe
public class ChineseIdiomData implements IChineseData {

    /**
     * 单例 map
     * @since 0.0.1
     */
    private static final Map> MAP;

    static {
        final long startTimeMills = System.currentTimeMillis();
        List allLines = StreamUtil.readAllLines(ChineseIdiomConst.IDIOM_INDEX_DICT_PATH);
        MAP = Guavas.newHashMap(allLines.size());
        for(String line : allLines) {
            if(StringUtil.isEmptyTrim(line)) {
                continue;
            }

            String[] entries = line.split(StringUtil.BLANK);
            String[] indexes = entries[1].split(PunctuationConst.COMMA);

            int startIndex = Integer.parseInt(indexes[0]);
            int endIndex = Integer.parseInt(indexes[1]);

            MAP.put(entries[0], Pair.of(startIndex, endIndex));
        }

        final long endTimeMills = System.currentTimeMillis();
        System.out.println("[Chinese] idiom data index loaded, cost " + (endTimeMills-startTimeMills) + " ms!");
    }

    @Override
    public Set words() {
        return MAP.keySet();
    }

    @Override
    public IChineseInfo getInfo(String s) {
        Pair pair = MAP.get(s);
        if(ObjectUtil.isNull(pair)) {
            return null;
        }

        String json = StreamUtil.getFileContent(ChineseIdiomConst.IDIOM_DICT_PATH,
                pair.getValueOne(), pair.getValueTwo());

        ChineseIdiomBean bean = JSON.parseObject(json, ChineseIdiomBean.class);
        return ChineseInfo.newInstance().text(bean.getWord()).explanation(bean.getExplanation());
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy