package org.diagbot.nlp.feature;

import org.diagbot.nlp.feature.extract.CaseToken;
import org.diagbot.nlp.feature.extract.CaseTokenFactory;
import org.diagbot.nlp.participle.ParticipleUtil;
import org.diagbot.nlp.participle.word.Lexeme;
import org.diagbot.nlp.participle.word.LexemePath;
import org.diagbot.nlp.util.Constants;
import org.diagbot.nlp.util.NlpCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

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


public class FeatureAnalyze {
    private LexemePath<Lexeme> lexemePath = null;
    private String featureNum = "";//特征提取范围

    Logger logger = LoggerFactory.getLogger(FeatureAnalyze.class);

    public List<Map<String, Object>> start(String content, FeatureType featureType) throws Exception {
        CaseToken caseToken = createCaseToken(featureType);
        return this.extract(caseToken, content);
    }

    public Map<String, List<Map<String, Object>>> start(String rdn, String content, FeatureType featureType) throws Exception {
        CaseToken caseToken = createCaseToken(featureType);
        long start = System.currentTimeMillis();
        Map<String, List<Map<String, Object>>> featuresMap = new HashMap<>();
        featuresMap.put(rdn, this.extract(caseToken, content));
        long end = System.currentTimeMillis();
        System.out.println("提取特征词耗时........." + (end - start));
        return featuresMap;
    }

    public Map<String, List<Map<String, Object>>> start(Map<String, String> map, FeatureType featureType) throws Exception {
        CaseToken caseToken = createCaseToken(featureType);
        long start = System.currentTimeMillis();
        Map<String, List<Map<String, Object>>> featuresMap = new HashMap<>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            featuresMap.put(entry.getKey(), this.extract(caseToken, entry.getValue()));
        }
        long end = System.currentTimeMillis();
        System.out.println("提取特征词耗时........." + (end - start));
        return featuresMap;
    }

    public List<Map<String, Object>> extract(CaseToken caseToken, String content) throws Exception {
        if (StringUtils.isEmpty(content)) {
            return null;
        }
//        logger.info("待分词文本：" + content);
        lexemePath = ParticipleUtil.participle(content);

//        String lexeme_text = "";
//        for (int i = 0; i < lexemePath.size(); i++) {
//            lexeme_text = lexeme_text + "|" + lexemePath.get(i).getText() + "^" + lexemePath.get(i).getProperty();
//        }
//        logger.info("分词文本结果：" + lexeme_text);
        lexemePath = replaceLexeme(lexemePath);
        caseToken.getFeatureSize(featureNum);
        return caseToken.analyze(lexemePath);
    }

    public CaseToken createCaseToken(FeatureType featureType) throws Exception {
        CaseToken caseToken = CaseTokenFactory.getInstance(featureType);
        return caseToken;
    }

    private LexemePath replaceLexeme(LexemePath<Lexeme> lexemePath) {
        if (NlpCache.standard_info_synonym_map == null) NlpCache.createSynonymCache();
        for (Lexeme l : lexemePath) {
            String[] props = l.getProperty().split(",");
            l.setConcept(null);
            for (int i = 0; i < props.length; i++) {
                if (NlpCache.standard_info_synonym_map.get(props[i]) == null
                        || NlpCache.standard_info_synonym_map.get(props[i]).get(l.getText()) == null) {
                    if (StringUtils.isEmpty(l.getConcept())) {
                        l.setConcept(l.getText());
                    } else {
                        l.setConcept(l.getConcept() + "," + l.getText());
                    }
                    continue;
                } else {
                    if (i == 0) {
                        l.setConcept(NlpCache.standard_info_synonym_map.get(props[i]).get(l.getText()));
                    } else {
                        l.setConcept(l.getConcept() + "," + NlpCache.standard_info_synonym_map.get(props[i]).get(l.getText()));
                    }
                    //如果是症状或体征，进行标准术语转换
                    if (props[i].equals(Constants.feature_type_symptom)) {
                        l.setText(NlpCache.standard_info_synonym_map.get(props[i]).get(l.getText()));
                    }
                }
            }
        }
        return lexemePath;
    }

    public String setFeatureNum(String featureNum){
        this.featureNum = featureNum;
        return featureNum;
    }
}
