Results 1 to 4 of 4
- 11-08-2010, 09:42 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
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 + " }");
}
}
}
- 11-08-2010, 09:48 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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?
- 11-08-2010, 10:08 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Thank you for replying. processLine() breaks the String into words and then passes each word to processWord. Thx.
- 11-09-2010, 12:10 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
Finding a word in a sentence?
By blackrabbit in forum New To JavaReplies: 6Last Post: 07-22-2010, 11:07 PM -
Count same word from many file in directory
By cassiests in forum New To JavaReplies: 4Last Post: 05-20-2010, 09:21 AM -
please help with counting specific word in a sentence
By noobinoo in forum New To JavaReplies: 4Last Post: 05-07-2010, 02:06 PM -
count occurence of word in a line of text
By sinyi88 in forum New To JavaReplies: 19Last Post: 02-28-2009, 07:37 AM -
Word Frequency
By capu in forum Advanced JavaReplies: 2Last Post: 10-09-2008, 02:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks