package BSChecker;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;


import opennlp.tools.cmdline.postag.POSModelLoader;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.tokenize.Tokenizer;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;
import opennlp.tools.util.InvalidFormatException;
import opennlp.tools.util.ObjectStream;
import opennlp.tools.util.PlainTextByLineStream;

public class VagueThisWhichError extends Error {

	public static void main(String[] args){
		String test = "Hi, my name is slim shady. Which is fun! I have this car in this.";
		ArrayList<int[]> errs = new VagueThisWhichError().findErrors(test);
		for(int[] err: errs){
			System.out.println(err[0] + " " + err[1] );
			System.out.println(test.substring(err[0], err[1]));
		}
	}
	
	public ArrayList<int[]> findErrors(String text){
		
		ArrayList<int[]> found = new ArrayList<int[]>();
		POSModel model = new POSModelLoader().load(new File("lib/en-pos-maxent.bin"));
		ObjectStream<String> lineStream = new PlainTextByLineStream(new StringReader(text));
		
		InputStream is;
		TokenizerModel tModel;
		try {
			is = new FileInputStream("lib/en-token.bin");
			tModel = new TokenizerModel(is);
		} catch (FileNotFoundException e1) {
			return null;
		} catch (InvalidFormatException e) {
			return null;
		} catch (IOException e) {
			return null;
		}
		
		
		Tokenizer tokenizer = new TokenizerME(tModel);
		POSTaggerME tagger = new POSTaggerME(model);
		String line;
		int totLen =0;
		try {
			while ((line = lineStream.read()) != null) {
				
				String tokens[] = tokenizer.tokenize(line);
				String[] tags = tagger.tag(tokens);
				int lineLen = 0;
				int wFound = 0, tFound = 0;
				for(int i = 0; i < tokens.length; i++){
					if(tokens[i].equalsIgnoreCase("this")){
						if(i == tokens.length-1 || tags[i+1].charAt(0)!='N'){
							int[] err = {totLen+locationOf(line,tokens[i],tFound)-1,
									totLen+locationOf(line,tokens[i],tFound)+tokens[i].length()-1};
							found.add(err);	
						}
						tFound++;
					}
					if(tokens[i].equalsIgnoreCase("which")){
						if(i == 0 || tags[i-1].charAt(0)!='N'){
							int[] err = {totLen+locationOf(line,tokens[i],tFound)-1,
									totLen+locationOf(line,tokens[i],tFound)+tokens[i].length()-1};
							found.add(err);	
						}
						wFound++;
					}
				}
				totLen+=line.length();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return found;
	}

	private int locationOf(String line, String string, int found) {
		int loc = 0;
		for(int i = 0; i <= found; i++){
			loc = line.indexOf(string,loc) +1;
		}
		return loc;
	}

	public static boolean equalsWord(String a, String b){
		return a.equalsIgnoreCase(b) || (a.substring(0,a.length()-1).equalsIgnoreCase(b) && isPunctuation(a.charAt(a.length()-1)));
	}
	private static boolean isPunctuation(char c) {
		return c=='.'||c=='!'||c=='?'||c==';';
	}

	public static boolean endOfSentence(String word) {
		char fChar = word.charAt(word.length()-1);
		return fChar == ';' || fChar =='.'||fChar=='?'||fChar=='!';
	}
}

@@ -781,16 +781,21 @@
 ve this 
+blue 
 car in t
@@ -2014,55 +2014,29 @@
 if(i
- == tokens.length-1 %7C%7C tags%5Bi+1%5D.charAt(0)!='N'
+sVague(tokens,tags,i)
 )%7B%0A%09
@@ -2662,16 +2662,274 @@
 nd;%0A%09%7D%0A%0A
+%09private boolean isVague(String%5B%5D tokens, String%5B%5D tags, int i) %7B%0A%09%09if(i==tokens.length-1) return true;%0A%09%09for(int j = i+1; j %3C tokens.length; j++)%7B%0A%09%09%09if(tags%5Bj%5D.charAt(0)=='N') return false;%0A%09%09%09if(tags%5Bj%5D.charAt(0)=='V') return true;%0A%09%09%7D%0A%09%09return true;%0A%09%7D%0A%0A
 %09private

Fix vagueThis