package org.diagbot.nlp.relation.extract;

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

import java.util.List;

/**
 * @ClassName org.diagbot.nlp.relation.extract.VitalExtract
 * @Description TODO
 * @Author fyeman
 * @Date 2019/1/24/024 10:28
 * @Version 1.0
 **/
public class VitalExtract extends BaseExtract {
    public OutputInfo extract(OutputInfo outputInfo, List<Lemma> lemmaTree, List<Lemma> lemmaParticiple) {
        for (int index = 0; index < lemmaParticiple.size(); index++) {
            Lemma participle_lemma = lemmaParticiple.get(index);
            String property = participle_lemma.getProperty();
            if (NlpUtil.isFeature(property, Constants.vital_type)) {          //特征词 体征指标、体征结果、部位
                Vital vital = lookVitalRelations(participle_lemma, lemmaTree);
                if (vital != null) {
                    outputInfo.getVitals().add(vital);
                }
            }
        }
        lemmaTree.removeAll(containsLemmaTree);
        return outputInfo;
    }

    private Vital lookVitalRelations(Lemma participle_lemma, List<Lemma> lemmaTree) {
        //在三元组中查找是否有该特征信息
        Vital vital = null;
        for (Lemma cnn_l : lemmaTree) {
            if (cnn_l.getText().equals(participle_lemma.getText())
                    && cnn_l.getPosition().equals(participle_lemma.getPosition())) {    //三元组中存在该特征
                vital = new Vital();
                if (NlpUtil.isFeature(participle_lemma.getProperty(), Constants.vital_index_type)) {
                    vital.setVitalName(cnn_l.getText());
                }
                if (NlpUtil.isFeature(participle_lemma.getProperty(), Constants.body_part_type)) {
                    BodyPart bodyPart = new BodyPart();
                    bodyPart.setPartBodyName(cnn_l.getText());
                    vital.setBodyPart(bodyPart);
                }
                for (Lemma relation_l : cnn_l.getRelationLemmas()) {
                    addFeatureToVital(relation_l, vital);
                }
                containsLemmaTree.add(cnn_l);
            }
        }
        return vital;
    }

    private void addFeatureToVital(Lemma lemma, Vital vital) {
        if (NlpUtil.isFeature(lemma.getProperty(), Constants.negative_type)) {      //阴性
            Negative negative = new Negative();
            negative.setNegaName(lemma.getText());
            vital.setNegative(negative);
        } else if (NlpUtil.isFeature(lemma.getProperty(), Constants.degree_type)) {   //程度
            Degree degree = new Degree();
            degree.setDegreeName(lemma.getText());
            vital.setDegree(degree);
        } else if (NlpUtil.isFeature(lemma.getProperty(), Constants.body_part_type)) {   //部位
            BodyPart bodyPart = vital.getBodyPart();
            if (bodyPart == null) {
                bodyPart = new BodyPart();
            }
            bodyPart.setPartBodyName(lemma.getText());
            //部位需要查看下有没有方位
            for (Lemma position_l : lemma.getRelationLemmas()) {
                if (NlpUtil.isFeature(lemma.getProperty(), Constants.position_type)) {
                    Position position = new Position();
                    position.setPositionName(position_l.getText());
                    bodyPart.setPosition(position);
                }
            }
            vital.setBodyPart(bodyPart);
        } else if (NlpUtil.isFeature(lemma.getProperty(), Constants.vital_value_type)) {
            vital.setValue(lemma.getText());
        } else if (NlpUtil.isFeature(lemma.getProperty(), Constants.unit_time_type)) {
            PD pd = vital.getPd();
            if (vital.getPd() == null) {
                pd = new PD();
                pd.setValue(lemma.getText());
            } else {
                pd.setValue(pd.getValue() + "、" + lemma.getText());
            }
            vital.setPd(pd);
        }
    }
}
