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.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 PacsExtract extends BaseExtract {
    public OutputInfo extract(OutputInfo outputInfo, List<Lemma> lemmaTree, List<Lemma> lemmaParticiple) {
        List<String> repeatPacsList = 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.pacs_name_type)) {          //特征词 症状
                if (repeatPacsList.contains(participle_lemma.getText())) {
                    continue;
                }
                Pacs pacs = lookPacsRelations(participle_lemma, lemmaTree);
                outputInfo.getPacses().add(pacs);

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

    private Pacs lookPacsRelations(Lemma participle_lemma, List<Lemma> lemmaTree) {
        Pacs pacs = new Pacs();
        pacs.setPacsName(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, pacs);
                }
                containsLemmaTree.add(cnn_l);
            }
        }
        return pacs;
    }

    private void addFeatureToLis(Lemma lemma, Pacs pacs) {
        if (NlpUtil.isFeature(lemma.getProperty(), Constants.pacs_result_type)) {
            pacs.setValue(lemma.getText());
        }
    }
}
