package cz.brmlab.yodaqa.analysis.answer;

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.resource.ResourceInitializationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cz.brmlab.yodaqa.model.CandidateAnswer.AnswerInfo;

/**
 * Generate a canonic form of the answer in CandidateAnswerCAS and
 * store it in the AnswerInfo. It is not for user consumption but for
 * internal comparison between syntactically similar answers.
 *
 * Basically, we lowercase, remove leading/trailing whitespace and
 * interpunction and leading DETs. */

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

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

	public void process(JCas jcas) throws AnalysisEngineProcessException {
		String text = jcas.getDocumentText();
		String canonText = getCanonText(text.toLowerCase());

		AnswerInfo ai = JCasUtil.selectSingle(jcas, AnswerInfo.class);
		ai.removeFromIndexes();
		ai.setCanonText(canonText);
		ai.addToIndexes();
	}

	/* Basically, throw away any DET and decorations at the
	 * beginning and end (like quotes, commas or apostrophes). */
	/* XXX: This method is also called from entirely different
	 * analysis classes. */
	public static String getCanonText(String text) {
		text = text.replaceAll("\\W*$", "");
		text = text.replaceAll("^\\W*", "");
		text = text.replaceAll("^(?i)(the|a|an|one)\\s+", "");
		text = text.replaceAll("^\\W*", "");
		return text;
	}
}

@@ -1604,16 +1604,161 @@
 text) %7B%0A
+%09%09/* If the text is completely %5CW, keep it that way;%0A%09%09 * e.g. %3C%3Cwhich number corresponds to * in ASCII?%3E%3E. */%0A%09%09if (!text.matches(%22%5E%5C%5CW*$%22)) %7B%0A%09
 %09%09text =
@@ -1780,32 +1780,33 @@
 l(%22%5C%5CW*$%22, %22%22);%0A
+%09
 %09%09text = text.re
@@ -1820,32 +1820,36 @@
 l(%22%5E%5C%5CW*%22, %22%22);%0A
+%09%09%7D%0A
 %09%09text = text.re
@@ -1889,16 +1889,48 @@
 %22, %22%22);%0A
+%09%09if (!text.matches(%22%5E%5C%5CW*$%22))%0A%09
 %09%09text =

Fix an edge case when the whole text would be \W