DaViDa
DaViDa

Reputation: 641

Boyer-moore counting words java

I have an assignment in java where I have to use the Boyer Moore substring search solution of Sedgewick: http://algs4.cs.princeton.edu/53substring/BoyerMoore.java.html

Now it will stop when the first occurence of the word is found and returns the place where it is found. So to count words I changed the search method to:

public String search(String txt) {
        int M = pat.length();
        int N = txt.length();
        int count = 0;
        int skip = 0;
        int charCount = 0;
        for (int i = 0; i <= N - M; i += skip) {
            skip = 0;
            for (int j = M-1; j >= 0; j--) {
                if (pat.charAt(j) != txt.charAt(i+j)) {
                    skip = Math.max(1, j - right[txt.charAt(i+j)]);
                    break;
                }
                charCount++;
            }
            if (skip == 0)
            {
                count++;
                skip++;
            }
        }
        return "Aantal char: " + charCount + "\n" + count;                      
    }

I changed the if skip statement to run a counter "count" and return it at the end. What happens is, if I feed it a pattern and some text by hand it seems to count fine so:

pattern: test text: "this test is a test test testtest" outcome: 5

However I need to read in a txt file of some text of about 70k words and substring search that:

        BufferedReader input = new BufferedReader(new FileReader(System.getProperty("user.home") + "/Desktop/opdr3tekst.txt"));
        StringBuilder stringBuilder = new StringBuilder();

        while(input.readLine() != null)
        {
            stringBuilder.append(input.readLine());
        }
        input.close();

        BoyerMoore boyer = new BoyerMoore("pattern to search");


        System.out.println(boyer.search(stringBuilder.toString()));

So when I search a word I always get a number thats a lot less than when I CMD+F the file itself in mac text editor. Any idea what is going wrong?

Upvotes: 0

Views: 549

Answers (1)

Titus
Titus

Reputation: 22474

You're skipping lines from the file when it is read. That is because of this while(input.readLine() != null). The line read when this statement is executed is never added to the StringBuilder

To fix that, you can do something like this:

for(String line;(line = input.readLine())!=null;){
     stringBuilder.append(line);
}

Upvotes: 1

Related Questions