package bschecker.bluesheets;

import bschecker.util.Error;
import bschecker.util.ErrorList;
import bschecker.util.Tools;
import bschecker.util.UtilityMethods;
import opennlp.tools.parser.Parse;

/**
 * Finds verbs in the past tense. (1)
 * @author Leo
 * @author tedpyne
 * @author JeremiahDeGreeff
 */
public class PastTense extends Bluesheet {
	
	private static final String[] TO_HAVE_CONJ = {"have", "has", "had", "having"};
	
	
	/**
	 * Finds all instances of past tense in a paragraph.
	 * @param line the paragraph in which to find errors
	 * @param parses a Parse array of each sentence of the line
	 * @return an ErrorList which for each Error references start token, end token, and, optionally, a note
	 */
	@Override
	protected ErrorList findErrors(String line, Parse[] parses) {
		ErrorList errors = new ErrorList(line);
		int tokenOffset = 0;
		for(int i = 0; i < parses.length; i++) {
			String sentence = parses[i].getText();
			String[] tokens = Tools.getTokenizer().tokenize(sentence);
			String[] tags = Tools.getPOSTagger().tag(tokens);
			ErrorList sentenceErrors = new ErrorList(sentence);
			boolean inQuote = false, inIntroducedQuote = false;
			for(int j = 0; j < tags.length; j++) {
				if(tokens[j].contains("\"")) {
					inIntroducedQuote = !inQuote && j > 0 && (tokens[j - 1].equals(",") || tokens[j - 1].equals(":"));
					inQuote = !inQuote;
				}
				if(!inIntroducedQuote && tags[j].equals("VBD"))
					sentenceErrors.add(new Error(j));
				if(!inIntroducedQuote && tags[j].equals("VBN") && j > 0 && UtilityMethods.arrayContains(TO_HAVE_CONJ, tokens[j - 1]))
					sentenceErrors.add(new Error(j - 1, j)); //does not currently look past intermediary adverbs
			}
			for(int j = 0; j < sentenceErrors.size(); j++)
				if(!UtilityMethods.parseHasParent(UtilityMethods.getParseAtToken(parses[i], sentenceErrors.get(j).getEndIndex()), "SBAR"))
					errors.add(new Error(sentenceErrors.get(j).getStartIndex() + tokenOffset, sentenceErrors.get(j).getEndIndex() + tokenOffset));
			tokenOffset += tokens.length;
		}
		return errors;
	}
	
}
@@ -859,41 +859,28 @@
 for(
-int i = 0; i %3C parses.length; i++
+Parse parse : parses
 ) %7B%0A
@@ -905,20 +905,16 @@
  = parse
-s%5Bi%5D
 .getText
@@ -1153,24 +1153,24 @@
 for(int 
-j
+i
  = 0; 
-j
+i
  %3C tags.
@@ -1177,17 +1177,17 @@
 length; 
-j
+i
 ++) %7B%0A%09%09
@@ -1198,17 +1198,17 @@
 (tokens%5B
-j
+i
 %5D.contai
@@ -1252,25 +1252,25 @@
 !inQuote && 
-j
+i
  %3E 0 && (tok
@@ -1269,25 +1269,25 @@
  && (tokens%5B
-j
+i
  - 1%5D.equals
@@ -1298,25 +1298,25 @@
 ) %7C%7C tokens%5B
-j
+i
  - 1%5D.equals
@@ -1380,33 +1380,33 @@
 edQuote && tags%5B
-j
+i
 %5D.equals(%22VBD%22))
@@ -1440,17 +1440,17 @@
 w Error(
-j
+i
 ));%0A%09%09%09%09
@@ -1479,17 +1479,17 @@
 && tags%5B
-j
+i
 %5D.equals
@@ -1499,17 +1499,17 @@
 BN%22) && 
-j
+i
  %3E 0 && 
@@ -1558,17 +1558,17 @@
  tokens%5B
-j
+i
  - 1%5D))%0A
@@ -1605,16 +1605,16 @@
 ror(
-j
+i
  - 1, 
-j
+i
 )); 
@@ -1685,16 +1685,16 @@
 int 
-j
+i
  = 0; 
-j
+i
  %3C s
@@ -1715,17 +1715,17 @@
 size(); 
-j
+i
 ++)%0A%09%09%09%09
@@ -1794,20 +1794,16 @@
 en(parse
-s%5Bi%5D
 , senten
@@ -1807,33 +1807,33 @@
 tenceErrors.get(
-j
+i
 ).getEndIndex())
@@ -1884,25 +1884,25 @@
 eErrors.get(
-j
+i
 ).getStartIn
@@ -1945,9 +1945,9 @@
 get(
-j
+i
 ).ge

clean up past tense