package jules;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import util.Pair;

public class Categorizer {
	
	public static void main(String[] args){
		getCategories("Vad heter Sveriges huvudstad?");
	}

	public static List<Pair> getCategories(String q) {
		String question = q;
		StringBuffer output = new StringBuffer();
		String[] cmdarray = {"bash","-c", "cd ./libshorttext-1.1 && ./demo.py " + q};
		try {
			Process p = Runtime.getRuntime().exec(cmdarray);
		    p.waitFor();
		
		    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
		 
		    StringBuilder sb = new StringBuilder();
		    String line = "";
		    String categories = "";
		    String decvals = "";
		    boolean catComming = false;
	    
			while ((line = reader.readLine())!= null) {
				if(catComming){
					categories = line.trim();
					catComming = false;
				} 
				
				if (line.contains(question)){
					catComming = true;
				}
				
				if(line.contains("decval")){
					decvals = line.substring(10).trim();
				}
				output.append(line + "\n");
			}
			System.out.println(output.toString());
			System.out.println(categories);
			System.out.println(decvals);
			
			String[] cats = categories.split("\\s+");
			String[] vals = decvals.split("\\s+");
			List<Pair> prs = new ArrayList<Pair>();
			for(int i = 0; i < cats.length; i++){
				Pair pa = new Pair(cats[i], new BigDecimal(vals[i]));
				System.out.println(pa.fst + "=" + pa.snd);
				prs.add(pa);
			}
			return prs;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

@@ -250,55 +250,124 @@
 %7B%0A%09%09
-getCategories(%22Vad heter Sveriges huvudstad?%22);
+String q = %22%22;%0A%09%09while(!(q = System.console().readLine(%22Enter a question: %22)).equals(%22q%22))%7B%0A%09%09%09getCategories(q);%0A%09%09%7D
 %0A%09%7D%0A

add eaasy testing for classification of question