Christian
Christian

Reputation: 269

Count how many times pair of words appeared in java

How do I get the pair of words example on the given string

the quick, quick brown , brown fox, fox jumps jumps over, etc...

then count how many times it appeared?

The code below can only count single word.

 import java.util.*;
    import java.util.Map;
    import java.util.HashMap;

    public class Tokenizer

    {
        public static void main(String[] args)
        {
            int index = 0; int tokenCount; int i =0;
            Map<String,Integer> wordCount = new HashMap<String,Integer>();
            Map<Integer,Integer> letterCount = new HashMap<Integer,Integer>();
            String message="The Quick brown fox jumps over the lazy brown dog the quick";

            StringTokenizer string = new StringTokenizer(message);


            tokenCount = string.countTokens();
            System.out.println("Number of tokens = " + tokenCount);
            while (string.hasMoreTokens()) {
                String word = string.nextToken().toLowerCase();
                Integer count = wordCount.get(word);
                Integer lettercount = letterCount.get(word);

                if(count == null) {
                    wordCount.put(word, 1);
                }
                else {
                    wordCount.put(word, count + 1);
                }
            }
            for (String words : wordCount.keySet())
            {System.out.println("Word : " +  words + " has count :" +wordCount.get(words));


            }
            int first ,second;
            first = second = Integer.MIN_VALUE;
            String firstword ="";
            String secondword="";


            for(Map.Entry<String, Integer> entry : wordCount.entrySet())
            {

                int count = entry.getValue();
                String word = entry.getKey();
                if(count>first){
                    second = first;
                    secondword = firstword;
                    first = count;
                    firstword = word;

                }
                else if(count>second && count ==first){
                    second = count;
                    secondword = word;
                }
            }
            System.out.println(firstword + "" + first);
            System.out.println(secondword + " " + second);

            for(i = 0; i < message.length(); i++){
                char c = message.charAt(i);
                if (c != ' ') {

                    int value = letterCount.getOrDefault((int) c, 0);
                    letterCount.put((int) c, value + 1);
                }
            }

            for(int key : letterCount.keySet()) {
                System.out.println((char) key + ": " + letterCount.get(key));
            }
        }

    }

Upvotes: 0

Views: 597

Answers (3)

Cool Tiger
Cool Tiger

Reputation: 1

while (string.hasMoreTokens()) {

      String word = string.nextToken().toLowerCase();

      if (string.hasMoreTokens())
        word += " "+string.nextToken().toLowerCase();

      Integer count = wordCount.get(word);
      Integer lettercount = letterCount.get(word);

      if(count == null) {
        wordCount.put(word,  1);
      }
      else {
        wordCount.put(word,  count + 1);
      }
    }

Upvotes: -1

aksappy
aksappy

Reputation: 3400

OKay, so from the question I understand that you need to check whether a pair of words from a string has to be counted in the whole string. I see your code and felt it to be a lot more complex than required. Please see the below snippet.

  1. Split the source string with space as the delimiter
  2. Concatenate the adjacent strings, with a space delimiting them
  3. Search for the concatenated string in the source string
  4. If not found, add into a Map with key as the word pair and value as 1.
  5. If found, get the value from the map for the word pair and increment and set it back.

    String message = "The Quick brown fox jumps over the lazy brown dog the quick";
    String[] split = message.split(" ");
    Map<String, Integer> map = new HashMap<>();
    int count = 0;
    for (int i = 0; i < split.length - 1; i++) {
        String temp = split[i] + " " + split[i + 1];
        temp = temp.toLowerCase();
        if (message.toLowerCase().contains(temp)) {
            if (map.containsKey(temp))
                map.put(temp, map.get(temp) + 1);
            else
                map.put(temp, 1);
        }
    
    }
    System.out.println(map);
    

Upvotes: 2

Vishal Gajera
Vishal Gajera

Reputation: 4207

Here is you complete main-method code, Let me know if any query.

public static void main(String[] args)
     {

         int index = 0; int tokenCount; int i =0;
         Map<String,Integer> wordCount = new HashMap<String,Integer>();
         Map<Integer,Integer> letterCount = new HashMap<Integer,Integer>();
         String message="The Quick brown fox jumps over the lazy brown dog the quick";

         StringTokenizer string = new StringTokenizer(message);


         tokenCount = string.countTokens();
         System.out.println("Number of tokens = " + tokenCount);

         while (string.hasMoreTokens()) {
             String word = string.nextToken().toLowerCase();
             Integer count = wordCount.get(word);
             Integer lettercount = letterCount.get(word);
             System.out.println("Count : " + count);
             if(count == null) {
                 wordCount.put(word, 1);
             }
             else {
                 wordCount.put(word, count + 1);
             }
         }
         for (String words : wordCount.keySet())
         {
             System.out.println("Word : " +  words + " has count :" +wordCount.get(words));
         }

     }

Upvotes: 0

Related Questions