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){
		String q = "";
		while(!(q = System.console().readLine("Enter a question: ")).equals("q")){
			getCategories(q);
		}
	}

	public static List<Pair<String, Double>> 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()));
		 
		    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<String, Double>> prs = new ArrayList<Pair<String, Double>>();
			for(int i = 0; i < cats.length; i++){
				Pair<String, Double> pa = new Pair<String, Double>(cats[i], new BigDecimal(vals[i]).doubleValue());
				prs.add(pa);
			}
			return prs;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

@@ -131,16 +131,71 @@
 ayList;%0A
+import java.util.Arrays;%0Aimport java.util.Collections;%0A
 import j
@@ -618,16 +618,24 @@
 t-1.1 &&
+ python3
  ./demo.
@@ -1467,19 +1467,352 @@
 %5C%5Cs+%22);%0A
-%09%09%09
+            List%3CDouble%3E valDubsAbs = new ArrayList%3CDouble%3E();%0A            for (String val : vals) %7B%0A                if (!%22%22.equals(val.trim()) &&  !val.trim().isEmpty())%0A                    valDubsAbs.add(Math.abs(new BigDecimal(val).doubleValue()));%0A            %7D%0A%0A            double maxAbs = Collections.max(valDubsAbs);%0A            
 List%3CPai
@@ -1875,16 +1875,17 @@
 le%3E%3E();%0A
+%0A
 %09%09%09for(i
@@ -1981,16 +1981,18 @@
 ats%5Bi%5D, 
+((
 new BigD
@@ -2021,16 +2021,29 @@
 Value())
+/maxAbs+1)/2)
 ;%0A%09%09%09%09pr

Normalize the values from Categorizer