From bf3521be529d87331652547bcd207f3abd59cd0e Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sat, 4 Apr 2015 14:51:37 +0200 Subject: Fix bugs when looking for more than one typo I thought that arraycopy had memcpy semantics, but oh gawd I was wrong. The other bug is that the best result could be overwritten by a worse one since the wrong result was checked. --- spellchecker/src/SpellCorrector.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/spellchecker/src/SpellCorrector.java b/spellchecker/src/SpellCorrector.java index 4bc3fe8..f3e504d 100644 --- a/spellchecker/src/SpellCorrector.java +++ b/spellchecker/src/SpellCorrector.java @@ -117,7 +117,11 @@ public class SpellCorrector { // if a better word was found, use the change if (rater.hasBetterSuggestion()) { rater.saveSuggestion(); - bestResult = rater; + if (bestResult == null || rater.getBestScore() > bestResult.getBestScore()) { + // apparently this is not just the best result of this + // round, but the overall result! Save it. + bestResult = rater; + } // If some other errors are still possible, hunt for those! modifiedIndices.add(i); @@ -127,7 +131,7 @@ public class SpellCorrector { // Did it find a corrected word? if (subResult != null) { // Yes it did. Is this the best one so far? - if (subResult.getBestScore() > rater.getBestScore()) { + if (subResult.getBestScore() > bestResult.getBestScore()) { debugPrint("Subresult is better!"); bestResult = subResult; } else { @@ -522,7 +526,7 @@ public class SpellCorrector { // save the word and the affected scores words[index] = word; - System.arraycopy(word_likelihoods, 0, scores, 0, words.length); + System.arraycopy(scores, 0, word_likelihoods, 0, words.length); sentence_probability = best_modification.probability; if (DEBUG_SCORE) { -- cgit v1.2.1