我有一个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));
答案 0 :(得分:2)
ArrayList<String> list
。代码:
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());
}