Hello i am doing an small project to find the top "n" words of an text file based on their frequency of occurrence but i am having few problems in my code now its printing the number of occurrences in each line not as whole file and my another problem is if the file have the following words
tall,taller,tallest then the code must find the root word(tall) and print its frequency as 3 but i am confused how to do this so i am posting my code here i request people here to kindly help me to rectify my mistake and help me to learn.
import java.io.*; import java.util.*; class Counter { public static void main(String[] args) throws NullPointerException { System.out.println("Enter the number of words to find its frequency:"); Scanner scan = new Scanner(System.in); int k=scan.nextInt();//Number of top words to be found is stored here Scanner scan1 = new Scanner(System.in); System.out.println("Enter the file path to analyse the words:"); String path=scan1.nextLine();// Directory path will be stored here String files; File folder = new File(path); File[] listOfFiles = folder.listFiles(); BufferedReader br = null; String words[]; String line=""; for(File f:listOfFiles) { if (f.isFile()) { files = f.getName(); if (files.endsWith(".txt") || files.endsWith(".TXT")) { try{ br=new BufferedReader(new FileReader(f)); try{ while((line=br.readLine())!=null){ Map<String, Integer> unique = new TreeMap<String, Integer>(); String wordlist[]=line.split("\\s+"); for (int i=0; i<wordlist.length; i++) { String string=wordlist[i]; unique.put(string,(unique.get(string) == null?1:(unique.get(string)+1))); } System.out.println(unique); } } catch(IOException e){ System.out.println("I am sorry:"+e); } } catch(FileNotFoundException e){ System.out.println("Here i have got"+e.getMessage()); } } } } } }