import java.util.*;
import java.io.*;
public class countunique {
private String[] splitter;
private int[] counter;
/*
* @param String - represents the sentence to be parsed
*
*/
public void countWords(String text){
// remove any '\n' characters that may occur
String temp = text.replaceAll("[\\n]", " ");
// replace any grammatical characters and split the String into an array
splitter = temp.replaceAll("[.,?!:;/]", "").split(" ");
// intialize an int array to hold count of each word
counter= new int[splitter.length];
// loop through the sentence
for(int i =0; i< splitter.length; i++){
// hold current word in the sentence in temp variable
temp = splitter[i];
// inner loop to compare current word with those in the sentence
// incrementing the counter of the adjacent int array for each match
for (int k=0; k< splitter.length; k++){
if(temp.equalsIgnoreCase(splitter[k]))
{
counter[k]++;
}
}
}
printResults();
}
private void printResults()
{
// create a HashMap to store unique combination of words and their counter
// the word being the key and the number of occurences is the value
HashMap map = new HashMap();
int cc=0;
// populate the map
for (int i=0; i< splitter.length; i++)
{
map.put(splitter[i].toLowerCase(), counter[i]);
}
// create an iterator on the map keys
Iterator it = map.keySet().iterator();
System.out.println("Word Count");
System.out.println("-----------------------");
// loop for each key
while(it.hasNext())
{
cc++;
String temp =(String)it.next();
// print the word itself
System.out.print(temp);
// add relevant spacing to print consistently
for (int i=0;i< (20 - temp.length());i++)
{
System.out.print(" ");
}
// print the value (i.e. count of each word)
System.out.println(map.get(temp.toString()));
}
System.out.println("Number of unique words in file "+cc);
}
// main method to test the class
public static void main(String[] args){
String f="";
String space=" ";
String str=null;
try {
FileInputStream fstream = new FileInputStream("C:/Documents and Settings/549176/Desktop/test.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((str = br.readLine()) != null) {
f=f.concat(str);
f=f.concat(" ");
//System.out.println(str);
}
in.close();
} catch (Exception e) {
System.err.println(e);
}
countunique wc = new countunique();
wc.countWords(f);
}
}
import java.io.*;
import java.util.*;
public class CountWordOccurrence {
public static void main(String[] args){
try{
BufferedReader br=new BufferedReader(new FileReader("c:/data.txt"));
String str="";
String st;
while((st=br.readLine())!=null){
str+=st+" ";
}
HashMap<String, Integer> map = new HashMap<String, Integer>();
str = str.toLowerCase();
int count = -1;
for (int i = 0; i < str.length(); i++) {
if ((!Character.isLetter(str.charAt(i))) || (i + 1 == str.length())) {
if (i - count > 1) {
if (Character.isLetter(str.charAt(i)))
i++;
String word = str.substring(count + 1, i);
if (map.containsKey(word)) {
map.put(word, map.get(word) + 1);
}
else {
map.put(word, 1);
}
}
count = i;
}
}
ArrayList<Integer> list = new ArrayList<Integer>();
list.addAll(map.values());
Collections.sort(list, Collections.reverseOrder());
int last = -1;
for (Integer i : list) {
if (last == i)
continue;
last = i;
for (String s : map.keySet()) {
if (map.get(s) == i)
System.out.println(s + ":" + i);
}
}
}
catch(Exception e){
System.out.println(e);
}
}
}