package cz.brmlab.yodaqa.analysis;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.Type;
import org.apache.uima.cas.text.AnnotationIndex;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.fit.util.CasUtil;
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.model.CandidateAnswer.PassageForParsing;

import de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS;

/**
 * An annotator that will create sofa-wide annotation if parsing is required.
 * That is, they aren't annotated even by POS annotations yet. Then, by aiming
 * StanfordParser at this annotation (PassageForParsing), we can make sure the
 * CAS is annotated with parsing tags iff it wasn't parsed yet. */

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

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

	@Override
	public void process(JCas jcas) throws AnalysisEngineProcessException {
		for (POS pos : JCasUtil.select(jcas, POS.class))
			return; // ok, already parser
		if (jcas.getDocumentText().length() == 0)
			return; // nothing to parse either

		PassageForParsing pfp = new PassageForParsing(jcas);
		pfp.setBegin(0);
		pfp.setEnd(jcas.getDocumentText().length());
		pfp.addToIndexes();
		logger.debug("requesting additional parse: {}", pfp.getCoveredText());
	}
}

@@ -163,229 +163,44 @@
 ima.
-cas.Type;%0Aimport org.apache.uima.cas.text.AnnotationIndex;%0Aimport org.apache.uima.fit.component.JCasAnnotator_ImplBase;%0Aimport org.apache.uima.fit.descriptor.ConfigurationParameter;%0Aimport org.apache.uima.fit.util.CasUtil
+fit.component.JCasAnnotator_ImplBase
 ;%0Aim
@@ -277,53 +277,8 @@
 as;%0A
-import org.apache.uima.jcas.tcas.Annotation;%0A
 impo
@@ -462,16 +462,16 @@
 rsing;%0A%0A
-
 import d
@@ -525,16 +525,82 @@
 pos.POS;
+%0Aimport de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;
 %0A%0A/**%0A *
@@ -1328,16 +1328,111 @@
  parser%0A
+%09%09if (JCasUtil.select(jcas, Token.class).isEmpty())%0A%09%09%09return; // XXX can happen with crf, bug%0A
 %09%09if (jc

Fix StanfordParser crash in some weird CRF-generated situations