我如何计算一行中单词的出现次数

时间:2014-03-10 16:00:25

标签: java frequency words

我对java很新。我想计算特定行中单词的出现次数。到目前为止,我只能计算单词,但不知道如何计算出现次数。

有一种简单的方法吗?

Scanner file = new Scanner(new FileInputStream("/../output.txt"));
int count = 0;
  while (file.hasNextLine()) {
    String s = file.nextLine();
    count++;    
      if(s.contains("#AVFC")){
       System.out.printf("There are %d words on this line ", s.split("\\s").length-1);
       System.out.println(count);   
      }

  }
file.close(); 

输出:

    There are 4 words on this line 1

    There are 8 words on this line 13

    There are 3 words on this line 16

5 个答案:

答案 0 :(得分:4)

我能想到的最简单的方法是使用String.split("\\s"),它将根据空格进行拆分。

然后让HashMap包含一个单词作为键,其值为使用它的次数。

   HashMap<String, Integer> mapOfWords = new HashMap<String, Integer>();

      while (file.hasNextLine()) {
        String s = file.nextLine(); 
        String[] words = s.split("\\s");
        int count;
        for (String word : words) {
           if (mapOfWords.get(word) == null) {
              mapOfWords.put(word, 1);
           }
           else {
              count = mapOfWord.get(word);
              mapOfWords.put(word, count + 1);
           }
        }
      }

您要求跳过包含特定字词的字符串的实现

   HashMap<String, Integer> mapOfWords = new HashMap<String, Integer>();

   while (file.hasNextLine()) {
        String s = file.nextLine(); 
        String[] words = s.split("\\s");
        int count;

        if (isStringWanted(s) == false) {
           continue;  
        } 

        for (String word : words) {
           if (mapOfWords.get(word) == null) {
              mapOfWords.put(word, 1);
           }
           else {
              count = mapOfWord.get(word);
              mapOfWords.put(word, count + 1);
           }
        }
      }

private boolean isStringWanted(String s) {
    String[] checkStrings = new String[] {"chelsea", "Liverpool", "#LFC"};

    for (String check : checkString) {
        if (s.contains(check)) {
           return false;
        }
    }
    return true;
}

答案 1 :(得分:4)

尝试以下代码,它可以解决您的问题,此外您可以在将其放入hashmap之前调用String.toLowerCase()

String line ="a a b b b b a q c c";
...
Map<String,Integer> map = new HashMap<String,Integer>();
Scanner scanner = new Scanner(line); 
while (scanner.hasNext()) {
    String s = scanner.next();
    Integer count = map.put(s,1); 
    if(count!=null) map.put(s,count + 1);
}
...
System.out.println(map);

结果:

{b=4, c=2, q=1, a=3}

答案 2 :(得分:0)

检查番石榴Multiset。他们的描述以'The traditional Java idiom for e.g. counting how many times a word occurs in a document is something like:'开头。如果没有MultiSet,你会发现一些代码片段。

BTW:如果你只想计算字符串中的单词数,为什么不计算空格呢?您可以使用apache commons中的StringUtils。它比创建分割部分的阵列要好得多。另请查看their implementation

int count = StringUtils.countMatches(string, " ");

答案 3 :(得分:0)

最快的是将分割的数据存储在ArrayList中然后迭代你的ArrayList并使用[Collections.frequency](http://www.tutorialspoint.com/java/util/collections_frequency.htm

答案 4 :(得分:-2)

在给定的String中,可以使用String并通过循环计算给定String#indexOf(String, int)的出现次数

String haystack = "This is a string";
String needle = "i";
int index = 0;

while (index != -1) {
    index = haystack.indexOf(needle, index + 1);

    if (index != -1) {
        System.out.println(String.format("Found %s in %s at index %s.", needle, haystack, index));
    }
}