Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Frequency of words in List of Strings of Strings

I have a list of Strings:

List<String> terms = ["Coding is great", "Search Engines are great", "Google is a nice search engine"]

How do I get the frequency of each word in the list: E.g.{Coding:1, Search:2, Engines:1, engine:1, ....}

Here is my Code:

    Map<String, Integer> wordFreqMap = new HashMap<>(); 
    for (String contextTerm : term.getContexTerms()  ) 
                {
                    String[] wordsArr = contextTerm.split(" ");
                    for (String  word : wordsArr) 
                    {
                        Integer freq = wordFreqMap.get(word); //this line is getting reset every time I goto a new COntexTerm
                        freq = (freq == null) ? 1: ++freq;
                        wordFreqMap.put(word, freq);
                    }
                }
like image 785
VeilEclipse Avatar asked Aug 22 '14 13:08

VeilEclipse


Video Answer


2 Answers

An idiomatic solution with Java 8 streams:

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SplitWordCount
{
    public static void main(String[] args)
    {
        List<String> terms = Arrays.asList(
            "Coding is great",
            "Search Engines are great",
            "Google is a nice search engine");

        Map<String, Integer> result = terms.parallelStream().
            flatMap(s -> Arrays.asList(s.split(" ")).stream()).
            collect(Collectors.toConcurrentMap(
                w -> w.toLowerCase(), w -> 1, Integer::sum));
        System.out.println(result);
    }
}

Note that you may have to think about whether upper/lower case of the strings should play a role. This one onverts the strings to lower case, and uses them as the keys for the final map. The result is then:

{coding=1, a=1, search=2, are=1, engine=1, engines=1, 
     is=2, google=1, great=2, nice=1}
like image 109
Marco13 Avatar answered Oct 06 '22 18:10

Marco13


public static void main(String[] args) {
    String msg="Coding is great search Engines are great Google is a nice search engine";                   
    ArrayList<String> list2 = new ArrayList<>();
    Map map = new HashMap();
    list2.addAll((List)Arrays.asList(msg.split(" ")));
    String n[]=msg.split(" ");
    int f=0;
    for(int i=0;i<n.length;i++){
         f=Collections.frequency(list2, n[i]);
         map.put(n[i],f);
    }     
    System.out.println("values are "+map);
}
like image 20
harish Avatar answered Oct 06 '22 19:10

harish