Skip to content

Apache Lucene open-source search software

LUCENE-9783: Hunspell: don't suggest more than 4 ngram corrections by default (#2388)

Peter Gromovf879c6ad8452
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java
index 23eaa1ea83e..7b1fdf96589 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java
@@ -166,7 +166,7 @@ public class Dictionary {
List<RepEntry> repTable = new ArrayList<>();
List<List<String>> mapTable = new ArrayList<>();
int maxDiff = 5;
- int maxNGramSuggestions = Integer.MAX_VALUE;
+ int maxNGramSuggestions = 4;
boolean onlyMaxDiff;
char noSuggest, subStandard;
ConvTable iconv, oconv;
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/GeneratingSuggester.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/GeneratingSuggester.java
index 59ae43a2f6e..b8a9a676678 100644
--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/GeneratingSuggester.java
+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/GeneratingSuggester.java
@@ -298,7 +298,7 @@ class GeneratingSuggester {
&& result.stream().noneMatch(weighted.word::contains)
&& speller.checkWord(weighted.word)) {
result.add(weighted.word);
- if (result.size() > dictionary.maxNGramSuggestions) {
+ if (result.size() >= dictionary.maxNGramSuggestions) {
break;
}
}
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestSpellChecking.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestSpellChecking.java
index b13a2f842eb..113fad0323b 100644
--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestSpellChecking.java
+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestSpellChecking.java
@@ -200,6 +200,10 @@ public class TestSpellChecking extends StemmerTestBase {
doTest("sug2");
}
+ public void testMaxNGramSugsDefaultIsNotUnlimited() throws Exception {
+ doTest("maxNGramSugsDefault");
+ }
+
public void testMixedCaseSuggestionHeuristics() throws Exception {
doTest("i58202");
}
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/maxNGramSugsDefault.aff b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/maxNGramSugsDefault.aff
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/maxNGramSugsDefault.dic b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/maxNGramSugsDefault.dic
new file mode 100644
index 00000000000..fa5b30f5971
--- /dev/null
+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/maxNGramSugsDefault.dic
@@ -0,0 +1,13 @@
+12
+cryptography
+cryptographer
+crystallographic
+cartographic
+photographic
+typographic
+cryptogram
+topographic
+orthographic
+tomographic
+phonographic
+pictograph
\ No newline at end of file
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/maxNGramSugsDefault.sug b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/maxNGramSugsDefault.sug
new file mode 100644
index 00000000000..4df42db61b7
--- /dev/null
+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/maxNGramSugsDefault.sug
@@ -0,0 +1 @@
+cryptography, cryptographer, crystallographic, cartographic
\ No newline at end of file
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/maxNGramSugsDefault.wrong b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/maxNGramSugsDefault.wrong
new file mode 100644
index 00000000000..9176e3a1a0a
--- /dev/null
+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/maxNGramSugsDefault.wrong
@@ -0,0 +1 @@
+cryptographic
\ No newline at end of file