package cz.brmlab.yodaqa.analysis.answer;

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

import cz.brmlab.yodaqa.model.AnswerHitlist.Answer;

/**
 * Annotate the AnswerHitlistCAS Answer FSes with score based on the
 * present AnswerFeatures.  This particular implementation uses
 * the estimated probability of the answer being correct as determined
 * by logistic regression based classifier trained on the training set
 * using the data/ml/train-answer.py script. */


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

	/** The weights of individual elements of the FV.  These weights
	 * are output by data/ml/train-answer.py as this:
	 * (array([[ 0.19767668,  0.        ,  0.25141517,  0.        ,  0.13811982,
	 *          -0.7311968 , -0.07886722, -0.09433039, -0.18034131,  0.0071437 ,
	 *           0.35216924, -0.52536685,  0.91762568, -0.04371385,  0.79076109,
	 *           0.24020316,  0.22022939,  0.24020316,  0.16670926, -0.33990687]]), array([-0.17319761]))
	 */
	public static double weights[] = {
		 0.19767668,  0.        ,  0.25141517,  0.        ,  0.13811982,
		-0.7311968 , -0.07886722, -0.09433039, -0.18034131,  0.0071437 ,
		 0.35216924, -0.52536685,  0.91762568, -0.04371385,  0.79076109,
		 0.24020316,  0.22022939,  0.24020316,  0.16670926, -0.33990687
	};
	public static double intercept = -0.17319761;

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

	protected class AnswerScore {
		public Answer a;
		public double score;

		public AnswerScore(Answer a_, double score_) {
			a = a_;
			score = score_;
		}
	}

	public void process(JCas jcas) throws AnalysisEngineProcessException {
		List<AnswerScore> answers = new LinkedList<AnswerScore>();

		for (Answer a : JCasUtil.select(jcas, Answer.class)) {
			AnswerFV fv = new AnswerFV(a);

			double t = intercept;
			double fvec[] = fv.getFV();
			for (int i = 0; i < fvec.length; i++) {
				t += fvec[i] * weights[i];
			}

			double prob = 1.0 / (1.0 + Math.exp(-t));
			answers.add(new AnswerScore(a, prob));
		}

		/* Reindex the touched answer info(s). */
		for (AnswerScore as : answers) {
			as.a.removeFromIndexes();
			as.a.setConfidence(as.score);
			as.a.addToIndexes();
		}
	}
}

@@ -966,14 +966,16 @@
 core
-Simple
+Logistic
 .cla

Fix logger class