package cz.brmlab.yodaqa.analysis.question;

import java.util.List;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.Annotation;
import org.apache.uima.resource.ResourceInitializationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cz.brmlab.yodaqa.analysis.TreeUtil;
import cz.brmlab.yodaqa.model.Question.Clue;
import cz.brmlab.yodaqa.model.Question.ClueSubject;
import cz.brmlab.yodaqa.model.Question.ClueSubjectAux;

import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;
import de.tudarmstadt.ukp.dkpro.core.api.syntax.type.constituent.Constituent;
import de.tudarmstadt.ukp.dkpro.core.api.syntax.type.constituent.ROOT;
import de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.NSUBJ;

/**
 * Generate Clue annotations in a QuestionCAS. These represent key information
 * stored in the question that is then used in primary search.
 *
 * This generates clues from the question subject, i.e. NSUBJ annotation.
 * E.g. in "When did Einstein die?", subject is "Einstein" and will have such
 * a clue generated. */

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

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

	public void process(JCas jcas) throws AnalysisEngineProcessException {
		for (ROOT sentence : JCasUtil.select(jcas, ROOT.class)) {
			processSentence(jcas, sentence);
		}
	}

	public void processSentence(JCas jcas, Constituent sentence) throws AnalysisEngineProcessException {
		for (NSUBJ subj : JCasUtil.selectCovered(NSUBJ.class, sentence)) {
			Token stok = subj.getDependent();

			/* Skip question word focuses (e.g. "Who"). */
			if (stok.getPos().getPosValue().matches("^W.*"))
				continue;

			addClue(new ClueSubject(jcas), subj.getBegin(), subj.getEnd(), subj, 2.5);
		}
	}

	protected void addClue(Clue clue, int begin, int end, Annotation base, double weight) {
		clue.setBegin(begin);
		clue.setEnd(end);
		clue.setBase(base);
		clue.setWeight(weight);
		clue.setLabel(clue.getCoveredText());
		clue.setIsReliable(true);
		clue.addToIndexes();
		logger.debug("new by {}: {}", base.getType().getShortName(), clue.getLabel());
	}
}

@@ -670,16 +670,79 @@
 ctAux;%0A%0A
+import de.tudarmstadt.ukp.dkpro.core.api.ner.type.NamedEntity;%0A
 import d
@@ -1012,21 +1012,172 @@
 endency.
-NSUBJ
+Dependency;%0Aimport de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.NSUBJ;%0Aimport de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.NSUBJPASS
 ;%0A%0A/**%0A 
@@ -2092,20 +2092,245 @@
 ntence))
+%0A%09%09%09processSubj(jcas, subj);%0A%09%09for (NSUBJPASS subj : JCasUtil.selectCovered(NSUBJPASS.class, sentence))%0A%09%09%09processSubj(jcas, subj);%0A%09%7D%0A%0A%09public void processSubj(JCas jcas, Dependency subj) throws AnalysisEngineProcessException
  %7B%0A
-%09
 %09%09Token 
@@ -2360,17 +2360,16 @@
 t();%0A%0A%09%09
-%09
 /* Skip 
@@ -2407,17 +2407,16 @@
 o%22). */%0A
-%09
 %09%09if (st
@@ -2465,21 +2465,17 @@
 %0A%09%09%09
-%09
-continue
+return
 ;%0A%0A
-%09
 %09%09ad
@@ -2547,20 +2547,16 @@
 , 2.5);%0A
-%09%09%7D%0A
 %09%7D%0A%0A%09pro

Mark NSUBJPASS as ClueSubject too