gpt4 book ai didi

java - 字符串频率搜索未找到所有单词

转载 作者:行者123 更新时间:2023-12-02 01:47:46 25 4
gpt4 key购买 nike

我正在尝试实现一个字符串频率搜索算法,该算法解析jokes.txt 文件并获取测试中每个唯一单词出现的次数。该算法应考虑区分大小写并使“a”和“A”都是唯一的。截至目前,该算法似乎跳过了测试中第一次出现的“a”以及随后出现的许多其他单词。

此外,words 数组包含文本中的每个单词。不知何故,(!isDuplicate) 条件中的循环会跳过“a”,并且不会增加计数

笑话.txt

I wondered why the baseball was getting bigger.
Then it hit me.

Police were called to a day care
where a 3-yr-old was resisting a rest.
...

WordCounter.java

import java.util.*;
import java.io.FileNotFoundException;
import java.io.FileInputStream;

public class WordCounter {
ArrayList<String> words = new ArrayList<String>();

//prints number of words in the file
public void numOfWords(Scanner key1) {
int counter = 1;
while(key1.hasNext()) {
words.add(key1.next().replaceAll("[^a-zA-Z]", ""));

}
}

//Takes word as parameter and returns frequency of that word
public void frequencyCounter(Scanner key1) {
ArrayList <String> freqWords = new ArrayList<String>();
int count = 1;
int counter = 1;

for(int i = 0; i < words.size(); i++){
boolean isDuplicate = false;
for (String s: freqWords){
if (s.contains(words.get(i).trim()))
isDuplicate =true;
}

if (!isDuplicate){

for(int j = i + 1; j < words.size(); j++){
if(words.get(i).equals(words.get(j))){
count++;
}
}
freqWords.add(count + "-" + words.get(i));
Collections.sort(freqWords, Collections.reverseOrder());
count = 1;
}
}

for(int i = 0; i < freqWords.size(); i++) {
System.out.print((i+1) + " ");
System.out.println(freqWords.get(i));
}
}

}

最佳答案

您确定重复项的逻辑有点不正确:

        boolean isDuplicate = false;
for (String s: freqWords){
if (s.contains(words.get(i).trim()))
isDuplicate =true;
}

如果words.get(i) 是“a”并且s 是“apple”,因为apple 包含“a”,这将使isDuplicate 为true。检查s中的单词是否与words.get(i)完全匹配。

关于java - 字符串频率搜索未找到所有单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53509753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com