package at.medunigraz.imi.abbres.stats;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import at.medunigraz.imi.abbres.dao.ValidationReader;
import at.medunigraz.imi.abbres.model.Abbreviation;
import at.medunigraz.imi.abbres.resolver.DefaultResolver;
import at.medunigraz.imi.abbres.resolver.Resolver;

public class Evaluator {
	private static final Logger LOG = LoggerFactory.getLogger(Evaluator.class);

	private ValidationReader validation;

	private Resolver resolver;

	private static final String DEFAULT_VALIDATION = "src/main/resources/validation.xlsx";

	private static final float SIMILARITY_THRESHOLD = 0.8f;

	private int correct = 0, total = 0, partial = 0;

	public Evaluator() {
		validation = new ValidationReader(new File(DEFAULT_VALIDATION));
		resolver = new DefaultResolver();
	}

	public double getAccuracy() {
		return correct / (double) total;
	}

	public double getPartialAccuracy() {
		return (correct + partial) / (double) total;
	}

	public int getPartialTotal() {
		return correct + partial;
	}

	public int getCorrect() {
		return correct;
	}

	public int getTotal() {
		return total;
	}

	public void evaluate() {
		float minGain = 1f, maxGain = 1f;
		LOG.debug(String.format("%s\t%s\t%s", "Abbr.", "Gold", "Guess"));
		while (validation.hasNext()) {
			Abbreviation gold = validation.next();
			if (gold == null) {
				continue;
			}

			float goldGain = gold.relativeGain(gold.getExpansion());
			if (goldGain > maxGain) {
				LOG.debug(String.format("Maximum gain has changed to %.4f at line %d (%s)", goldGain,
						validation.getRowNum(), gold.getToken()));
				maxGain = goldGain;
			}
			if (goldGain < minGain) {
				LOG.debug(String.format("Minimum gain has changed to %.4f at line %d (%s)", goldGain,
						validation.getRowNum(), gold.getToken()));
				minGain = goldGain;
			}

			Abbreviation guess = gold.clone();

			// Ensure guess is empty both in memory and in file
			guess.withExpansion("");
			validation.writeGuess("");

			guess.withExpansion(resolver.resolve(guess));
			float similarity = gold.tokenSimilarity(guess);
			if (gold.equals(guess)) {
				correct++;
			} else {
				if (similarity >= SIMILARITY_THRESHOLD) {
					partial++;
				} else {
					validation.writeGuess(guess);
				}
			}
			total++;
		}

		LOG.debug(String.format("%.4f < gain < %.4f", minGain, maxGain));

		validation.close();
	}

	public static void main(String args[]) {
		Evaluator e = new Evaluator();
		e.evaluate();
		LOG.info(String.format("Total: %d", e.getTotal()));
		LOG.info(String.format("Strict matches: %d (%.4f)", e.getCorrect(), e.getAccuracy()));
		LOG.info(String.format("Partial matches: %d (%.4f) (sim > %.4f)", e.getPartialTotal(), e.getPartialAccuracy(),
				SIMILARITY_THRESHOLD));
	}

}

@@ -1175,44 +1175,8 @@
 ) %7B%0A
-%09%09float minGain = 1f, maxGain = 1f;%0A
 %09%09LO
@@ -1239,16 +1239,16 @@
 ess%22));%0A
+
 %09%09while 
@@ -1361,463 +1361,8 @@
 %09%7D%0A%0A
-%09%09%09float goldGain = gold.relativeGain(gold.getExpansion());%0A%09%09%09if (goldGain %3E maxGain) %7B%0A%09%09%09%09LOG.debug(String.format(%22Maximum gain has changed to %25.4f at line %25d (%25s)%22, goldGain,%0A%09%09%09%09%09%09validation.getRowNum(), gold.getToken()));%0A%09%09%09%09maxGain = goldGain;%0A%09%09%09%7D%0A%09%09%09if (goldGain %3C minGain) %7B%0A%09%09%09%09LOG.debug(String.format(%22Minimum gain has changed to %25.4f at line %25d (%25s)%22, goldGain,%0A%09%09%09%09%09%09validation.getRowNum(), gold.getToken()));%0A%09%09%09%09minGain = goldGain;%0A%09%09%09%7D%0A%0A
 %09%09%09A
@@ -1803,82 +1803,13 @@
 ++;%0A
+
 %09%09%7D%0A%0A
-%09%09LOG.debug(String.format(%22%25.4f %3C gain %3C %25.4f%22, minGain, maxGain));%0A%0A
 %09%09va

Remove unused debug messages