从用户输入中找到最常见的词

Find the most common word from user input

我对 Java 创建软件应用程序还很陌生,该软件应用程序允许用户在字段中输入文本,程序会遍历所有文本并识别最常用的词是什么。目前,我的代码如下所示:

JButton btnMostFrequentWord = new JButton("Most Frequent Word");
btnMostFrequentWord.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    String text = textArea.getText();
    String[] words = text.split("\s+");
    HashMap<String, Integer> occurrences = new HashMap<String, Integer>();
    for (String word : words) {
      int value = 0;
      if  (occurrences.containsKey(word)) {
        value = occurrences.get(word);
      }
      occurrences.put(word, value + 1);
    }

    JOptionPane.showMessageDialog(null, "Most Frequent Word: " + occurrences.values());
  }
}

这只是打印单词的值,但我希望它能告诉我排名第一的最常见单词是什么。非常感谢任何帮助。

您可以遍历出现映射并找到最大值或 像下面这样尝试

String text = textArea.getText();;
String[] words = text.split("\s+");
HashMap<String, Integer> occurrences = new HashMap<>();
int mostFreq = -1;
String mostFreqWord = null;

for (String word : words) {
    int value = 0;
    if (occurrences.containsKey(word)) {
        value = occurrences.get(word);
    }
    value = value + 1;
    occurrences.put(word, value);

    if (value > mostFreq) {
        mostFreq = value;
        mostFreqWord = word;
    }
}

JOptionPane.showMessageDialog(null, "Most Frequent Word: " + mostFreqWord);

就在 for 循环之后,您可以按值对映射进行排序,然后按值反转排序的条目,select 第一个。

for (String word: words) {
    int value = 0;
    if  (occurrences.containsKey(word)) {
        value = occurrences.get(word);
    }
    occurrences.put(word, value + 1);
}

Map.Entry<String,Integer> tempResult = occurrences.entrySet().stream()
                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                .findFirst().get();
JOptionPane.showMessageDialog(null, "Most Frequent Word: " + tempResult.getKey());

我会做这样的事情

int max = 0;
String a = null;
for (String word : words) {
    int value = 0;
    if(occurrences.containsKey(word)){
        value = occurrences.get(word);
    }
    occurrences.put(word, value + 1);
    if(max < value+1){
        max = value+1;
        a = word;
    }
}
System.out.println(a);

你可以对它进行排序,解决方案会更短,但我认为它运行得更快。

对于更熟悉 Java 的人来说,这里有一个使用 Java 8 的非常简单的方法:

List<String> words = Arrays.asList(text.split("\s+"));

Collections.sort(words, Comparator.comparingInt(word -> {
    return Collections.frequency(words, word);
}).reversed());

最常见的单词排序后存储在words.get(0)中。