【问题标题】:Find the most common word from user input从用户输入中查找最常用的词
【发布时间】:2017-10-13 12:45:34
【问题描述】:

我对 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());
  }
}

这只是打印单词的值,但我希望它告诉我最常见的单词是什么。任何帮助将不胜感激。

【问题讨论】:

    标签: java arrays sorting hashmap windowbuilder


    【解决方案1】:

    for 循环之后,您可以按值对地图进行排序,然后按值反转排序的条目并选择第一个。

    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());
    

    【讨论】:

      【解决方案2】:

      对于更熟悉 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)中。

      【讨论】:

        【解决方案3】:

        我会做这样的事情

        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);
        

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

        【讨论】:

          【解决方案4】:

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

          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);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-09-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-04-28
            相关资源
            最近更新 更多