【问题标题】:remove repeated words from String Array从字符串数组中删除重复的单词
【发布时间】:2016-03-10 13:40:58
【问题描述】:

早安

我写了一个函数来计算一个词的频率:

public static int tfCalculator(String[] totalterms, String termToCheck) {
    int count = 0;  //to count the overall occurrence of the term termToCheck
    for (String s : totalterms) {
        if (s.equalsIgnoreCase(termToCheck)) {
            count++; 
        }
    } 
    return count;
}

然后我在下面的代码中使用它来计算String[] words中的每个单词

for(String word:words){
    int freq = tfCalculator(words, word);

    System.out.println(word + "|" + freq);
    mm+=word + "|" + freq+"\n";
}

我遇到的问题是这里重复的单词是例如结果:

  • 细胞骨架|2
  • 网络|1
  • 启用|1
  • 等于|1
  • 主轴|1
  • 细胞骨架|2
  • ...
  • ...

所以有人可以帮我删除重复的单词并得到这样的结果:

  • 细胞骨架|2
  • 网络|1
  • 启用|1
  • 等于|1
  • 主轴|1
  • ...
  • ...

非常感谢!

【问题讨论】:

  • 将数组放入Set,重复项就消失了
  • @KevinEsche 不好计算频率。我会使用Map<Integer, String> 将单词映射到它们的频率。话又说回来,有很多更好的方法来计算频率本身,但那是另一回事了。
  • 能否请您发布完整的程序?
  • 关于代码质量的旁注:您的一些变量(如 mm)或方法的名称......非常糟糕。如果你给你的方法起一个名字来说明它的真正作用;事情变得清晰得多。喜欢:int countOccurancesOfTerm(String term, String[] stringsToCheck) 或类似的东西。
  • @Mena 谢谢你的回答,你能解释一下我如何使用Map<Integer, String>,或者另一种计算词频的方法

标签: java arrays text


【解决方案1】:

Java 8 解决方案

words = Arrays.stream(words).distinct().toArray(String[]::new);

distinct 方法删除重复项。 words 被替换为没有重复的新数组

【讨论】:

    【解决方案2】:

    我想在这里你想打印数组 totalterms 中每个字符串的频率。我认为使用 Map 是一个更简单的解决方案,因为在数组的单次遍历中它将存储所有字符串的频率检查以下实现。

    public static void printFrequency(String[] totalterms)
    {
        Map frequencyMap = new HashMap<String, Integer>();
    
        for (String string : totalterms) {
            if(frequencyMap.containsKey(string))
            {
                Integer count = (Integer)frequencyMap.get(string);
                frequencyMap.put(string, count+1);
            }
            else
            {
                frequencyMap.put(string, 1);
            }
        }
    
        Set <Entry<String, Integer>> elements= frequencyMap.entrySet();
    
        for (Entry<String, Integer> entry : elements) {
            System.out.println(entry.getKey()+"|"+entry.getValue());
        }
    }
    

    【讨论】:

    • 谢谢@Saurav,这对我来说很好,我真的很感激
    • 这是非常冗长和不必要的......最好检查一些其他解决方案@HaKiM's
    【解决方案3】:

    您可以只使用HashSet,这应该可以解决重复问题:

    words = new HashSet<String>(Arrays.asList(words)).toArray(new String[0]);
    

    这将获取您的数组,将其转换为List,将其提供给HashSet&lt;String&gt; 的构造函数,然后为您将其转换回数组。

    【讨论】:

      【解决方案4】:

      对数组进行排序,然后你可以只计算相等的相邻元素:

      Arrays.sort(totalterms);
      int i = 0;
      while (i < totalterms.length) {
        int start = i;
        while (i < totalterms.length && totalterms[i].equals(totalterms[start])) {
          ++i;
        }
        System.out.println(totalterms[start] + "|" + (i - start));
      }
      

      【讨论】:

        【解决方案5】:

        两行:


        String s = "细胞骨架|2 - 网络|1 - 启用|1 - 等于|1 - 主轴|1 - 细胞骨架|2"; System.out.println(new LinkedHashSet(Arrays.asList(s.split("-"))).toString().replaceAll("(^\[|\]$)", "").replace(", ", "-"));

        【讨论】:

          【解决方案6】:

          您的代码很好,您只需要跟踪已经遇到过哪些单词。为此,您可以保持运行集:

          Set<String> prevWords = new HashSet<>();
          for(String word:words){
              // proceed if word is new to the set, otherwise skip
              if (prevWords.add(word)) {
                  int freq = tfCalculator(words, word);
          
                  System.out.println(word + "|" + freq);
                  mm+=word + "|" + freq+"\n";
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2012-03-14
            • 2013-08-11
            • 1970-01-01
            • 1970-01-01
            • 2019-11-29
            • 1970-01-01
            • 2014-12-16
            相关资源
            最近更新 更多