George Chetan
George Chetan

Reputation: 33

How can I find the most frequent word in a text?

I have a problem.It seems like if I have an input like this: "Thanks Thanks Thanks car car" The output will be "thanks". If my word starts with an uppercase letter it will print that word with a lowercase letter. What can I add to my solution to solve that problem?

 public class Main {
 public static void main(String[] args) throws IOException {
     String line;
     String[] words = new String[100];
     Map < String, Integer > frequency = new HashMap < > ();
     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
     while ((line = reader.readLine()) != null) {
         line = line.trim();
         if (!line.isEmpty()) {
             words = line.split("\\W+");
             for (String word: words) {
                 String processed = word.toLowerCase();
                 processed = processed.replace(",", "");

                 if (frequency.containsKey(processed)) {
                     frequency.put(processed,
                         frequency.get(processed) + 1);
                 } else {
                     frequency.put(processed, 1);
                 }
             }
         }
     }
     int mostFrequentlyUsed = 0;
     String theWord = null;

     for (String word: frequency.keySet()) {
         Integer theVal = frequency.get(word);
         if (theVal > mostFrequentlyUsed) {
             mostFrequentlyUsed = theVal;
             theWord = word;
         } else if (theVal == mostFrequentlyUsed && word.length() <
             theWord.length()) {
             theWord = word;
             mostFrequentlyUsed = theVal;
         }

     }
     System.out.printf(theWord);
 }

Upvotes: 0

Views: 321

Answers (2)

Banupriya velusamy
Banupriya velusamy

Reputation: 1

Please find the below program which print both upper and lower case based on input.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

 public class Main {
 public static void main(String[] args) throws IOException {

     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
     String[] strArr=reader.readLine().split(" ");
     String result=null;
     int maxCount=0;
     Map<String, Integer> strMap=new HashMap<String, Integer>();
     int count=0;
     for(String s:strArr){
         count=0;
         if(strMap.containsKey(s)){
             count=strMap.get(s);
             strMap.put(s,++count);
         }else{
             strMap.put(s, ++count);
         }
     }  
         //find Maximum

         for(Map.Entry<String, Integer> itr: strMap.entrySet()){

             if(maxCount==0){                
                 maxCount=itr.getValue();
                 result=itr.getKey();                
             }else{

                 if(maxCount < itr.getValue()){                  
                     maxCount=itr.getValue();
                     result=itr.getKey();
                 }
             }   
         }

         // No of occurences with count
         System.out.println("word"+ result+"count"+ maxCount);

         printInLowerOrUpperCare(result);

 }

      public static void printInLowerOrUpperCare(String result){

          if(result.charAt(0) >='a' && result.charAt(0) >= 'z' ){

              System.out.println(result.toUpperCase());
          }else{
              System.out.println(result.toLowerCase());
          }           

      }

 }

Upvotes: 0

Lazycoder-007
Lazycoder-007

Reputation: 1215

To let the code print the most frequent word in the format it was entered and not in lowercase, You can change below line of code.

String processed = word.toLowerCase();

Change it to :

String processed = word;

But then be aware then containsKey() method is case-sensitive and won't consider "Thanks" and 'thanks" as the same word.

Upvotes: 1

Related Questions