簡體   English   中英

查找文檔中出現的單詞或短語的次數

[英]Find how many times a word or phrase occurs in a document

我正在研究一個讀取文件的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是好的'。 然后你使用的是scan.next,它將返回Java,然后你會問這是否等於'Java is good'。 顯然會返回虛假。

@Mikkel Andersen正走在正確的道路上。 JavaDoc for Scanner聲明next是分隔符,默認分隔符是空格。 雖然Scanner確實提供了更改其分隔符的方法,但我相信在這種情況下, hasNext(String)next(String)將更有用。 要使用這些方法,您需要修改while循環,如下所示。

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

編輯:還值得一提的是,您可能仍會遇到換行問題。 由於Scanner可能會看到“Java is \\ ngood”而非“Java is good”。為了解決這個問題,您需要在輸入短語時使用正則表達式。

您想要的行為對解決方案至關重要。

@FrankPuffer問了一個很棒的問題: “如果你的文字是”xxxx“,短語”xx“會出現多少次?兩次或三次?”

這個問題的基礎是如何消耗比賽。 在你對他的問題回答“三”時,掃描的行為將是單個字符消費的行為。 也就是說,在匹配位置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();
    }
}

如果您只需要發生次數,那么我的解決方案將更簡單

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);
  } 
}

如果您了解邏輯,那么我認為您可以相應地編輯您的代碼。 希望這可以幫助!

暫無
暫無

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

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