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 blue 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(isVague(tokens,tags,i)){
							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 boolean isVague(String[] tokens, String[] tags, int i) {
		if(i==tokens.length-1) return true;
		for(int j = i+1; j < tokens.length; j++){
			if(tags[j].charAt(0)=='N') return false;
			if(tags[j].charAt(0)=='V') return true;
		}
		return true;
	}

	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=='!';
	}
}

@@ -2922,447 +2922,7 @@
 ;%0A%09%7D
-%0A%0A%09public static boolean equalsWord(String a, String b)%7B%0A%09%09return a.equalsIgnoreCase(b) %7C%7C (a.substring(0,a.length()-1).equalsIgnoreCase(b) && isPunctuation(a.charAt(a.length()-1)));%0A%09%7D%0A%09private static boolean isPunctuation(char c) %7B%0A%09%09return c=='.'%7C%7Cc=='!'%7C%7Cc=='?'%7C%7Cc==';';%0A%09%7D%0A%0A%09public static boolean endOfSentence(String word) %7B%0A%09%09char fChar = word.charAt(word.length()-1);%0A%09%09return fChar == ';' %7C%7C fChar =='.'%7C%7CfChar=='?'%7C%7CfChar=='!';%0A%09%7D
 %0A%7D%0A

Remove useless internal code