package cz.brmlab.yodaqa.analysis.answer;

import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;

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.model.Question.Focus;

/**
 * Focus annotations in a CandidateAnswerCAS. This is the focus point of the
 * clause, i.e. its "root":
 *
 *   mathematics -> mathematics
 *   several complex variables -> variables
 *   very slow -> slow
 *   many different and incompatible systems of notation for them -> systems
 *
 * It serves similar purpose as question focus, i.e. defining the answer type
 * or possibly a support for alignment. */

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

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

	public void process(JCas jcas) throws AnalysisEngineProcessException {
		Token focusTok = null;
		Annotation focus = null;

		/* We do a pretty naive thing - selecting the first noun
		 * or the last adverb / adjective / number.  No focus
		 * if there is neither. */

		if (focus == null) {
			for (Token t : JCasUtil.select(jcas, Token.class)) {
				if (t.getPos().getPosValue().matches("^NN.*")) {
					focusTok = t;
					focus = focusTok;
					break;
				} else if (t.getPos().getPosValue().matches("^RB.*")) {
					focusTok = t;
				} else if (t.getPos().getPosValue().matches("^JJ.*")) {
					focusTok = t;
				} else if (t.getPos().getPosValue().matches("^CD.*")) {
					focusTok = t;
				}
				focus = focusTok;
			}
		}

		if (focus == null) {
			logger.info("?. No focus in: " + jcas.getDocumentText());
			return;
		} else {
			logger.debug(".. Focus '{}' in: {}", focus.getCoveredText(), jcas.getDocumentText());
		}

		Focus f = new Focus(jcas);
		f.setBegin(focus.getBegin());
		f.setEnd(focus.getEnd());
		f.setBase(focus);
		f.setToken(focusTok);
		f.addToIndexes();
	}
}

package cz.brmlab.yodaqa.analysis.answer;

import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;

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.model.Question.Focus;

/**
 * Focus annotations in a CandidateAnswerCAS. This is the focus point of the
 * clause, i.e. its "root":
 *
 *   mathematics -> mathematics
 *   several complex variables -> variables
 *   very slow -> slow
 *   many different and incompatible systems of notation for them -> systems
 *
 * It serves similar purpose as question focus, i.e. defining the answer type
 * or possibly a support for alignment. */

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

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

	protected class FocusPair {
		public Token focusTok;
		public Annotation focus;

		public FocusPair(Token focusTok_, Annotation focus_) {
			focusTok = focusTok_;
			focus = focus_;
		}

		public Token getFocusTok() { return focusTok; }
		public Annotation getFocus() { return focus; }
	}

	public void process(JCas jcas) throws AnalysisEngineProcessException {
		FocusPair fp = null;

		if (fp == null)
			fp = fpByPos(jcas);

		if (fp == null) {
			logger.info("?. No focus in: " + jcas.getDocumentText());
			return;
		} else {
			logger.debug(".. Focus '{}' in: {}", fp.getFocus().getCoveredText(), jcas.getDocumentText());
		}

		Focus f = new Focus(jcas);
		f.setBegin(fp.getFocus().getBegin());
		f.setEnd(fp.getFocus().getEnd());
		f.setBase(fp.getFocus());
		f.setToken(fp.getFocusTok());
		f.addToIndexes();
	}

	/* We do a pretty naive thing - selecting the first noun or
	 * the last adverb / adjective / number. */
	protected FocusPair fpByPos(JCas jcas) {
		Token focusTok = null;
		Annotation focus = null;

		for (Token t : JCasUtil.select(jcas, Token.class)) {
			if (t.getPos().getPosValue().matches("^NN.*")) {
				focusTok = t;
				focus = focusTok;
				break;
			} else if (t.getPos().getPosValue().matches("^RB.*")) {
				focusTok = t;
			} else if (t.getPos().getPosValue().matches("^JJ.*")) {
				focusTok = t;
			} else if (t.getPos().getPosValue().matches("^CD.*")) {
				focusTok = t;
			}
			focus = focusTok;
		}

		if (focusTok != null)
			return new FocusPair(focusTok, focus);
		else
			return null;
	}
}

Move POS-based logic to helper method fpByPos()