import CS342.Hwk3.*; import java.net.URL; import java.applet.Applet; import java.util.Hashtable; import java.util.Enumeration; import java.awt.Panel; import java.awt.TextField; import java.awt.TextComponent; import java.awt.Component; import java.awt.Label; import java.awt.TextArea; /** * This class shows how to use the CS342.Hwk3 package (specifically the * classes DefaultDisplay and URLTokenizer) to display a file. This forms * a basis for the third homework task. */ public class Homework3 extends DefaultDisplay { /*----------------------------------------------------------------------*/ /** * The method defines process_input(String) and so makes this a * concrete class that is extending an abstract class. Notice that * we don't need to define either init or handleEvent as they are * taken care of in the DefaultDisplay definition. */ public void process_input(String the_url_name) { URLTokenizer the_url_tokenizer; WordCounter the_word_counter = new WordCounter(); try { the_url_tokenizer = new URLTokenizer(the_base_url, the_url_name); } catch (URLTokenizerException the_exception) { the_text_output.appendText(the_exception.getMessage()); return; } try { for(;;) { String the_word_just_read = the_url_tokenizer.nextToken(); if (the_word_just_read.equals("")) { the_text_output.appendText(" -- have finished reading " + the_url_name + "\n"); break; } the_word_counter.add_word(the_word_just_read); // the_text_output.appendText(the_word_just_read + "\n"); } } catch(URLTokenizerException the_exception) { the_text_output.appendText(the_exception.getMessage()); return; } try { the_text_output.appendText(" -- The top 10 words are:\n"); for (int i = 1 ; i <= 10 ; i++) { the_text_output.appendText(i + ": " + the_word_counter.remove_most_frequent() + "\n"); } } catch(WordCounterException the_exception) { the_text_output.appendText(the_exception.getMessage() + "\n"); } } } /** * This exception is thrown if an attempt is made to remove a word from an * empty WordCounter. */ class WordCounterException extends Exception { /*----------------------------------------------------------------------*/ public WordCounterException() { super(); } /*----------------------------------------------------------------------*/ public WordCounterException(String s) { super(s); } } /** * This class is used to count the number of times each token appears in the text. */ class WordCounter extends Hashtable { /*----------------------------------------------------------------------*/ /** * This method adds a new word to the table, or increments the counter on * an existing word. */ public void add_word(String the_word) { if (containsKey(the_word)) put(the_word, new Integer(((Integer) get(the_word)).intValue() + 1)); else put(the_word, new Integer(1)); } /*----------------------------------------------------------------------*/ /** * This method removes the word with the hightest count from the table, and * returns a string containing the word and its associated count. */ public synchronized String remove_most_frequent() throws WordCounterException { String the_current_key, the_maximum_key = ""; int the_current_value, the_maximum_value = 0; Enumeration all_the_keys = keys(); if (!all_the_keys.hasMoreElements()) throw new WordCounterException("Hmm. There are no more words."); while (all_the_keys.hasMoreElements()) { the_current_key = (String) all_the_keys.nextElement(); the_current_value = ((Integer) get(the_current_key)).intValue(); if (the_current_value > the_maximum_value) { the_maximum_value = the_current_value; the_maximum_key = the_current_key; } } remove(the_maximum_key); return (the_maximum_key + "[" + the_maximum_value + "]"); } }