package org.diagbot.nlp.relation.extract;

import org.algorithm.core.cnn.entity.Lemma;
import org.diagbot.nlp.relation.extract.cell.*;
import org.diagbot.nlp.relation.extract.module.Vital;
import org.diagbot.nlp.relation.extract.output.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();
                vital.setVitalName(cnn_l.getText());
                for (Lemma l : cnn_l.getRelationLemmas()) {
                    if (NlpUtil.isFeature(l.getProperty(), Constants.negative_type)) {      //阴性
                        Negative negative = new Negative();
                        negative.setNegaName(l.getText());
                        vital.setNegative(negative);
                    } else if (NlpUtil.isFeature(l.getProperty(), Constants.degree_type)) {   //程度
                        Degree degree = new Degree();
                        degree.setDegreeName(l.getText());
                        vital.setDegree(degree);
                    } else if (NlpUtil.isFeature(l.getProperty(), Constants.body_part_type)) {   //方位
                        BodyPart bodyPart = new BodyPart();
                        bodyPart.setPartBodyName(l.getText());
                        //部位需要查看下有没有方位
                        for (Lemma position_l : l.getRelationLemmas()) {
                            if (NlpUtil.isFeature(l.getProperty(), Constants.position_type)) {
                                Position position = new Position();
                                position.setPositionName(position_l.getText());
                                bodyPart.setPosition(position);
                            }
                        }
                        vital.setBodyPart(bodyPart);
                    } else if (NlpUtil.isFeature(l.getProperty(), Constants.vital_index_type)) {
                        Item item = new Item();
                        item.setItemName(l.getText());
                        vital.setItem(item);
                    }
                }
                containsLemmaTree.add(cnn_l);
                break;
            }
        }
        return vital;
    }
}
