Results 1 to 4 of 4
  1. #1
    bMorgan is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default Count the frequency of the word in a text file instead of a sentence.

    Hi everyone. I want to count the frequency of word that appears in text file through hash table, but i only know how to count the frequency of different words in a sentence. The problem i have is through Filereader i can read the text file line by line, but because the same word appears in different sentences in the text file. How to add the frequency of the same word together?

    The following is the code i use to calculate the frequency of different words in a sentence. Thank you.


    import java.util.Hashtable;
    import java.util.StringTokenizer;
    import java.util.Enumeration;

    public class Main{

    public static void main(String[] args)
    {

    WordCount wc = new WordCount();
    wc.processLine("da do ro ro ro, da do ro ro");
    wc.print ();
    }
    }

    import java.util.Hashtable;
    import java.util.StringTokenizer;
    import java.util.Enumeration;


    public class WordCount
    {
    Hashtable ht;
    public WordCount ()
    {
    ht = new Hashtable ();
    }

    public void processLine (String s)
    {
    StringTokenizer st = new StringTokenizer (s, " ,.");
    while (st.hasMoreTokens()) {
    String word = st.nextToken();
    processWord (word.toLowerCase ());
    }
    }

    public void processWord (String word)
    {
    if (ht.containsKey (word)) {
    Integer i = (Integer) ht.get (word);
    Integer j = new Integer (i.intValue() + 1);
    ht.put(word, j);
    }
    else {
    ht.put(word, new Integer (1));
    }
    }

    public void print ()
    {
    Enumeration enum1 = ht.keys ();
    while (enum1.hasMoreElements ()) {
    String key = (String) enum1.nextElement ();
    Integer value = (Integer) ht.get (key);
    System.out.println ("{ " + key + ", " + value + " }");
    }
    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I see you create an instance of WordCount which contains a map (hashtable) used for counting, and then call processLine(). What does the processLine() method do? Specifically, what is the effect of processLine() on the map that stores the counts?

  3. #3
    bMorgan is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Thank you for replying. processLine() breaks the String into words and then passes each word to processWord. Thx.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Yes. And when processLine() has done its stuff, what is the effect on your count map (the hashtable)?

    When processLine() starts you have some counts and when it has finished those counts have been changed in some way. The intent of my question was for you to describe that change.

Similar Threads

  1. Finding a word in a sentence?
    By blackrabbit in forum New To Java
    Replies: 6
    Last Post: 07-23-2010, 12:07 AM
  2. Count same word from many file in directory
    By cassiests in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 10:21 AM
  3. Replies: 4
    Last Post: 05-07-2010, 03:06 PM
  4. count occurence of word in a line of text
    By sinyi88 in forum New To Java
    Replies: 19
    Last Post: 02-28-2009, 08:37 AM
  5. Word Frequency
    By capu in forum Advanced Java
    Replies: 2
    Last Post: 10-09-2008, 03:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •