package com.uas.search;

import com.uas.search.util.CollectionUtils;
import com.uas.search.util.StringUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 词典录入辅助类
 *
 * @author sunyj
 * @since 2017/11/20 14:31
 */
public class DictionaryHelper {

    public static void main(String[] args) throws IOException {
        List<String> words = readDict(new File("C:\\Users\\sunyj-pc\\Desktop\\kind.txt"));
//         List<String> words=readDict(new File("C:\\Users\\sunyj-pc\\Desktop\\brand.txt"));
//        List<String> words= Arrays.asList("签约仪式前，秦光荣、李纪恒、仇和等一同会见了参加签约的企业家。",
//                "王国强、高峰、汪洋、张朝阳光着头、韩寒、小四",
//                "张浩和胡健康复员回家了",
//                "王总和小丽结婚了",
//                "编剧邵钧林和稽道青说",
//                "这里有关天培的有关事迹",
//                "龚学平等领导,邓颖超生前");
        print(ik(words));
//        suggest();
    }

    private static void print(List<String> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (String str : list) {
            System.out.println(str);
        }
    }

    private static List<String> readDict(File file) throws IOException {
        List<String> words = new ArrayList<>();
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
            Logger logger = LoggerFactory.getLogger(DictionaryHelper.class);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (StringUtils.isEmpty(line)) {
                    logger.error("line 为空 <" + line + ">");
                    continue;
                }
                // 每行格式为 "宇瞻科技	Apacer"
                String[] strings = line.split("\t");
                for (String str : strings) {
                    if (StringUtils.isEmpty(str) || str.equalsIgnoreCase("NULL")) {
//                        logger.error("str 为空 <" + str + "> <" + line + ">");
                        continue;
                    }
                    if (words.contains(str)) {
                        logger.error("str 重复 <" + str + "> <" + line + ">");
                        continue;
                    }
                    words.add(str);
                }
            }
        }
        return words;
    }

    private static List<String> ik(List<String> words) throws IOException {
        List<String> result = new ArrayList<>();
        for (String sentence : words) {
            Analyzer analyzer = new IKAnalyzer(true);
            TokenStream tokenStream = analyzer.tokenStream("", sentence);
            tokenStream.reset();
            CharTermAttribute cta = tokenStream.addAttribute(CharTermAttribute.class);
            while (tokenStream.incrementToken()) {
                result.add(cta.toString());
            }
            tokenStream.close();
            analyzer.close();
        }
        return result;
    }
}
