如何计算存储在arraylists中的每个句子中每个单词的出现次数?

时间:2016-08-03 18:29:16

标签: java arraylist hashmap word-count

我有一个arraylist来保存文档的每一行 - 例如

list.add("I like to play pool")

list.add("How far can you run")

list.add("Do you like fanta because I like fanta")

我希望能够查看存储在arrayList中的每个句子并计算每个句子中每个单词的出现次数,有人可以帮助我吗?

EDIT 这是我试过的,但它只告诉我每个句子的发生。我需要它能够计算每个句子的单词。

Set<String> unique = new HashSet<String>(list);
        for (String key : unique) {
            System.out.println(key + ": " + Collections.frequency(list, key));

1 个答案:

答案 0 :(得分:2)

  1. 请致电ArrayList<String> list
  2. 让我们创建一个String []列表2 3,将句子拆分为数组。
  3. 计算出现次数
  4. 代码:

    ArrayList<String> list = new ArrayList<>();
    //add sentences here
    list.add("My first sentence sentence");
    list.add("My second sentence1 sentence1");
    
    ArrayList<String[]> list2 = new ArrayList<>();
    for (String s : list) { list2.add(s.split(" "));};
    for (String[] s : list2) {
        Map<String, Integer> wordCounts = new HashMap<String, Integer>();
    
        for (String word : s) {
            Integer count = wordCounts.get(word);
            if (count == null) {
                count = 0;
            }
            wordCounts.put(word, count + 1);
        }
        for (String key : wordCounts.keySet()) {
            System.out.println(key + ": " + wordCounts.get(key).toString());
    }