package jules;

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

import util.Constants;
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 = Constants.whiteList(q);
		StringBuffer output = new StringBuffer();
		String tmpQ = q.replace("&", "\\&");
		String[] cmdarray = { "bash", "-c", "cd ./libshorttext-1.1 && python3 ./demo.py \"" + tmpQ + "\"" };
		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{1,10}");

			String[] vals = decvals.split("\\s+");
			List<Double> valDubsAbs = new ArrayList<Double>();
			for (String val : vals) {
				if (!"".equals(val.trim()) || !val.trim().isEmpty())
					valDubsAbs.add(Math.abs(new BigDecimal(val).doubleValue()));
			}

			double maxAbs = Collections.max(valDubsAbs);
			List<Pair<String, Double>> prs = new ArrayList<Pair<String, Double>>();

			for (int i = 0; i < cats.length; i++) {
				cats[i] = cats[i].trim();
				if (!cats[i].isEmpty()) {
					try {
						Pair<String, Double> pa = new Pair<String, Double>(cats[i],
								((new BigDecimal(vals[i]).doubleValue()) / maxAbs + 1) / 2);
						prs.add(pa);
					} catch (Exception e) {
						System.err.println("Something went wrong in categorizer: ");
						System.out.println(question);
						System.out.println(categories);
						System.out.println(decvals);
						// prs.add(new Pair<String, Double>("other", 0.0));
					}
				}
			}
			return prs;
		} catch (Exception e) {
			System.out.println(q);
			e.printStackTrace();
		}
		return null;
	}
}

@@ -582,47 +582,8 @@
 ();%0A
-%09%09String tmpQ = q.replace(%22&%22, %22%5C%5C&%22);%0A
 %09%09St
@@ -670,12 +670,16 @@
 %22 + 
-tmpQ
+question
  + %22

remove redundant replace