package BSChecker;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import opennlp.tools.cmdline.postag.POSModelLoader;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSSample;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.tokenize.WhitespaceTokenizer;
import opennlp.tools.util.ObjectStream;
import opennlp.tools.util.PlainTextByLineStream;

public class PastTense extends Error{
	public static void main(String[] args) {
		String input = "Hamlet walked to the store, I walked to the mall.";
		Error tester = new PastTense();		
		ArrayList<int[]> found = tester.findErrors(input);
		for(int[] inds: found){
			System.out.println(inds[0] + " " + inds[1]);
		}
	}


	@Override
	public ArrayList<int[]> findErrors(String text) {

		ArrayList<int[]> found = new ArrayList<int[]>();
		POSModel model = new POSModelLoader()	
				.load(new File("lib/en-pos-maxent.bin"));
		POSTaggerME tagger = new POSTaggerME(model);

		ObjectStream<String> lineStream = new PlainTextByLineStream(new StringReader(text));
		String line;

		try {
			while ((line = lineStream.read()) != null) {

				String whitespaceTokenizerLine[] = WhitespaceTokenizer.INSTANCE
						.tokenize(line);
				String[] tags = tagger.tag(whitespaceTokenizerLine);

				POSSample sample = new POSSample(whitespaceTokenizerLine, tags);

				ArrayList<Integer> index = new ArrayList<Integer>();


				for(int i = 0; i < tags.length; i++)
				{
					if(tags[i].equals("VBD")){
						index.add(i);
					}
				}
				
				
				for(int j = 0; j < index.size(); j++)
				{
					int lastError = 0;
					boolean contains = text.contains(whitespaceTokenizerLine[index.get(j)]);
					
					while(contains)
					{
						int[] err = {text.indexOf(whitespaceTokenizerLine[index.get(j)]) + lastError,
							text.indexOf(whitespaceTokenizerLine[index.get(j)]) + lastError + whitespaceTokenizerLine[index.get(j)].length() - 1};
						found.add(err);

						// update last error index
						lastError = text.indexOf(whitespaceTokenizerLine[index.get(j)]) + whitespaceTokenizerLine[index.get(j)].length() - 1;
						
						// trims the text string
						text = text.substring(lastError);
						contains = text.contains(whitespaceTokenizerLine[index.get(j)]);
					}
					
				}
				//System.out.println(output);	
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return found;
	}
}
@@ -49,27 +49,125 @@
 java.io.
-IOException
+FileInputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.IOException;%0Aimport java.io.InputStream
 ;%0Aimport
@@ -388,16 +388,146 @@
 ggerME;%0A
+import opennlp.tools.tokenize.Tokenizer;%0Aimport opennlp.tools.tokenize.TokenizerME;%0Aimport opennlp.tools.tokenize.TokenizerModel;%0A
 import o
@@ -561,32 +561,82 @@
 spaceTokenizer;%0A
+import opennlp.tools.util.InvalidFormatException;%0A
 import opennlp.t
@@ -824,43 +824,8 @@
 lked
- to the store, I walked to the mall
 .%22;%0A
@@ -1244,16 +1244,362 @@
 model);%0A
+%09%09%0A%09%09InputStream is;%0A%09%09TokenizerModel tModel;%0A%09%09try %7B%0A%09%09%09is = new FileInputStream(%22lib/en-token.bin%22);%0A%09%09%09tModel = new TokenizerModel(is);%0A%09%09%7D catch (FileNotFoundException e1) %7B%0A%09%09%09return null;%0A%09%09%7D catch (InvalidFormatException e) %7B%0A%09%09%09return null;%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%0A%09%09Tokenizer tokenizer = new TokenizerME(tModel);
 %0A%09%09Objec
@@ -1766,71 +1766,28 @@
 ing 
-whitespaceTokenizerLine%5B%5D = WhitespaceTokenizer.INSTANCE%0A%09%09%09%09%09%09
+tokens%5B%5D = tokenizer
 .tok
@@ -1830,39 +1830,22 @@
 ger.tag(
-whitespaceTokenizerLine
+tokens
 );%0A%0A%09%09%09%09
@@ -1877,39 +1877,22 @@
 SSample(
-whitespaceTokenizerLine
+tokens
 , tags);
@@ -2184,39 +2184,22 @@
 ontains(
-whitespaceTokenizerLine
+tokens
 %5Bindex.g
@@ -2273,39 +2273,22 @@
 indexOf(
-whitespaceTokenizerLine
+tokens
 %5Bindex.g
@@ -2328,39 +2328,22 @@
 indexOf(
-whitespaceTokenizerLine
+tokens
 %5Bindex.g
@@ -2364,39 +2364,22 @@
 Error + 
-whitespaceTokenizerLine
+tokens
 %5Bindex.g
@@ -2491,72 +2491,38 @@
 xOf(
-whitespaceTokenizerLine%5Bindex.get(j)%5D) + whitespaceTokenizerLine
+tokens%5Bindex.get(j)%5D) + tokens
 %5Bind
@@ -2659,31 +2659,14 @@
 ins(
-whitespaceTokenizerLine
+tokens
 %5Bind

Change ver of toxeniser