// file: Wfreq5.java

import java.util.*;

/** Find frequencies of words in a given text.
 * @author Jaanus Poial
 * @version 0.6
 * @since 1.5
 */
public class Wfreq5 {

   /** Main method, the text is read from the command line. */
   public static void main (String[] arg) {
      
      // form a table of frequencies
      HashMap <String, Integer> ftable = calcTable (arg);

      // sort keys alphabetically
      String[] oa = ftable.keySet().toArray(new String[0]);
      Arrays.sort (oa);

      // output pairs "word - frequency"
      for (String word : oa) {
         int frequency = ftable.get (word);
         System.out.println (word + " " + frequency);
      }
   } // main

   /** Calculate frequencies.
    * @param t text as an array of strings
    * @return hashtable of "String-Integer"-pairs
    */
   public static HashMap <String, Integer> calcTable (String[] t) {
      HashMap <String, Integer> result = new HashMap <String, Integer>();
      for (String s : t) {
         StringTokenizer st = new StringTokenizer (s);
         while (st.hasMoreTokens()) {
            String word = st.nextToken();
            if (result.containsKey (word)) {
               int k = result.get (word);
               result.put (word, k+1);
            } else {
               result.put (word, 1);
            }
         }
      }
      return result;
   } // calcTable

} // Wfreq5

// end of file

