package org.diagbot.nlp.relation.extract;

import org.algorithm.core.cnn.entity.Lemma;
import org.diagbot.nlp.relation.module.Lis;
import org.diagbot.nlp.relation.module.cell.PD;
import org.diagbot.nlp.relation.util.OutputInfo;
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.LisExtract
 * @Description TODO
 * @Author fyeman
 * @Date 2019/3/22/022 10:23
 * @Version 1.0
 **/
public class LisExtract extends BaseExtract {
    public OutputInfo extract(OutputInfo outputInfo, List<Lemma> lemmaTree, List<Lemma> lemmaParticiple) {
        List<String> repeatLisList = 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.lis_name_type)) {          //特征词 症状
                if (repeatLisList.contains(participle_lemma.getText())) {
                    continue;
                }
                Lis lis = lookLisRelations(participle_lemma, lemmaTree);
                outputInfo.getLises().add(lis);

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

    private Lis lookLisRelations(Lemma participle_lemma, List<Lemma> lemmaTree) {
        Lis lis = new Lis();
        lis.setLisName(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()) {
                    addFeatureToLis(relation_l, lis);
                }
                containsLemmaTree.add(cnn_l);
            }
        }
        return lis;
    }

    private void addFeatureToLis(Lemma lemma, Lis lis) {
        //化验结果信息
        if (NlpUtil.isFeature(lemma.getProperty(), Constants.unit_time_type)) {
            PD pd = lis.getPd();
            if (pd == null) {
                pd = new PD();
                pd.setValue(lemma.getText());
            } else {
                pd.setValue(pd.getValue() + "、" + lemma.getText());
            }
            lis.setPd(pd);
        }

        if (NlpUtil.isFeature(lemma.getProperty(), Constants.lis_result_type)) {
            lis.setValue(lemma.getText());
        }
    }
}
