import java.io.*;
import java.util.Arrays;

/**
 * N-gram Punctuation Prediction. Requires a corpus with the punctuation symbols
 * tokenized as , --> ,COMMA . --> .PERIOD ? --> ?QMARK
 * 
 * @author joakimlilja
 *
 */
public class PunctuationPredicter {

	public static final String CORPUS_TEST_PATH = "ppSentenses.txt";
	public static NGramWrapper nGramWrapper;

	/**
	 * Contructor
	 * 
	 * @param nGramLength
	 *            - length of n-gram
	 * @param corpusPath
	 *            - path to corpus
	 */
	public PunctuationPredicter(int nGramLength, String corpusPath) {
		nGramWrapper = new NGramWrapper(nGramLength);
		if (!corpusPath.equals("")) {
			nGramWrapper.readFile(new File(corpusPath));
		} else {
			nGramWrapper.readFile(new File(CORPUS_TEST_PATH));
		}
	}

	/**
	 * Predicts the most likely sentence with punctuation symbols inserted given
	 * the input
	 * 
	 * @param input
	 *            - string from where the prediction is to be made
	 * @return the predicted sentence
	 */
	public String predictPunctuation(String input) {
		System.err
				.println("-----------------------PREDICTION---------------------------");

		// Split into words
		String[] words = input.split(" ");

		// Generate all possible punctuation combinations
		HyperStringFSA3 hypString = new HyperStringFSA3(words, nGramWrapper);

		// For each combination get it's count (last index)
		String prediction = "";
		double maxCount = 0;
		for (String[] s : hypString.getOutputs()) {
			//System.err.println(Arrays.toString(s));
			double count = Double.parseDouble(s[s.length - 1]);
			if (count > maxCount) {
				prediction = "";
				maxCount = count;
				for (String w : s) {
					prediction += w + " ";
				}
			}
		}

		return prediction;
	}

	// Test method to run input from command line
	private void handleInput(int nGramLength) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					System.in));
			String input = br.readLine();

			while (input != null) {
				System.out.println(predictPunctuation(input));
				input = br.readLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// Test
	public static void main(String[] args) {
		int nGramLength = 3;
		for (int i = 0; i < args.length; i += 2) {
			if (args[i].equals("n-gram")) {
				nGramLength = Integer.parseInt(args[i + 1]);
			}
		}
		PunctuationPredicter pI = new PunctuationPredicter(nGramLength, "ppCorpus.txt");
        String evaluate = "testSentences.txt";
        try {
            BufferedReader br = new BufferedReader(new FileReader(evaluate));
            int counter = 6;
            while(counter>0) { //Risky?
                long time = System.currentTimeMillis();
                String fix = br.readLine();
                System.err.println("----------------------");
                System.err.println(fix);
                fix = fix.trim().replaceAll("( )+", " ");
                System.err.println("---------------------");
                System.err.println(fix);
                System.out.println(pI.predictPunctuation(fix));
                time = System.currentTimeMillis()-time;
                time = time/1000;
                System.err.println("Spent "+time+" s calculating sentence.");
                counter--;
            }
        } catch(IOException e) {
            e.printStackTrace();
        }
		//System.out.println("Ready for prediction");
		//pI.handleInput(nGramLength);
	}
}
@@ -1566,16 +1566,57 @@
  - 1%5D);%0A
+            //System.err.println(count);%0A
 %09%09%09if (c
@@ -2622,9 +2622,25 @@
 r = 
-6
+Integer.MAX_VALUE
 ;%0A  
@@ -2655,16 +2655,17 @@
   while(
+(
 counter%3E
@@ -2666,16 +2666,29 @@
 unter%3E0)
+&&br.ready())
  %7B //Ris
@@ -2799,32 +2799,34 @@
                 
+//
 System.err.print
@@ -2842,33 +2842,32 @@
 ----------------
--
 %22);%0A            
@@ -2862,32 +2862,34 @@
                 
+//
 System.err.print
@@ -3069,24 +3069,26 @@
             
+//
 System.out.p
@@ -3119,24 +3119,68 @@
 tion(fix));%0A
+                pI.predictPunctuation(fix);%0A
             

Structure change but essentially the same