import java.util.*;
import java.io.*;

/**
 *  This program parses text files that are assumed to contain only correctly spelled
 *  words and creates a "dictionary" of these words by inserting them into a wordList
 *  object and then having them printed out to a file in alphabetical order
 *
 * @author: Sriram Pemmaraju
 * @date: Nov 14th, 2008
 */

public class buildDict{

	public static void main(String args[])
	{
		try
		{
			// This program is set up to read from a file called novels.txt
			BufferedReader in = new BufferedReader(new FileReader("temp.txt"));
			String line;	// a string to store each line of the text
			String word;    // a string to store each word extracted from a line

			// Defining a dictionary to store the words
			wordList dictionary = new wordList(20000);


			// This loop reads all the lines in the file and processes each line
			// separately
			while( (line= in.readLine()) != null )
			{
				word = "";
				// Now I process each character of the line one by one
				for(int i = 0; i < line.length(); i++)
				{
					char ch = line.charAt(i);

					// if the character being processed is a letter, add it to the
					// word currently being constructed
					if(Character.isLetter(ch))
						word = word+ch;

					// If the character being processed is not a letter, then the word
					// we just constructed is complete and needs to be inserted into the 
					// dictionary
					else if(!word.equals(""))
					{
						dictionary.insert(word);
						word = "";
					}

				} // end for-loop
				
				// There might have been a word at the very end of line and this needs to 
				// be flushed out before we begin the next line
				if(!word.equals(""))
				{
					dictionary.insert(word);
					word = "";
				}

			} // end while-loop

			in.close();

			// Print some statistics of the dictionary we constructed
			System.out.println("The size of the hash table is " + dictionary.size());
			System.out.println("The number of words in the hash table is " + dictionary.numberWords());
			System.out.println("The number of probes per insertion table is " + dictionary.averageNumberOfProbes());
			System.out.println("The maximum number of probes over all insertions is " + dictionary.maxNumberOfProbes());
			
			// Prints out the contents of the dictionary in alphabetical order
			dictionary.sortedPrint();
		}
		catch (IOException e) {}

	}

}
