Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Spell check single lexeme against suggested alteratives

AuthorDaveJarvis <email>
Date2021-11-28 16:02:32 GMT-0800
Commit13fd9ae23aec5c698033315e8b229707e8e0f682
Parent7d9b188
Delta20 lines added, 6 lines removed, 14-line increase
src/main/java/com/keenwrite/spelling/impl/SymSpellSpeller.java
import java.io.InputStreamReader;
import java.text.BreakIterator;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
import static com.keenwrite.constants.Constants.LEXICONS_DIRECTORY;
/**
* Prevent direct instantiation so that only the {@link SpellChecker}
- * interface
- * is available.
+ * interface is available.
*
* @param symSpell The implementation-specific spell checker.
*/
private SymSpellSpeller( final SymSpell symSpell ) {
mSymSpell = symSpell;
}
+ /**
+ * This expensive operation is only called for viable words, not for
+ * single punctuation characters or whitespace.
+ *
+ * @param lexeme The word to check for correctness.
+ * @return {@code false} if the word is not in the lexicon.
+ */
@Override
public boolean inLexicon( final String lexeme ) {
- return lookup( lexeme, CLOSEST ).size() == 1;
+ assert lexeme != null;
+ assert !lexeme.isBlank();
+
+ final var words = lookup( lexeme, CLOSEST );
+ return !words.isEmpty() && lexeme.equals( words.get( 0 ).getSuggestion() );
}
@Override
public void proofread(
- final String text, final SpellCheckListener consumer ) {
+ final String text,
+ final SpellCheckListener consumer ) {
assert text != null;
assert consumer != null;
*/
private boolean isWord( final String word ) {
- return !word.isEmpty() && isLetter( word.charAt( 0 ) );
+ return !word.isBlank() && isLetter( word.charAt( 0 ) );
}