package jules;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import tagging.PosTagger;
import tagging.Word;
import util.Constants;
import util.Pair;

public class LiblinearInit {
	/**
	 * iterate over questions 
	 * 		gather unique question words 
	 * 		query qs and gather all answer words 
	 * 		gather question category stats (scaled?)
	 * 
	 * 
	 * 0/1 List<question> List<Answer> List<catstats>
	 * 
	 * @param args
	 */

	static File dir = new File(Constants.qDir);

	static TreeSet<String> uniqueQuestions = new TreeSet<String>();
	static TreeSet<String> uniqueAnswers = new TreeSet<String>();
	static String[] uniqueCategories = { "concept", "location", "definition",
			"description", "multiplechoice", "amount", "organization",
			"other", "person", "abbreviation", "verb", "title",
			"timepoint", "duration", "money" };
	
	
	private static void headerCreator(){

		try{
			PrintWriter writer = new PrintWriter(Constants.liblinearWordMap, "UTF-8");
			
			String line = "";
			int i = 1;
			
			for(String q : uniqueQuestions){
				line += "\t" + i + ":" + q;
				i++;
			}
			writer.println(line.trim());
			line = "";
			for(String a : uniqueAnswers){
				line += "\t" + i + ":" + a;
				i++;
			}

			writer.println(line.trim());
			line = "";
			for(String c : uniqueCategories){
				line += "\t" + i + ":" + c;
				i++;
			}
			writer.println(line.trim());
			writer.close();
		} catch (Exception e){
			
		}
	}
	
	public static void createLiblinearTrain() {

		List<String> corrAnswers = new ArrayList<String>();
		List<String> questions = new ArrayList<String>();
		List<List<String>> answers = new ArrayList<List<String>>();
		List<List<Pair<String, Double>>> catstats = new ArrayList<List<Pair<String, Double>>>();

		try {
			PosTagger tagger = PosTagger.getInstance();
			PrintWriter writer = new PrintWriter(Constants.liblinearTrain, "UTF-8");
			int n = 0;
			for (File f : dir.listFiles()) {
				if (f.getName().startsWith("."))
					continue;
				n++;
				System.out.println("Reading file: (" + n+"/7) " + f.getName());
				BufferedReader br = new BufferedReader(new FileReader(f));
				String line, question, corrAnswer;

				while ((line = br.readLine()) != null) {
					String[] cols = line.split("\t");
					corrAnswer = cols[6].toLowerCase();
					if (corrAnswer.contains(" ")) continue;
					System.out.println(corrAnswer);

                    corrAnswers.add(tagger.tagString(corrAnswer).get(0)[0].lemma);

					question = cols[5].toLowerCase();
					questions.add(question);

					List<String> as = new ArrayList<String>();
					for (ScoreWord sw : RankNouns.findTopNouns(QueryPassager.query(question, 100))) {
						as.add(sw.lemma);
						uniqueAnswers.add(sw.lemma);
					}
					if(!as.contains(corrAnswer)){
						as.add(corrAnswer);
						uniqueAnswers.add(corrAnswer);
					}
					answers.add(as);

					for (Word w : tagger.tagString(question).get(0)) {
						uniqueQuestions.add(w.lemma);
					}
					List<Pair<String, Double>> cStat = Categorizer.getCategories(question);
					catstats.add(cStat);
				}
				br.close();

			}
			int qSize = uniqueQuestions.size();
			int aSize = uniqueAnswers.size();
			int qaSize = qSize + aSize;

			String line = "";
			try {
				for (int i = 0; i < questions.size(); i++) {
					for (String str : answers.get(i)) {
						line = "";
						if (str.equalsIgnoreCase(corrAnswers.get(i))) {
							line += "1";
						} else {
							line += "0";
						}
						TreeSet<String> ulq = new TreeSet<String>();
						for (Word word : tagger.tagString(questions.get(i)).get(0)) {
							ulq.add(word.lemma);
						}
						for (String sulq : ulq) {
							line += "\t" + (uniqueQuestions.headSet(sulq).size()+1) + ":1";
						}
						line += "\t" + (uniqueAnswers.headSet(str).size()+1 + qSize) + ":1";
						for (int j = 0; j < uniqueCategories.length; j++) {
							try {
								for (Pair<String, Double> cs : catstats.get(i)) {
									if (cs.fst.equalsIgnoreCase(uniqueCategories[j])) {
										line += "\t" + (qaSize + j + 1) + ":" + cs.snd;
									}
								}
							} catch (Exception e) {
								continue;
							}
						}
						writer.println(line);
					}
				}

			} catch (NullPointerException npe) {
				npe.printStackTrace();
				System.out.println("nullpointer");
				System.out.println(line);
			}
			writer.close();
			headerCreator();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void testRankTopNouns(String q) throws IOException {
		List<Map<String, String>> res = QueryPassager.query(q, 100);
		List<ScoreWord> lm = RankNouns.findTopNouns(res);
		for (ScoreWord sw : lm) {
			System.out.println(sw.word + " : " + sw.lemma + " : " + sw.getTotalRank());
		}

	}
}

@@ -244,16 +244,40 @@
 eeSet;%0A%0A
+import minerva.Minerva;%0A
 import t
@@ -2775,85 +2775,91 @@
 %09%09%09%09
-for (ScoreWord sw : RankNouns.findTopNouns(QueryPassager.query(question, 100)
+Minerva min = new Minerva(question, 100);%0A%09%09%09%09%09for (ScoreWord sw : min.getTopNouns(
 )) %7B
@@ -4676,59 +4676,33 @@
 %7B%0A%09%09
-List%3CMap%3CString, String%3E%3E res = QueryPassager.query
+Minerva min = new Minerva
 (q, 
@@ -4709,21 +4709,21 @@
 100);%0A%09%09
-List%3C
+for (
 ScoreWor
@@ -4727,68 +4727,31 @@
 Word
-%3E lm = RankNouns.findTopNouns(res);%0A%09%09for (ScoreWord sw : lm
+ sw : min.getTopNouns()
 ) %7B%0A

Use the Minerva API ® in LiblinearInit.java