pls help me with word occurances program
i have a program which reads a file and displays the number of occurances of each word in the file and also sorts it occording to number of occurances
i want to get only top 10 words from the file pls help me with the code
the program is:
Code Java:
import java.io.*;
import java.util.*;
public class HashMapEx {
public static void main(String[] args) {
LinkedHashMap<String, Integer> wordcount =
new LinkedHashMap<String, Integer>();
try {
BufferedReader in = new BufferedReader(
new FileReader("c:\\test\\sample.txt"));
String str;
while ((str = in.readLine()) != null) {
str = str.toLowerCase(); // convert to lower case
int idx1 = -1;
for (int i = 0; i < str.length(); i++) {
if ((!Character.isLetter(str.charAt(i))) ||
(i + 1 == str.length())) {
if (i - idx1 > 1) {
if (Character.isLetter(str.charAt(i)))
i++;
String word = str.substring(idx1 + 1, i);
if (wordcount.containsKey(word)) {
wordcount.put(word, wordcount.get(word) + 1);
} else {
wordcount.put(word, 1);
}
}
idx1 = i;
}
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
ArrayList<Integer> values = new ArrayList<Integer>();
values.addAll(wordcount.values());
Collections.sort(values, Collections.reverseOrder());
int last_i = -1;
for (Integer i : values) {
if (last_i == i) // without duplicates
continue;
last_i = i;
for (String s : wordcount.keySet()) {
if (wordcount.get(s) == i) // which have this value
System.out.println(s + ":" + i);
}
}
}
}
Re: pls help me with word occurances program
Quote:
i want to get only top 10 words from the file
Do you have an algorithm for finding the top words used?
How are you counting the words and saving those counts?
Once you have saved the counts then if you sorted them highest count first, you could then pick off the number that you wanted from the top of the list
Re: pls help me with word occurances program