WordFrequency.java.html

  1  import java.util.Map;
  2  import java.util.Scanner;
  3  import java.util.TreeMap;
  4  import java.io.File;
  5  import java.io.FileNotFoundException;
  6  
  7  /**
  8     This program prints the frequencies of all words in "Alice in Wonderland".
  9  */
 10  public class WordFrequency
 11  {
 12     public static void main(String[] args) 
 13        throws FileNotFoundException
 14     {
 15        Map<String, Integer> frequencies = new TreeMap<String, Integer>();
 16        Scanner in = new Scanner(new File("alice30.txt"));
 17        while (in.hasNext())
 18        {
 19           String word = clean(in.next());    
 20  
 21           // Get the old frequency count
 22  
 23           Integer count = frequencies.get(word);
 24  
 25           // If there was none, put 1; otherwise, increment the count
 26  
 27           if (count == null) { count = 1; }
 28           else { count = count + 1; }        
 29  
 30           frequencies.put(word, count);
 31        }
 32  
 33        // Print all words and counts
 34  
 35        for (String key : frequencies.keySet())
 36        {
 37           System.out.printf("%-20s%10d\n", key, frequencies.get(key));
 38        }
 39     }
 40  
 41     /**
 42        Removes characters from a string that are not letters.
 43        @param s a string
 44        @return a string with all the letters from s
 45     */
 46     public static String clean(String s)
 47     {
 48        String r = "";
 49        for (int i = 0; i < s.length(); i++)
 50        {
 51           char c = s.charAt(i);
 52           if (Character.isLetter(c))
 53           {
 54              r = r + c;
 55           }
 56        }
 57        return r.toLowerCase();
 58     }
 59  }