package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;

import jules.Categorizer;
import jules.QueryPassager;
import jules.RankNouns;
import jules.Reranker;
import jules.ScoreWord;
import tagging.PosTagger;
import tagging.Word;
import util.Constants;
import util.Pair;

public class TestRerank {
	
	static Map<String, String> questions;
	static PrintWriter writer;
	static int queries = 100;
	
	private static void setUp() throws Exception {
		questions = new HashMap<String, String>();
		File dir = new File(Constants.qDir);
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String outFile = queries + "P:" + sdf.format(date) + ".txt";
		System.out.println("OutFile: " + outFile);
		writer = new PrintWriter(outFile, "UTF-8");
		for (File f : dir.listFiles()) {
			if(f.getName().startsWith(".")) continue;
			System.out.println("Reading file: " + f.getName());
			BufferedReader br = new BufferedReader(new FileReader(f));
			String line;
			while ((line = br.readLine()) != null) {
				String[] cols = line.split("\t");
				if (!cols[6].trim().contains(" "))
					questions.put(cols[5].trim().toLowerCase(), cols[6].trim()
							.toLowerCase());
			}
			br.close();
		}
	}
	
	public static void testReranker() throws Exception {
		setUp();
		//writer.println(Integer.toString(questions.entrySet().size()));
		for (Entry<String, String> question : questions.entrySet()) {
			List<Map<String, String>> list = QueryPassager.query(question.getKey(), queries);
			List<ScoreWord> topN = RankNouns.findTopNouns(list);
			List<Pair<String, Double>> cat;
			try{
				cat = Categorizer.getCategories(question.getKey());
			}catch(Exception e){
				continue;
			}
			if(cat==null)
				continue;
			Reranker ins = Reranker.getInstance();
			PosTagger tagger = PosTagger.getInstance();
			List<Word[]> words = tagger.tagString(question.getKey());
			List<String> qLemmas = new ArrayList<String>();
			for(Word w : words.get(0)){
				qLemmas.add(w.lemma);
			}
			List<ScoreWord> results = ins.rerank(topN, qLemmas, cat);
			int i = 0;
			for (ScoreWord sw : results) {
				String s = sw.lemma;
				i++;
				if (s.equalsIgnoreCase(question.getValue())){
					//			  index found 		rank				total number of nouns
					writer.println(i + "\t" + sw.getTotalRank() + "\t" + results.size() + "\t" + question.getKey());
					writer.flush();
					break;
				}
			}
		}
		writer.close();
	}


}

@@ -463,24 +463,48 @@
 .ScoreWord;%0A
+import minerva.Minerva;%0A
 import taggi
@@ -1729,581 +1729,62 @@
 %0A%09%09%09
-List%3CMap%3CString, String%3E%3E list = QueryPassager.query(question.getKey(), queries);%0A%09%09%09List%3CScoreWord%3E topN = RankNouns.findTopNouns(list);%0A%09%09%09List%3CPair%3CString, Double%3E%3E cat;%0A%09%09%09try%7B%0A%09%09%09%09cat = Categorizer.getCategories(question.getKey());%0A%09%09%09%7Dcatch(Exception e)%7B%0A%09%09%09%09continue;%0A%09%09%09%7D%0A%09%09%09if(cat==null)%0A%09%09%09%09continue;%0A%09%09%09Reranker ins = Reranker.getInstance();%0A%09%09%09PosTagger tagger = PosTagger.getInstance();%0A%09%09%09List%3CWord%5B%5D%3E words = tagger.tagString(question.getKey());%0A%09%09%09List%3CString%3E qLemmas = new ArrayList%3CString%3E();%0A%09%09%09for(Word w : words.get(0))%7B%0A%09%09%09%09qLemmas.add(w.lemma);%0A%09%09%09%7D
+Minerva min = new Minerva(question.getKey(), queries);
 %0A%09%09%09
@@ -1813,37 +1813,32 @@
 s = 
+m
 in
-s.rerank(topN, qLemmas, cat
+.getRerankedTopNouns(
 );%0A%09

Rewrite TestRerank.java to use the Minerva API ®