package org.diagbot.nlp.relation.extract;

import org.algorithm.core.cnn.entity.Lemma;
import org.diagbot.nlp.relation.module.Pacs;
import org.diagbot.nlp.relation.module.Treat;
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.PacsExtract
 * @Description TODO
 * @Author fyeman
 * @Date 2019/3/25/025 10:05
 * @Version 1.0
 **/
public class TreatExtract extends BaseExtract {
    public OutputInfo extract(OutputInfo outputInfo, List<Lemma> lemmaTree, List<Lemma> lemmaParticiple) {
        List<String> repeatTreatList = 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.medicine_type)) {          //特征词
                if (repeatTreatList.contains(participle_lemma.getText())) {
                    continue;
                }
                Treat treat = lookTreatRelations(participle_lemma, lemmaTree);
                outputInfo.getTreats().add(treat);

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

    private Treat lookTreatRelations(Lemma participle_lemma, List<Lemma> lemmaTree) {
        Treat treat = new Treat();
        treat.setTreatName(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()) {
                    addFeatureToTreat(relation_l, treat);
                }
                containsLemmaTree.add(cnn_l);
            }
        }
        return treat;
    }

    private void addFeatureToTreat(Lemma lemma, Treat treat) {
        if (NlpUtil.isFeature(lemma.getProperty(), Constants.treatment_type)) {
            treat.setValue(lemma.getText());
        }
    }
}
