/*
  James Little
  Sat 17 March, Exercise 4, COSC 241
  Program reads a stream of texts and counts the 
  frequency of the words contained.
*/

package jlittle_ex4;
import java.util.*;
import java.io.*;

public class AppWord {
    
    // data fields
    private static final int MAX =100;
    private static BufferedReader reader =
	new BufferedReader(new InputStreamReader(System.in));// get ready to read
    private static StringTokenizer tokenizedInput = new StringTokenizer(""); // start up the tokenizer
    private static String inputString = ""; // create a new string
    
	private static Vector list = new Vector();
	private static Word w;
	private static int count=0;
	private static int totalwords=0;
    // get last token
    public static String getLast() {  // the last one
	return inputString;
    }

    // count tokens
    public static int countTokens() {
	return tokenizedInput.countTokens();  // tokenize them tokens and tell me how many
    }

    // read new line
    public static String readLine() {
	inputString = null;  // delete input
	tokenizedInput = new StringTokenizer(""); // another string tokenizer

	try {
	    inputString = reader.readLine();     
	} catch (IOException e) {
	    System.out.println("Error: " + e.getMessage());
	    System.exit(-1);  
	}
	tokenizedInput = (inputString != null) ?
	    new StringTokenizer(inputString) :
	    new StringTokenizer("");  // if true, tokenized input =  inputstring, else empty braces

	return inputString; 
    }

    // get next token
    public static String nextToken() {
	try {
	    return tokenizedInput.nextToken();
	} catch (NoSuchElementException e) {
	    System.out.println("Error: no more tokens to get.");
	    System.exit(-1);
	    return "This shouldn't be reached"; // thats for sure.. System should have exited.
	}
    }

    public static void main(String [] args){
	// variables
	
	// start reading input
	while (readLine() != null) {
	   // very local variables
	   int end=countTokens();     // end condition for loop
	   String temp=null;             // string to feed to Word
	   String temp2=null;            // another word if tokenizer is lazy
	   int charvalue=0;              // what value are you?

	   // loop through tokens on line
	   for(int i=0; i<end; i++) {

	       // set onlist to false
	       boolean onlist=false; 
	       if(truth){
		   temp=word2;
		   truth=false;
	       }else{
		   // load next token in lower case
		   temp=nextToken().toLowerCase();
	       }
	       // int offset is the start, endindex the end of word
	       int offset=0;
	       int endindex=temp.length();
	       
	       // go through the word letter by letter
	       for(int x=0; x<endindex; x++){
		   // set a boolean value to keep track of corrections
		   boolean correction=false;
		   // convert the char to a unicode int... since thats what i did last time
		   charvalue=(int) temp.charAt(x);
		   // if tokenizer hasn't done what I wanted
		   if(charvalue==32||charvalue==13||charvalue==10||charvalue==9||charvalue==11){
		       temp2=temp.substring(x+1,endindex);
		       temp=temp.substring(offset,x);
		       endindex=temp.length();
		       setTruth(true);
		       setWord(temp2);
		       correction=false;
		       end=end+1;
		   }
		   // if we don't want the char at x
		   else if((charvalue<48)||(charvalue>57&&charvalue<65)||((charvalue>90)&&(charvalue<97))||charvalue>122){ 
		       if(x==0){
			   temp=temp.substring(x+1,endindex); // cut first character
			   endindex=temp.length();
			   correction=true;
		    
		       }else if(x==endindex){
			   temp= temp.substring(offset,endindex); // cut end character
			   endindex=temp.length(); 
			   correction=true;
		       }else {
			   // cut middle character
			   temp=temp.substring(offset,x)+temp.substring(x+1,endindex);
			   endindex=temp.length(); 
			   correction=true;
		       }//end if
		   }// end if
		   
		   if(correction){
		       x=-1; // start loop again?? as x++ increments after loop
		   }
	       }// end for
    	       // search for word in Word Vector
	       for(int z=0; z<count; z++){
		   w = (Word)list.elementAt(z);
		   if (temp.equals(w.getWord())){
		       w.addOne(); // add one to word count if its there
		       totalwords++;
		       onlist=true;
		   }// end if
	       }// end for loop
	       // only add Strings that are not empty, we don't want them!
	       if((!onlist)&&(temp.length()>0)){ // if onlist is true don't add new word
		   w = new Word(temp);
		   list.addElement(w);
		   totalwords++;
		   count++;
	       }// end if
	   }// end for loop
	   
	} // end while
	
	// display word list
	for(int i=0; i<count; i++){
	    w = (Word)list.elementAt(i);
	    w.writeWord();
	}// end for loop
	
	// program main output
	System.out.println("\n"+totalwords +" words in text.");
	System.out.println(count +" unique words.");
	System.out.println("END OF OUTPUT");
    }// end main
    
    private static boolean truth=false;
    private static String word2=null;
    
    public static void setTruth(boolean same){
	truth=same;
    }

    public static void setWord(String temp2){
	word2=temp2;
    }
    
       
}// end class
