package org.diagbot.nlp.relation.extract;

import org.algorithm.core.cnn.entity.Lemma;
import org.diagbot.nlp.relation.module.Symptom;
import org.diagbot.nlp.relation.util.OutputInfo;
import org.diagbot.nlp.relation.module.cell.*;
import org.diagbot.nlp.util.Constants;
import org.diagbot.nlp.util.NlpUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName org.diagbot.nlp.relation.extract.PresentExtract
 * @Description TODO
 * @Author fyeman
 * @Date 2019/1/18/018 14:46
 * @Version 1.0
 **/
public class PresentExtract extends BaseExtract {
    public OutputInfo extract(OutputInfo outputInfo, List<Lemma> lemmaTree, List<Lemma> lemmaParticiple) {
        List<String> repeatSymptomsList = new ArrayList<>();
        for (int index = 0; index < lemmaParticiple.size(); index++) {
            Lemma participle_lemma = lemmaParticiple.get(index);
            String property = participle_lemma.getProperty();
            if (NlpUtil.isFeature(property, Constants.symptom_type)) {          //特征词 症状
                if (repeatSymptomsList.contains(participle_lemma.getText())) {
                    continue;
                }
                Symptom symptom = lookSymptomRelations(participle_lemma, lemmaTree);
                outputInfo.getSymptoms().add(symptom);

                repeatSymptomsList.add(participle_lemma.getText());
            }
        }
        lemmaTree.removeAll(containsLemmaTree);
        return outputInfo;
    }

    private Symptom lookSymptomRelations(Lemma participle_lemma, List<Lemma> lemmaTree) {
        Symptom symptom = new Symptom();
        symptom.setSymptomName(participle_lemma.getText());
        for (Lemma cnn_l : lemmaTree) {
            if (cnn_l.getText().equals(participle_lemma.getText())
                    && cnn_l.getPosition().equals(participle_lemma.getPosition())) {
                for (Lemma relation_l : cnn_l.getRelationLemmas()) {
                    addFeatureToSymptom(relation_l, symptom);
                }
                containsLemmaTree.add(cnn_l);
            }
        }
        return symptom;
    }

    private void addFeatureToSymptom(Lemma lemma, Symptom symptom) {
        //时间信息
        if (NlpUtil.isFeature(lemma.getProperty(), Constants.unit_time_type) || NlpUtil.isFeature(lemma.getProperty(), Constants.event_time_desc_type)) {
            List<PD> pds = symptom.getPds();
            if (pds == null) {
                pds = new ArrayList<>();

            }
            PD pd = new PD();
            pd.setValue(lemma.getText());
            pds.add(pd);
            symptom.setPds(pds);
        }
        //部位信息
        if (NlpUtil.isFeature(lemma.getProperty(), Constants.body_part_type)) {
            BodyPart bodyPart = new BodyPart();
            bodyPart.setPartBodyName(lemma.getText());
            if (lemma.getRelationLemmas() != null && lemma.getRelationLemmas().size() > 0) {
                Lemma l = lemma.getRelationLemmas().get(0);
                if (NlpUtil.isFeature(l.getProperty(), Constants.position_type)) {
                    Position position = new Position();
                    position.setPositionName(l.getText());
                    bodyPart.setPosition(position);
                }
            }
            symptom.setBodyPart(bodyPart);
        }
        //程度
        if (NlpUtil.isFeature(lemma.getProperty(), Constants.degree_type)) {
            Degree degree = new Degree();
            degree.setDegreeName(lemma.getText());
            for (Lemma l : lemma.getRelationLemmas()) {
                if (NlpUtil.isFeature(l.getProperty(), Constants.unit_time_type)) {
                    PD pd = new PD();
                    pd.setValue(l.getText());
                    degree.setPd(pd);
                }
            }
            symptom.setDegree(degree);
        }
        //性质
        if (NlpUtil.isFeature(lemma.getProperty(), Constants.property_type)) {
            Property property = new Property();
            property.setPropertyName(lemma.getText());
            symptom.setProperty(property);
        }
        //诱因
        if (NlpUtil.isFeature(lemma.getProperty(), Constants.cause_type)) {
            Cause cause = new Cause();
            cause.setCauseName(lemma.getText());
            symptom.setCause(cause);
        }

        //阴性
        if (NlpUtil.isFeature(lemma.getProperty(), Constants.negative_type)) {
            Negative negative = new Negative();
            negative.setNegaName(lemma.getText());
            symptom.setNegative(negative);
        }
    }
}
