Click here to Skip to main content
14,869,990 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
class Solution {
  static String[][] wordCountEngine(String document) {
    Hashtable ht=new Hashtable();
    String words[]=document.split(" ");  //split sentence to words
    int largestcount=0;
    String word;
    for(int i=0;i<words.length;i++)
    { word=words[i].toLowerCase();      // ignoring case-sensitive by converting all 
                                        //  to lowercase
    List<character> li=new ArrayList<character>();
    for (char ch:word.tocharArray())
      if(ch>='a' && ch<='z')
        li.add(ch);                   //ignoring special characters like '@'and digits 
                                      //and adding it to list
    String s="";
    for(char ch:li)
      s=s+ch;                        //words without special characters and digits
    if(s.length()<1)
      continue;
    int count=0;
     
       while(document.contains(s))        //while words present in given sentence
       {    
            document=document.substring(document.indexOf(s)+s.length(),document.length()-1);
            count++;
        }
    else 
      count=1;
    if(count>largestcount)
      largestcount=count;
    //ht.put(s,count);  
  }


What I have tried:

After counting the words I'm not able to convert it into a list consisting of word and its occurrences and displaying them according to their descending order of occurrences.

Example
input:
Java
document = "Practice makes perfect. you'll only
                    get Perfect by practice. just practice!"

output:
Java
[ ["practice", "3"], ["perfect", "2"],
          ["makes", "1"], ["youll", "1"], ["only", "1"], 
          ["get", "1"], ["by", "1"], ["just", "1"] ]
Posted
Updated 12-Sep-18 21:43pm
v3

1 solution

Use a SortedMap (Java Platform SE 8 )[^] with the word being the key and the count being the value. If the Map does not contain the word then add it in, and set its value to 1. If the map does contain the key then just increment its value.
   
Comments
Member 13954890 14-Sep-18 10:26am
   
public class stringtowords
{ public static Hashtable<String, Integer> CountW(String s,String ss)
{
Hashtable<String,Integer> ht=new Hashtable();
int count=1;
if(s.contains(ss))
{
if(!ht.containsKey(ss))
{
ht.put(ss,count);
}
else
{ count=count+1;
}
}
return ht;
}
public static void main(String args[])
{
Hashtable<String,Integer> hte=new Hashtable();
String s="hello! my my name@ is pragya";

String wo[]=s.split(" ");

for(String w:wo)
{
System.out.println(w);
}
String word;
for(int i=0;i<wo.length;i++)
{ word=wo[i].toLowerCase();

List<character> li=new ArrayList<character>();
for (char ch:word.toCharArray())
if(ch>='a' && ch<='z')
li.add(ch);
String ss="";
for(char ch:li)
ss=ss+ch;
hte=CountW(s,ss);
System.out.println(hte);
}
}
}
but for each word it's printing 1 only
I'm not getting where i'm going wrong.
Richard MacCutchan 14-Sep-18 10:49am
   
1. You are using a HashTable rather than a SortedMap.
2. You are splitting your words into characters for some reason not mentioned in your question.

All you need to do with a SortedMap is as follows:
- convert the word to all lower case (if necessary).
- if the lst already contains this word then add 1 to the value.
- if the list does not contain the word, then add it as a new entry with a count of 1.
- repeat for all words.
Member 13954890 14-Sep-18 12:57pm
   
sir what's the difference between sortedmap and a hashtable?
Member 13954890 14-Sep-18 13:39pm
   
public class dhshk
{
public static SortedMap<String, Integer> CountW(String s,String ss)
{
SortedMap<String,Integer> ht=new TreeMap<String,Integer>();
int count=1;
if(s.contains(ss))
{
if(!ht.containsKey(ss))
{
ht.put(ss,count);
}
else
{ count=count+1;
}
}
return ht;
}
public static void main(String args[])
{
SortedMap<String,Integer> hte=new TreeMap<String,Integer>();
String s="hello! my my name@ is pragya";

String wo[]=s.split(" ");

for(String w:wo)
{
System.out.println(w);
}
String word;
for(int i=0;i<wo.length;i++)
{
word=wo[i].toLowerCase();

List<character> li=new ArrayList<character>();
for (char ch:word.toCharArray())
if(ch>='a' && ch<='z')
li.add(ch);
String ss="";
for(char ch:li)
ss=ss+ch;
hte=CountW(s,ss);
System.out.println(hte);
}
}
}
Still i'm not getting the answer.
Richard MacCutchan 15-Sep-18 3:00am
   
You are still splitting your words into Lists of characters, for some unknown reason. Your CountW method creates a TreeMap, again for some unknown reason. You seem to be creating objects for the sake of creating them, rather than for any useful reason.
Member 13954890 15-Sep-18 4:47am
   
Because I want my words without any special characters like @ and no. That's why I'm splitting my words into characters and then removing all special characters and no. from it and then again converting it to words.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900