Java 查找单词或短语在文档中出现的次数

Java 查找单词或短语在文档中出现的次数,java,file,user-interface,input,Java,File,User Interface,Input,我正在开发一个GUI,它读取一个文件并搜索一个单词和短语出现的次数。我在搜索单个单词时使用了代码,但没有搜索短语。我已经在下面发布了具体的方法,有人能帮我吗 public void run() { File f = new File("ARI Test.txt"); try { Scanner scanner = new Scanner(f); while (scanner.hasNext()) { Stri

我正在开发一个GUI,它读取一个文件并搜索一个单词和短语出现的次数。我在搜索单个单词时使用了代码,但没有搜索短语。我已经在下面发布了具体的方法,有人能帮我吗

public void run() {
    File f = new File("ARI Test.txt");
    try {
        Scanner scanner = new Scanner(f);
        while (scanner.hasNext())
        {
            String str = scanner.next();
            if (str.equals(word))
                count++;
        }
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                textArea.append(word + " appears: " + count + " time(s)\n");
            }
        });
        scanner.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

扫描仪逻辑可能有问题。当您调用scanner.next时,它将只返回下一个单词,而不是整行


假设您的文本文件包含“Java很好,Java很好”。您正在搜索“Java是好的”。然后使用scanner.next,它将返回Java,然后询问这是否等于“Java是好的”。显然,这将返回false。

扫描仪逻辑可能有问题。当您调用scanner.next时,它将只返回下一个单词,而不是整行


假设您的文本文件包含“Java很好,Java很好”。您正在搜索“Java是好的”。然后使用scanner.next,它将返回Java,然后询问这是否等于“Java是好的”。显然,这将返回一个错误。

@Mikkel Andersen走在正确的道路上。for
扫描仪
说明
下一个
使用分隔符,默认分隔符为空白。虽然
Scanner
确实提供了更改其分隔符的方法,但我相信and在这种情况下会更有用。要使用这些方法,您需要修改while循环,如下所示

 while(scanner.hasNext(word))
 {
     scanner.next(word);
     count++;
 }

编辑:还值得一提的是,您可能仍然会遇到换行问题。由于
Scanner
可能会看到“Java很好”而不是“Java很好”,因此在输入短语时需要使用正则表达式

@Mikkel Andersen走上了正确的道路。for
扫描仪
说明
下一个
使用分隔符,默认分隔符为空白。虽然
Scanner
确实提供了更改其分隔符的方法,但我相信and在这种情况下会更有用。要使用这些方法,您需要修改while循环,如下所示

 while(scanner.hasNext(word))
 {
     scanner.next(word);
     count++;
 }
编辑:还值得一提的是,您可能仍然会遇到换行问题。由于
Scanner
可能会看到“Java很好”而不是“Java很好”,因此在输入短语时需要使用正则表达式

您想要的行为对解决方案至关重要。 问了一个很好的问题:“如果你的文本是“x”,短语“x x”出现了多少次?两次还是三次?”

这个问题的基础是如何使用匹配项。在您对他的问题回答“三”时,扫描的行为将是单字符消费。也就是说,在位置0匹配后,您只会在之后搜索位置1+。这与非重叠搜索形成对比,非重叠搜索将起始搜索点增加
word.length

你这么说:

是的,如果我想从“Java是好的,但是____;是好的”中找到“Java是好的” “更好”,结果应为0倍

这说明你不想要这两种解决方案。听起来您想要“搜索参数与列表中的一行匹配的次数”。如果是这样,这很简单

代码 您想要的行为对解决方案至关重要。 问了一个很好的问题:“如果你的文本是“x”,短语“x x”出现了多少次?两次还是三次?”

这个问题的基础是如何使用匹配项。在您对他的问题回答“三”时,扫描的行为将是单字符消费。也就是说,在位置0匹配后,您只会在之后搜索位置1+。这与非重叠搜索形成对比,非重叠搜索将起始搜索点增加
word.length

你这么说:

是的,如果我想从“Java是好的,但是____;是好的”中找到“Java是好的” “更好”,结果应为0倍

这说明你不想要这两种解决方案。听起来您想要“搜索参数与列表中的一行匹配的次数”。如果是这样,这很简单

代码
如果您只需要事件计数,那么我的解决方案将更简单

public class SentenceCounter
{    
  public static void main(String[] args)
  {
    //The sentence for which you need to find the occurrence count
    String sentence = "Game of Thrones is";

    //Find the length of the sentence
    int sentenceLength = sentence.length();

    //This is the original text in which you are going to search
    String text = "Game of Thrones is a wonderful series. Game of Thrones is also a most famous series. Game of Thrones is and always will be the best HBO series";

    //Calculate the length of the entire text
    int initialLength = text.length();

    //Perform String 'replaceAll' operation to remove the sentence from original text
    text = text.replaceAll(sentence, "");

    //Calculate the new length of the 'text'
    int newLength = text.length();

    //Below formula should give you the No. of times the 'sentence' has occurred in the 'text'
    System.out.println((initialLength - newLength) / sentenceLength);
  } 
}

如果您理解逻辑,那么我认为您可以相应地编辑代码。希望这有帮助

如果您只需要事件计数,那么我的解决方案会更简单

public class SentenceCounter
{    
  public static void main(String[] args)
  {
    //The sentence for which you need to find the occurrence count
    String sentence = "Game of Thrones is";

    //Find the length of the sentence
    int sentenceLength = sentence.length();

    //This is the original text in which you are going to search
    String text = "Game of Thrones is a wonderful series. Game of Thrones is also a most famous series. Game of Thrones is and always will be the best HBO series";

    //Calculate the length of the entire text
    int initialLength = text.length();

    //Perform String 'replaceAll' operation to remove the sentence from original text
    text = text.replaceAll(sentence, "");

    //Calculate the new length of the 'text'
    int newLength = text.length();

    //Below formula should give you the No. of times the 'sentence' has occurred in the 'text'
    System.out.println((initialLength - newLength) / sentenceLength);
  } 
}

如果您理解逻辑,那么我认为您可以相应地编辑代码。希望这有帮助

如果你的文本是“x”,短语“x”出现多少次?两次或三次?短语“x x”将出现0次,因为它与“x x”不完全匹配。如果我想知道“Java是好的”出现了多少次,它应该只搜索与之完全匹配的字符串,而不仅仅是短语的一部分。短语“Java是好的Java是好的”在句子“Java是好的Java是好的Java是好的Java是好的”中出现了多少次是您要计算文档中出现次数的短语,您希望得到什么结果?如果“x x x x”中没有包含“x x x”,则此赋值似乎不太可靠如果您的文本是“x x x x”,则短语“x x”出现多少次?两次或三次?短语“x x”将出现0次,因为它与“x x”不完全匹配。如果我想知道“Java是好的”出现了多少次,它应该只搜索与之完全匹配的字符串,而不仅仅是短语的一部分。短语“Java是好的Java是好的”在句子“Java是好的Java是好的Java是好的Java是好的”中出现了多少次是您要计算文档中出现次数的短语,您希望得到什么结果?此赋值似乎不太正确