Hi everyone,I am assigned with an task of writing an java code to create an Word-Frequency-Counter which needs to satisfy the following constraints:
1)It must prompt the user to enter an path from where the code will read all the contents of text files(.txt) present in that directory.
2)An property file named stop.txt has to be read by the code which will have list of words that has to be excluded from the frequency count Example:If stop.txt have the word "is,that,or"then the count of "is,that,or" has to be skipped.
3)All the identical words has to be chopped down to root word,Example:If the file have words like tall,taller,tallest then the count of the word tall must be 3 not 1.
4)The user must be prompted to enter an number and that number of words has to be displayed Example:If an user enters 2 then the top 2 words(Based on the Frequency) has to be displayed:
Here is the code which i have made so far:
/**
*This code is to create an Word-Frequency Counter
*@author Dinesh
*@version 0.1
*/
import java.io.*;
import java.util.*;
class FrequencyCounter {
public static void main(String[] args) {
System.out.println("Enter the file path to analyse:");
Scanner scan = new Scanner(System.in);
String path = scan.nextLine();//Files present in this path will be analysed to count frequency
File directory = new File(path);
File[] listOfFiles = directory.listFiles();//To get the list of file-names found at the "directoy"
BufferedReader br = null;
String words[] = null;
String line;
String files;
Map<String, Integer> wordCount = new HashMap<String, Integer>(); //Creates an Hash Map for storing the words and its count
for (File file : listOfFiles) {
if (file.isFile()) {
files = file.getName();
try {
if (files.endsWith(".txt") || files.endsWith(".TXT")) { //Checks whether an file is an text file
br = new BufferedReader(new FileReader(files)); //creates an Buffered Reader to read the contents of the file
while ((line = br.readLine()) != null) {
line = line.toLowerCase();
words = line.split("\\s+"); //Splits the words with "space" as an delimeter
}
br.close();
}
for (String read : words) {
Integer freq = wordCount.get(read);
wordCount.put(read, (freq == null) ? 1 : freq + 1); //For Each word the count will be incremented in the Hashmap
}
} catch (NullPointerException | IOException e) {
System.out.println("I could'nt read your files:" + e);
}
}
System.out.println(wordCount.size() + " distinct words:"); //Prints the Number of Distinct words found in the files read
System.out.println(wordCount); //Prints the Word and its occurrence
}
}
}
When i tried running this code i have got the output as follows:
Enter the file path to analyse:
/home/dinesh/Desktop
0 distinct words:
{}
I could'nt read your files:java.lang.NullPointerException
0 distinct words:
{}
5 distinct words:
{hello=1, are=1, how=1, you=1, hi=1}
5 distinct words:
{hello=2, are=2, how=2, you=2, hi=2}
5 distinct words:
{hello=3, are=3, how=3, you=3, hi=3}
5 distinct words:
{hello=4, are=4, how=4, you=4, hi=4}
5 distinct words:
{hello=5, are=5, how=5, you=5, hi=5}
5 distinct words:
{hello=6, are=6, how=6, you=6, hi=6}
5 distinct words:
{hello=7, are=7, how=7, you=7, hi=7}
5 distinct words:
{hello=8, are=8, how=8, you=8, hi=8}
6 distinct words:
{=1, hello=8, are=8, how=8, you=8, hi=8}
6 distinct words:
{=1, hello=8, are=8, how=8, you=8, hi=8}
6 distinct words:
{=2, hello=8, are=8, how=8, you=8, hi=8}
6 distinct words:
{=3, hello=8, are=8, how=8, you=8, hi=8}
6 distinct words:
{=4, hello=8, are=8, how=8, you=8, hi=8}
6 distinct words:
{=4, hello=8, are=8, how=8, you=8, hi=8}
6 distinct words:
{=5, hello=8, are=8, how=8, you=8, hi=8}
6 distinct words:
{=6, hello=8, are=8, how=8, you=8, hi=8}
6 distinct words:
{=7, hello=8, are=8, how=8, you=8, hi=8}
6 distinct words:
{=8, hello=8, are=8, how=8, you=8, hi=8}
6 distinct words:
{=9, hello=8, are=8, how=8, you=8, hi=8}
8 distinct words:
{=9, hello=8, dine=2, dinesh=1, are=8, how=8, you=8, hi=8}
8 distinct words:
{=9, hello=8, dine=4, dinesh=2, are=8, how=8, you=8, hi=8}
8 distinct words:
{=9, hello=8, dine=6, dinesh=3, are=8, how=8, you=8, hi=8}
But this is not the expected form of output needed and i am confused about implementing the constraints 2,3 and 4 kindly help/guide me to complete this assignment.Thanks in advance! `