package org.diagbot.nlp.test;

import org.diagbot.nlp.util.CharacterUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 提词算法
 * Created by fyeman on 2018/1/23.
 */
public class CHNCharacterTest {
    private List<Character[]> contents = new ArrayList<Character[]>();
    private Map<Integer, Character> chars = new HashMap<Integer, Character>(100, 0.75f);
    private Integer charsKey;

    public static void main(String[] args) {
        CHNCharacterTest test = new CHNCharacterTest();
//        test.createAnalyzedFile();
        test.startAnalyzedFile();
    }

    public void createAnalyzedFile() {
//        MysqlJdbc mysql = new MysqlJdbc();
//        List<Info> infos = mysql.searchDocInfos();
//        List<String> contents = new ArrayList();
//        for (Info info : infos) {
//            if (StringUtils.isNotEmpty(info.getDiag())) {
//                contents.add(info.getDiag());
//            }
//            if (StringUtils.isNotEmpty(info.getPresent())) {
//                contents.add(info.getPresent());
//            }
//            if (StringUtils.isNotEmpty(info.getVital())) {
//                contents.add(info.getVital());
//            }
//            if (StringUtils.isNotEmpty(info.getLis())) {
//                contents.add(info.getLis());
//            }
//            if (StringUtils.isNotEmpty(info.getPacs())) {
//                contents.add(info.getPacs());
//            }
//        }
//
//        Configuration cfg = new DefaultConfig();
//        cfg.writeFileContents(contents, "E:\\git\\diagbot\\nlp\\src\\main\\resources\\present.txt");
    }

    public void startAnalyzedFile() {
//        long start = System.currentTimeMillis();
//        CHNCharacterTest test = new CHNCharacterTest();
//
//        NlpJdbc mysql = new NlpJdbc();
////        List<String> vitals = mysql.searchDocVitals("select vital from doc_info where vital is not null \n" +
////                "and vital != 'null' and vital != '合作性 : 合作   表情 : 自如' and vital != ''");
//        Configuration cfg = new DefaultConfig();
//        List<String> contents = cfg.readFileContents("lexemecorpus6.txt");
//        int i = 0;
//        int length = contents.size();
//        for (i = 0; i < length; i++) {
//            test.cleanChars(contents.get(i).toCharArray());
//        }
//
//        int size = test.contents.size();
//        CHNCharacterUtil CHNCharUtil = new CHNCharacterUtil();
//        for (i = 0; i < size; i++) {
//            if (i % 10000 == 0) {
//                System.out.println("开始处理第...." + i + "行");
//            }
//            CHNCharUtil.fillCHNCharacter(test.contents.get(i));
//        }
//
//        Map<Character, CHNCharacter> words = CHNCharUtil.getWords();
//        CHNCharacterFilter CHNCharFilter = new CHNCharacterFilter();
//        CHNCharFilter.filter(words);
//
//        CHNCharacterContext context = new CHNCharacterContext(words);
//
////        List<String> temp = new ArrayList<>();
////        String t = "";
//        for (i = 0; i < size; i++) {
//            context.start(test.contents.get(i));
////
////            t = String.valueOf(ArrayUtils.toPrimitive(test.contents.get(i)));
////            if (t.indexOf("姚人民医院") > -1) {
////                temp.add(t);
////            }
//        }
////        cfg.writeFileContents(temp, "E:\\git\\diagbot\\nlp\\src\\main\\resources\\temp.txt");
//
//        List<Map.Entry<String, Float>> ontologyLibraryList = new ArrayList<Map.Entry<String, Float>>(context.getOntologyLibraryMap().entrySet());
//        Collections.sort(ontologyLibraryList, new Comparator<Map.Entry<String, Float>>() {
//            public int compare(Map.Entry<String, Float> o1, Map.Entry<String, Float> o2) {
//                return (o1.getKey()).toString().compareTo(o2.getKey());
//            }
//        });
//
////        for (i = 0; i < ontologyLibraryList.size();i++ ) {
////            Map.Entry<String, Float> m = ontologyLibraryList.get(i);
////
////            System.out.println(m.getKey());
////        }
//
//
//        mysql.insertLexiconInfo(ontologyLibraryList);
//
//        long end = System.currentTimeMillis();
//
//        System.out.println("总耗时:........." + (end - start));
    }

    private void cleanChars(char[] lineChars) {
        List<Character> chineseCharsList = new ArrayList<>();
        peelChineseChars(lineChars, chineseCharsList);
    }

    /**
     * 截取中文语句
     *
     * @param lineChars
     * @param chineseCharsList
     */
    private void peelChineseChars(char[] lineChars, List<Character> chineseCharsList) {
        int i = 0;
        for (i = 0; i < lineChars.length; i++) {
            if (CharacterUtil.identifyCharType(lineChars[i]) == CharacterUtil.CHAR_CHINESE || CharacterUtil.identifyCharType(lineChars[i]) == CharacterUtil.CHAR_ENGLISH) {
                charsKey = Integer.valueOf(lineChars[i]);
                if (chars.get(charsKey) == null) {
                    chars.put(charsKey, new Character(lineChars[i]));
                }
                chineseCharsList.add(chars.get(charsKey));
            } else {
                if (chineseCharsList.size() > 0) {
                    this.addChineseChars(chineseCharsList);
                    chineseCharsList = new ArrayList<>();
                }
            }
            if (i == lineChars.length - 1) {
                if (chineseCharsList.size() > 0) {
                    this.addChineseChars(chineseCharsList);
                }
            }
        }
    }

    private void addChineseChars(List<Character> chineseCharsList) {
        Character[] chineseChars = new Character[chineseCharsList.size()];
        chineseCharsList.toArray(chineseChars);
        contents.add(chineseChars);
    }
}
