package cz.brmlab.yodaqa.analysis.result;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.CASException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.descriptor.SofaCapability;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cz.brmlab.yodaqa.model.Question.Clue;
import cz.brmlab.yodaqa.model.SearchResult.CandidateAnswer;
import cz.brmlab.yodaqa.model.SearchResult.Passage;

import de.tudarmstadt.ukp.dkpro.core.api.ner.type.NamedEntity;

/**
 * Create CandidateAnswers for all NEs (named entities) that do not
 * contain supplied clues.
 *
 * This is pretty naive but should generate some useful answers. */

@SofaCapability(
	inputSofas = { "Question", "PickedPassages" },
	outputSofas = { "PickedPassages" }
)

public class CanByNESurprise extends JCasAnnotator_ImplBase {
	final Logger logger = LoggerFactory.getLogger(CanByNPSurprise.class);

	public void initialize(UimaContext aContext) throws ResourceInitializationException {
		super.initialize(aContext);
	}

	public void process(JCas jcas) throws AnalysisEngineProcessException {
		JCas questionView, passagesView;
		try {
			questionView = jcas.getView("Question");
			passagesView = jcas.getView("PickedPassages");
		} catch (CASException e) {
			throw new AnalysisEngineProcessException(e);
		}
		for (NamedEntity ne : JCasUtil.select(passagesView, NamedEntity.class)) {
			String text = ne.getCoveredText();

			/* TODO: This can be optimized a lot. */
			boolean matches = false;
			for (Clue clue : JCasUtil.select(questionView, Clue.class)) {
				if (text.contains(clue.getCoveredText())) {
					matches = true;
					break;
				}
			}
			if (matches)
				continue;

			/* Surprise! */

			logger.info("caNE {}", ne.getCoveredText());
			CandidateAnswer ca = new CandidateAnswer(passagesView);
			ca.setBegin(ne.getBegin());
			ca.setEnd(ne.getEnd());
			ca.setPassage(JCasUtil.selectCovering(Passage.class, ne).get(0));
			ca.setBase(ne);
			ca.setConfidence(1.0);
			ca.addToIndexes();
		}
	}
}

@@ -1110,17 +1110,17 @@
 r(CanByN
-P
+E
 Surprise
@@ -1544,30 +1544,25 @@
 %09%09%7D%0A
+%0A
 %09%09for (
-NamedEntity ne 
+Passage p
 : JC
@@ -1589,16 +1589,82 @@
 esView, 
+Passage.class)) %7B%0A%09%09%09for (NamedEntity ne : JCasUtil.selectCovered(
 NamedEnt
@@ -1672,24 +1672,27 @@
 ty.class
+, p
 )) %7B%0A%09%09%09
 String t
@@ -1683,16 +1683,17 @@
 )) %7B%0A%09%09%09
+%09
 String t
@@ -1720,16 +1720,17 @@
 ext();%0A%0A
+%09
 %09%09%09/* TO
@@ -1765,16 +1765,17 @@
 lot. */%0A
+%09
 %09%09%09boole
@@ -1793,24 +1793,25 @@
 = false;%0A%09%09%09
+%09
 for (Clue cl
@@ -1856,24 +1856,25 @@
 e.class)) %7B%0A
+%09
 %09%09%09%09if (text
@@ -1914,16 +1914,17 @@
  %7B%0A%09%09%09%09%09
+%09
 matches 
@@ -1940,15 +1940,17 @@
 %09%09%09%09
+%09
 break;%0A
+%09
 %09%09%09%09
@@ -1954,18 +1954,20 @@
 %09%09%09%7D%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09if (m
@@ -1978,16 +1978,17 @@
 es)%0A%09%09%09%09
+%09
 continue
@@ -1990,16 +1990,17 @@
 tinue;%0A%0A
+%09
 %09%09%09/* Su
@@ -2011,16 +2011,17 @@
 se! */%0A%0A
+%09
 %09%09%09logge
@@ -2060,16 +2060,18 @@
 ext());%0A
+%0A%09
 %09%09%09Candi
@@ -2120,24 +2120,25 @@
 esView);%0A%09%09%09
+%09
 ca.setBegin(
@@ -2152,24 +2152,25 @@
 egin());%0A%09%09%09
+%09
 ca.setEnd(ne
@@ -2177,24 +2177,25 @@
 .getEnd());%0A
+%09
 %09%09%09ca.setPas
@@ -2203,60 +2203,13 @@
 age(
-JCasUtil.selectCovering(Passage.class, ne).get(0)
+p
 );%0A
+%09
 %09%09%09c
@@ -2223,16 +2223,17 @@
 se(ne);%0A
+%09
 %09%09%09ca.se
@@ -2250,16 +2250,17 @@
 e(1.0);%0A
+%09
 %09%09%09ca.ad
@@ -2273,16 +2273,21 @@
 exes();%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%7D

Fix passage determination in case the NamedEntity spans a fullstop