簡體   English   中英

我如何計算一行中單詞的出現

[英]how do i count occurrence of words in a line

我是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

我能想到的最簡單的方法是使用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;
}

嘗試下面的代碼,它可能會解決您的問題,此外,您可以在將其放入哈希圖中之前調用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}

檢查番石榴的Multiset 他們的描述始於'The traditional Java idiom for eg counting how many times a word occurs in a document is something like:' 您會找到一些代碼片段,而不使用MultiSet怎么做。

順便說一句:如果您只想計算字符串中的單詞數,為什么不只計算空格呢? 您可以使用來自Apache Commons的StringUtils 這比創建拆分部分的數組要好得多。 也看看它們的實現

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

最快的方法是將拆分后的數據存儲在ArrayList中,然后在ArrayList上進行迭代並使用[Collections.frequency]( http://www.tutorialspoint.com/java/util/collections_frequency.htm

在給定的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));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM