【问题标题】:How to count how many times a keyword appears in a tax in java?如何计算关键字在java中的税中出现的次数?
【发布时间】:2013-10-11 20:14:49
【问题描述】:

问题一:

我正在尝试计算关键字的频率,我的代码可以正常工作,只是它也很重要 那些也包含关键字的词(例如,如果我搜索“count”,那么“account”之类的词也会被计算在内。)有人知道如何解决这个问题吗?

问题 2:

我还想计算文本中唯一单词的数量(这意味着我只计算重复单词一次)。我也不知道如何实现这一点。我的代码只给了我总字数。

这是我的代码:

import java.util.Scanner;


public class Text_minining {

/**
 * @param args
 */
public static void main(String[] args) {

    //Prompt the user for the search word
    System.out.print("enter a search word: ");
    //Get the user's search word input
    Scanner keywordScanner = new Scanner(System.in);
    String keyword = keywordScanner.nextLine();
    keyword = keyword.toLowerCase();

    //Prompt the user for the text
    System.out.println("Enter a string of words (words separated by single spaces or tabs): ");
    //Get the user's string input
    Scanner userInputScanner = new Scanner(System.in);
    String userInput = userInputScanner.nextLine();
    userInput = userInput.toLowerCase();

    int keywordCount = 0, wordCount = 0;
    int lastIndex = 0;

    while(lastIndex != -1){
        lastIndex = userInput.indexOf(keyword,lastIndex);
        if(lastIndex != -1){
            keywordCount ++;
            lastIndex = keyword.length() + lastIndex;
        }
    }

    boolean wasSpace=true;
    for (int i = 0; i < userInput.length(); i++) 
    {
        if (userInput.charAt(i) == ' ') {
            wasSpace=true;
        }
        else{
            if(wasSpace == true) wordCount++;
            wasSpace = false;
            }
    }

    //Print the results to the screen
    System.out.println("-------");
    System.out.println("Good, \"" + keyword + "\"appears in the text and the word count is " + keywordCount);
    System.out.println("The total number of unique words in the text is " + wordCount);


    System.exit(0);
}
    }

【问题讨论】:

  • This answer 并不完全是您要寻找的东西,但它可能会有所帮助。看看吧。

标签: java count keyword frequency


【解决方案1】:

首先:userInput.split(keyword).length - 1 可以解决问题。我们使用正则表达式。

第二个:

Set<String> uniqueWords = new HashSet<String>();
for (String word : userInput.split(" ")) {
   uniqueWords.add(word);
}
System.out.println("Unique words count " + uniqueWords.size());

【讨论】:

    【解决方案2】:

    只需使用字符串方法拆分即可。

    String words[] = userInput.split(keyword);

    然后检查并统计关键字...

    for ( String w : words) {
       // do check
    }
    

    【讨论】:

      【解决方案3】:

      同意。使用 split 创建数组,然后就可以使用

      (new HashSet(Arrays.asList(yourArray))).size(); 
      

      找到计数

      【讨论】:

        【解决方案4】:

        我建议你采用这种方法:

        • 用空格分割userInput 字符串:userInput.split("\\s+")。你会得到一个数组。见String.split()
        • 对于问题 1:遍历数组,将每个字符串与您的 关键字 进行比较。请参阅String.equals()String.equalsIgnoreCase()
        • 对于问题 2:将数组添加到 Set。由于这不能包含任何重复的项目,它的大小将为您提供答案。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-06
          • 1970-01-01
          • 2016-10-01
          • 1970-01-01
          • 2014-07-25
          • 1970-01-01
          • 2011-06-14
          • 1970-01-01
          相关资源
          最近更新 更多