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

时间:2016-06-15 20:00:22

标签: 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())
        {
            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();
    }
}

4 个答案:

答案 0 :(得分:0)

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

考虑一下你的文本文件包含'Java好,java好'。而你正在寻找'Java是好的'。然后你使用的是scan.next,它将返回Java,然后你会问这是否等于'Java is good'。显然会返回虚假。

答案 1 :(得分:0)

@Mikkel Andersen正走在正确的道路上。 Scanner的{​​{3}}表示next使用分隔符,默认分隔符是空格。虽然Scanner确实提供了更改其分隔符的方法,但我相信JavaDochasNext(String)在这种情况下会更有用。要使用这些方法,您需要修改while循环,如下所示。

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

编辑:还值得一提的是,您可能仍会遇到换行问题。由于Scanner可能会看到“Java is \ ngood”而非“Java is good”为了解决这个问题,您需要在输入短语时使用正则表达式。

答案 2 :(得分:0)

您想要的行为对解决方案至关重要。

@FrankPuffer提出了一个很好的问题:"如果您的文字是" x x x x",该短语" x x"发生?两次或三次?"

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

您这样说:

  

是的,如果我想找到" Java是好的"来自" Java很好,但___是   更好",结果应该是0次。

这告诉我你不想要这些解决方案。这听起来像你想要"搜索参数与列表中的行匹配的次数。"如果是这种情况,这很容易。

代码

public void run() {
    File f = new File("ARI Test.txt");
    try {
        Scanner scanner = new Scanner(f);
        while (scanner.hasNextLine())
        {
            String line = scanner.nextLine();
            if (line.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();
    }
}

答案 3 :(得分: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);
  } 
}

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

相关问题