import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;

public class Test {

    public static WordCount processFile (String filename)
	throws FileNotFoundException, IOException {

	// open the file and create a BufferedReader for it
	FileReader fileReader = new FileReader (filename);
	BufferedReader in = new BufferedReader (fileReader);

	// use the BufferedReader to get lines from the file
	// and pass each to the WordCount

	WordCount wc = new WordCount ();
	while (true) {
	    String s = in.readLine();
	    if (s == null) break;
	    wc.processLine (s);
	}
	return wc;
    }
    
    public static void main (String[] args)
	throws FileNotFoundException, IOException {

	WordCount wc = processFile ("text.txt");

	System.out.println (wc.count + " words");
	System.out.println ("enumeration appears " + 
			    wc.ht.get ("enumeration") +
			    " times");
	System.out.println (wc.ht.size() + " different words");
	
	Heap heap = new Heap ();
	Enumeration enum = wc.ht.keys ();
	while (enum.hasMoreElements ()) {
	    String key = (String) enum.nextElement ();
	    Integer value = (Integer) wc.ht.get (key);
	    heap.insert (new Pair (key, value));
	}

	int i = 1;
	while (!heap.empty ()) {
	    Pair p = (Pair) heap.remove ();
	    System.out.print (i + "\t");
	    i++;
	    p.print ();
	    if (i > 20) break;
	}
    }
}

/*

77344 words
enumeration appears 6 times
4069 different words
1       { the, 5219 }
2       { a, 2145 }
3       { to, 2037 }
4       { of, 2015 }
5       { is, 1920 }
6       { and, 1369 }
7       { that, 1332 }
8       { in, 1116 }
9       { you, 1048 }
10      { it, 968 }
11      { we, 880 }
12      { verbatim, 861 }
13      { this, 697 }
14      { for, 688 }
15      { are, 681 }
16      { an, 653 }
17      { if, 544 }
18      { as, 538 }
19      { method, 501 }
20      { can, 442 }

*/
