package document;

/** 
 * A class that represents a text document
 * @author UC San Diego Intermediate Programming MOOC team
 */
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class Document {

	private String text;
	
	/** Create a new document from the given text.
	 * Because this class is abstract, this is used only from subclasses.
	 * @param text The text of the document.
	 */
	protected Document(String text)
	{
		this.text = text;
	}
	
	/** Returns the tokens that match the regex pattern from the document 
	 * text string.
	 * @param pattern A regular expression string specifying the 
	 *   token pattern desired
	 * @return A List of tokens from the document text that match the regex 
	 *   pattern
	 */
	protected List<String> getTokens(String pattern)
	{
		ArrayList<String> tokens = new ArrayList<String>();
		Pattern tokSplitter = Pattern.compile(pattern);
		Matcher m = tokSplitter.matcher(text);
		
		while (m.find()) {
			tokens.add(m.group());
		}
		
		return tokens;
	}
	
	// This is a helper function that returns the number of syllables
	// in a word.  You should write this and use it in your 
	// BasicDocument class.
	// You will probably NOT need to add a countWords or a countSentences method
	// here.  The reason we put countSyllables here because we'll use it again
	// next week when we implement the EfficientDocument class.
	protected int countSyllables(String word)
	{
		// TODO: Implement this method so that you can call it from the 
	    // getNumSyllables method in BasicDocument (module 1) and 
	    // EfficientDocument (module 2).
		boolean vowel = false;
		int syllables = 0;
		char [] letters = word.toLowerCase().toCharArray();
		for (int i = 0 ; i < letters.length; i++ ) {
			switch(letters[i]) {  
				case 'a': case 'i': case 'o': case 'u': case 'y':
					if (!vowel) {
							vowel = true;
							syllables += 1;
						}
					break;
				case 'e':
					if (!vowel && i != letters.length - 1 ) {
						vowel = true;
						syllables += 1;
					}
					break;
				default:
					vowel = false;
			}
		}
		if (syllables == 0) {
			syllables = 1;
		}
		return syllables;
	}
	
	/** A method for testing
	 * 
	 * @param doc The Document object to test
	 * @param syllables The expected number of syllables
	 * @param words The expected number of words
	 * @param sentences The expected number of sentences
	 * @return true if the test case passed.  False otherwise.
	 */
	public static boolean testCase(Document doc, int syllables, int words, int sentences)
	{
		System.out.println("Testing text: ");
		System.out.print(doc.getText() + "\n....");
		boolean passed = true;
		int syllFound = doc.getNumSyllables();
		int wordsFound = doc.getNumWords();
		int sentFound = doc.getNumSentences();
		if (syllFound != syllables) {
			System.out.println("\nIncorrect number of syllables.  Found " + syllFound 
					+ ", expected " + syllables);
			passed = false;
		}
		if (wordsFound != words) {
			System.out.println("\nIncorrect number of words.  Found " + wordsFound 
					+ ", expected " + words);
			passed = false;
		}
		if (sentFound != sentences) {
			System.out.println("\nIncorrect number of sentences.  Found " + sentFound 
					+ ", expected " + sentences);
			passed = false;
		}
		
		if (passed) {
			System.out.println("passed.\n");
		}
		else {
			System.out.println("FAILED.\n");
		}
		return passed;
	}
	
	
	/** Return the number of words in this document */
	public abstract int getNumWords();
	
	/** Return the number of sentences in this document */
	public abstract int getNumSentences();
	
	/** Return the number of syllables in this document */
	public abstract int getNumSyllables();
	
	/** Return the entire text of this document */
	public String getText()
	{
		return this.text;
	}
	
	/** return the Flesch readability score of this document */
	public double getFleschScore()
	{
	    // 
		int words = getNumWords();
		int syllables = getNumSyllables();
		int sentences = getNumSentences();
	    return 208.635 - (1.015 * (words/sentences)) - (84.6 * (syllables/words));
	}
	
	
	
}

@@ -14,16 +14,49 @@
 ent;%0D%0A%0D%0A
+import java.text.DecimalFormat;%0D%0A
 /** %0D%0A *
@@ -4218,18 +4218,74 @@
 ;%0D%0A%09
-    return
+%09DecimalFormat df = new DecimalFormat(%22###.#%22);%0D%0A%09%09double flesch =
  208
@@ -4348,16 +4348,69 @@
 words));
+ %0D%0A%09    return Double.parseDouble(df.format(flesch));
 %0D%0A%09%7D%0D%0A%09%0D

update flesch to return sigle decimal precision