package org.diagbot.common.push.work;

import org.algorithm.core.cnn.entity.Lemma;
import org.algorithm.core.cnn.entity.Triad;
import org.diagbot.common.push.bean.SearchData;
import org.diagbot.common.push.cache.ApplicationCacheUtil;
import org.diagbot.common.push.util.PushConstants;
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 java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: HUJING
 * @Date: 2019/9/9 17:30
 */
public class RelationExtractionUtil {
    public List<Triad> createTriad(SearchData searchData) throws IOException {
        List<Triad> triads = new ArrayList<>();
        String[] pacsSplits = searchData.getPacs().trim().split("。|\n");
        List<Lemma> lemmaList = new ArrayList<>();
        Lemma lemma = null;
        for (String pacsSplit : pacsSplits) {
            LexemePath<Lexeme> pacsLexemes = ParticipleUtil.participlePacs(pacsSplit);
            for (int i = 0; i < pacsLexemes.size(); i++) {
                //跳过非医学词
                if (Constants.word_property_other.equals(pacsLexemes.get(i).getProperty())) {
                    continue;
                }
                lemma = new Lemma();
                lemma.setText(pacsLexemes.get(i).getText());
                lemma.setPosition(String.valueOf(pacsLexemes.get(i).getOffset()) + "," + (Integer.valueOf(pacsLexemes.get(i).getOffset() + pacsLexemes.get(i).getLength()) - 1));
                lemma.setProperty(PushConstants.featureTypeMap.get(pacsLexemes.get(i).getProperty()));
                lemmaList.add(lemma);
            }
        }
        for (int i = 0; i < lemmaList.size() - 1; i++) {
            for (int j = i + 1; j < lemmaList.size(); j++) {
                Triad triad = new Triad();
                triad.setL_1(lemmaList.get(i));
                triad.setL_2(lemmaList.get(j));
                triads.add(triad);
            }
        }
        return triads;
    }

    public void addToSearchDataInputs(List<List<String>> relationExtractionContents, SearchData searchData) throws Exception {
        StringBuffer sb = null;
        for (List<String> contents : relationExtractionContents) {
            sb = new StringBuffer();
            for (String content : contents) {
                sb.append(content);
            }
            if (isExist(sb.toString())) {
                Map<String, String> map = new HashMap<>();
                map.put("featureType", Constants.feature_type_pacs);
                map.put("featureName", sb.toString());
                map.put("property", Constants.word_property_PACS_Result);
                map.put("concept", sb.toString());
                //全是有
                map.put("negative", Constants.default_negative);
                if (searchData.getInputs().get(map.get("featureName")) == null) {
                    searchData.getInputs().put(map.get("featureName"), map);
                }
                if (searchData.getGraphInputs().get(map.get("featureName")) == null) {
                    searchData.getGraphInputs().put(map.get("featureName"), map);
                }
            }
        }
    }

    /**
     * 关系抽取输出的content是否在已有诊断依据中存在
     * @param content
     * @return
     */
    public boolean isExist(String content){
        Map<String, Map<String, String>> kl_diagnose_detail_filter_map = ApplicationCacheUtil.getKl_diagnose_detail_filter_map();
        if (kl_diagnose_detail_filter_map.get("4") != null){
            if (kl_diagnose_detail_filter_map.get("4").containsKey(content)){
                return true;
            }
        }
        return false;
    }

}
