Apache Lucene open-source search software
LUCENE-5497: fix hunspell escaping/optional conditions
Robert Muir75d580e78e9f
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 7b6e0f79559..7314f808973 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -147,6 +147,9 @@ Bug fixesrecursive affix application are driven correctly by continuation classes in the affix file.(Robert Muir)+* LUCENE-5497: HunspellStemFilter properly handles escaped terms and affixes without conditions.+ (Robert Muir)+Test Framework* LUCENE-5449: Rename _TestUtil and _TestHelper to remove the leading _.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.javaindex 974d24185db..c2e8add8fb0 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@@ -348,8 +348,10 @@ public class Dictionary {String line = reader.readLine();String ruleArgs[] = line.split("\\s+");- if (ruleArgs.length < 5) {- throw new ParseException("The affix file contains a rule with less than five elements", reader.getLineNumber());+ // from the manpage: PFX flag stripping prefix [condition [morphological_fields...]]+ // condition is optional+ if (ruleArgs.length < 4) {+ throw new ParseException("The affix file contains a rule with less than four elements: " + line, reader.getLineNumber());}char flag = flagParsingStrategy.parseFlag(ruleArgs[1]);@@ -370,7 +372,7 @@ public class Dictionary {Arrays.sort(appendFlags);}- String condition = ruleArgs[4];+ String condition = ruleArgs.length > 4 ? ruleArgs[4] : ".";// at least the gascon affix file has this issueif (condition.startsWith("[") && !condition.endsWith("]")) {condition = condition + "]";@@ -550,6 +552,24 @@ public class Dictionary {throw new IllegalArgumentException("Unknown flag type: " + flagType);}+ final char FLAG_SEPARATOR = 0x1f; // flag separator after escaping++ String unescapeEntry(String entry) {+ StringBuilder sb = new StringBuilder();+ for (int i = 0; i < entry.length(); i++) {+ char ch = entry.charAt(i);+ if (ch == '\\' && i+1 < entry.length()) {+ sb.append(entry.charAt(i+1));+ i++;+ } else if (ch == '/') {+ sb.append(FLAG_SEPARATOR);+ } else {+ sb.append(ch);+ }+ }+ return sb.toString();+ }+/*** Reads the dictionary file through the provided InputStreams, building up the words map*@@ -570,8 +590,9 @@ public class Dictionary {String line = lines.readLine(); // first line is number of entries (approximately, sometimes)while ((line = lines.readLine()) != null) {+ line = unescapeEntry(line);if (needsInputCleaning) {- int flagSep = line.lastIndexOf('/');+ int flagSep = line.lastIndexOf(FLAG_SEPARATOR);if (flagSep == -1) {CharSequence cleansed = cleanInput(line, sb);writer.write(cleansed.toString().getBytes(IOUtils.CHARSET_UTF_8));@@ -604,7 +625,7 @@ public class Dictionary {scratch1.length = o1.length;for (int i = scratch1.length - 1; i >= 0; i--) {- if (scratch1.bytes[scratch1.offset + i] == '/') {+ if (scratch1.bytes[scratch1.offset + i] == FLAG_SEPARATOR) {scratch1.length = i;break;}@@ -615,7 +636,7 @@ public class Dictionary {scratch2.length = o2.length;for (int i = scratch2.length - 1; i >= 0; i--) {- if (scratch2.bytes[scratch2.offset + i] == '/') {+ if (scratch2.bytes[scratch2.offset + i] == FLAG_SEPARATOR) {scratch2.length = i;break;}@@ -648,7 +669,7 @@ public class Dictionary {String entry;char wordForm[];- int flagSep = line.lastIndexOf('/');+ int flagSep = line.lastIndexOf(FLAG_SEPARATOR);if (flagSep == -1) {wordForm = NOFLAGS;entry = line;@@ -828,6 +849,9 @@ public class Dictionary {}StringBuilder builder = new StringBuilder();+ if (rawFlags.length() % 2 == 1) {+ throw new IllegalArgumentException("Invalid flags (should be even number of characters): " + rawFlags);+ }for (int i = 0; i < rawFlags.length(); i+=2) {char cookedFlag = (char) ((int) rawFlags.charAt(i) + (int) rawFlags.charAt(i + 1));builder.append(cookedFlag);diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/StemmerTestBase.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/StemmerTestBase.javaindex 083ae26ac76..0613dd37d90 100644--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/StemmerTestBase.java+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/StemmerTestBase.java@@ -55,7 +55,7 @@ abstract class StemmerTestBase extends LuceneTestCase {}try {- Dictionary dictionary = new Dictionary(affixStream, Arrays.asList(dictStreams), true);+ Dictionary dictionary = new Dictionary(affixStream, Arrays.asList(dictStreams), ignoreCase);stemmer = new Stemmer(dictionary);} finally {IOUtils.closeWhileHandlingException(affixStream);diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestAllDictionaries2.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestAllDictionaries2.javaindex d0a83561802..98a9c6eadc2 100644--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestAllDictionaries2.java+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestAllDictionaries2.java@@ -101,10 +101,10 @@ public class TestAllDictionaries2 extends LuceneTestCase {"hausa_spelling_dictionary-0.2-tb+fx.xpi", "dictionaries/ha-GH.dic", "dictionaries/ha-GH.aff","hebrew_spell_checking_dictionary_from_hspell-1.2.0.1-fx+sm+tb.xpi", "dictionaries/he.dic", "dictionaries/he.aff","hindi_spell_checker-0.4-fx+tb+sm+sb+fn.xpi", "dictionaries/hi_IN.dic", "dictionaries/hi_IN.aff",-//BUG! "hungarian_dictionary-1.6.1.1-fx+tb+sm+fn.xpi", "dictionaries/hu_HU.dic", "dictionaries/hu_HU.aff",+//BUG! "hungarian_dictionary-1.6.1.1-fx+tb+sm+fn.xpi", "dictionaries/hu.dic", "dictionaries/hu.aff",//BUG! "icelandic_dictionary-1.3-fx+tb+sm.xpi", "dictionaries/is.dic", "dictionaries/is.aff","kamus_pengecek_ejaan_bahasa_indonesia-1.1-fx+tb.xpi", "dictionaries/id.dic", "dictionaries/id.aff",-//BUG! "kannada_spell_checker-2.0.1-tb+sm+fn+an+fx.xpi", "dictionaries/kn.dic", "dictionaries/kn.aff",+ "kannada_spell_checker-2.0.1-tb+sm+fn+an+fx.xpi", "dictionaries/kn.dic", "dictionaries/kn.aff","kashubian_spell_checker_poland-0.9-sm+tb+fx.xpi", "dictionaries/Kaszebsczi.dic", "dictionaries/Kaszebsczi.aff","kiswahili_spell_checker-0.3-sb+tb+fn+fx+sm.xpi", "dictionaries/sw_TZ.dic", "dictionaries/sw_TZ.aff","kurdish_spell_checker-0.96-fx+tb+sm.xpi", "dictionaries/ku-TR.dic", "dictionaries/ku-TR.aff",@@ -125,7 +125,7 @@ public class TestAllDictionaries2 extends LuceneTestCase {"oriya_spell_checker-0.3-fn+tb+fx+sm+sb.xpi", "dictionaries/or-IN.dic", "dictionaries/or-IN.aff","polski_slownik_poprawnej_pisowni-1.0.20110621-fx+tb+sm.xpi", "dictionaries/pl.dic", "dictionaries/pl.aff","punjabi_spell_checker-0.3-fx+tb+sm+sb+fn.xpi", "dictionaries/pa-IN.dic", "dictionaries/pa-IN.aff",-//BUG! "romanian_spellchecking_dictionary-1.14-sm+tb+fx.xpi", "dictionaries/ro_RO-ante1993.dic", "dictionaries/ro_RO-ante1993.aff",+ "romanian_spellchecking_dictionary-1.14-sm+tb+fx.xpi", "dictionaries/ro_RO-ante1993.dic", "dictionaries/ro_RO-ante1993.aff",//BUG! "russian_hunspell_dictionary-1.0.20131101-tb+sm+fn+fx.xpi", "dictionaries/ru_RU.dic", "dictionaries/ru_RU.aff","sanskrit_spell_checker-1.1-fx+tb+sm+sb+fn.xpi", "dictionaries/sa_IN.dic", "dictionaries/sa_IN.aff","scottish_gaelic_spell_checker-2.7-tb+fx+sm.xpi", "dictionaries/gd-GB.dic", "dictionaries/gd-GB.aff",@@ -161,7 +161,7 @@ public class TestAllDictionaries2 extends LuceneTestCase {"verificador_ortografico_para_portugues_do_brasil-2.3-3.2b1-tb+sm+fn+fx.xpi", "dictionaries/pt_BR.dic", "dictionaries/pt_BR.aff","vietnamese_dictionary-2.1.0.159-an+sm+tb+fx+fn.xpi", "dictionaries/vi-DauCu.dic", "dictionaries/vi-DauCu.aff","vietnamese_dictionary-2.1.0.159-an+sm+tb+fx+fn.xpi", "dictionaries/vi-DauMoi.dic", "dictionaries/vi-DauMoi.aff",-//BUG! "woordenboek_nederlands-3.1.1-sm+tb+fx+fn.xpi", "dictionaries/nl.dic", "dictionaries/nl.aff",+ "woordenboek_nederlands-3.1.1-sm+tb+fx+fn.xpi", "dictionaries/nl.dic", "dictionaries/nl.aff","xhosa_spell_checker-20110323-tb+fn+fx+sm.xpi", "dictionaries/xh-ZA.dic", "dictionaries/xh-ZA.aff","xuxen-4.0.1-fx+tb+sm.xpi", "dictionaries/eu.dic", "dictionaries/eu.aff","yiddish_spell_checker_yivo-0.0.3-sm+fn+fx+tb.xpi", "dictionaries/yi.dic", "dictionaries/yi.aff",@@ -196,7 +196,7 @@ public class TestAllDictionaries2 extends LuceneTestCase {}public void testOneDictionary() throws Exception {- String toTest = "hausa_spelling_dictionary-0.2-tb+fx.xpi";+ String toTest = "woordenboek_nederlands-3.1.1-sm+tb+fx+fn.xpi";for (int i = 0; i < tests.length; i++) {if (tests[i].equals(toTest)) {File f = new File(DICTIONARY_HOME, tests[i]);diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestDictionary.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestDictionary.javaindex 5d7682e88c0..bb81ed18629 100644--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestDictionary.java+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestDictionary.java@@ -87,7 +87,7 @@ public class TestDictionary extends LuceneTestCase {new Dictionary(affixStream, dictStream);fail("didn't get expected exception");} catch (ParseException expected) {- assertEquals("The affix file contains a rule with less than five elements", expected.getMessage());+ assertTrue(expected.getMessage().startsWith("The affix file contains a rule with less than four elements"));assertEquals(24, expected.getErrorOffset());}diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestEscaped.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestEscaped.javanew file mode 100644index 00000000000..d5b1489b3fc--- /dev/null+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestEscaped.java@@ -0,0 +1,36 @@+package org.apache.lucene.analysis.hunspell;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import org.junit.BeforeClass;++public class TestEscaped extends StemmerTestBase {+ @BeforeClass+ public static void beforeClass() throws Exception {+ init("escaped.aff", "escaped.dic");+ }++ public void testStemming() {+ assertStemsTo("works", "work");+ assertStemsTo("work", "work");+ assertStemsTo("R2/D2", "R2/D2");+ assertStemsTo("R2/D2s", "R2/D2");+ assertStemsTo("N/A", "N/A");+ assertStemsTo("N/As");+ }+}diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestHunspellStemFilter.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestHunspellStemFilter.javaindex f42afcfa9cc..5656f6ee753 100644--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestHunspellStemFilter.java+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestHunspellStemFilter.java@@ -31,6 +31,7 @@ import org.apache.lucene.analysis.hunspell.Dictionary;import org.apache.lucene.analysis.hunspell.HunspellStemFilter;import org.apache.lucene.analysis.miscellaneous.SetKeywordMarkerFilter;import org.apache.lucene.analysis.util.CharArraySet;+import org.apache.lucene.util.IOUtils;import org.junit.AfterClass;import org.junit.BeforeClass;@@ -39,9 +40,13 @@ public class TestHunspellStemFilter extends BaseTokenStreamTestCase {@BeforeClasspublic static void beforeClass() throws Exception {- try (InputStream affixStream = TestStemmer.class.getResourceAsStream("simple.aff");- InputStream dictStream = TestStemmer.class.getResourceAsStream("simple.dic")) {+ // no multiple try-with to workaround bogus VerifyError+ InputStream affixStream = TestStemmer.class.getResourceAsStream("simple.aff");+ InputStream dictStream = TestStemmer.class.getResourceAsStream("simple.dic");+ try {dictionary = new Dictionary(affixStream, dictStream);+ } finally {+ IOUtils.closeWhileHandlingException(affixStream, dictStream);}}@@ -97,9 +102,13 @@ public class TestHunspellStemFilter extends BaseTokenStreamTestCase {public void testIgnoreCaseNoSideEffects() throws Exception {final Dictionary d;- try (InputStream affixStream = TestStemmer.class.getResourceAsStream("simple.aff");- InputStream dictStream = TestStemmer.class.getResourceAsStream("simple.dic")) {+ // no multiple try-with to workaround bogus VerifyError+ InputStream affixStream = TestStemmer.class.getResourceAsStream("simple.aff");+ InputStream dictStream = TestStemmer.class.getResourceAsStream("simple.dic");+ try {d = new Dictionary(affixStream, Collections.singletonList(dictStream), true);+ } finally {+ IOUtils.closeWhileHandlingException(affixStream, dictStream);}Analyzer a = new Analyzer() {@Overridediff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestOptionalCondition.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestOptionalCondition.javanew file mode 100644index 00000000000..bddee500347--- /dev/null+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestOptionalCondition.java@@ -0,0 +1,42 @@+package org.apache.lucene.analysis.hunspell;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import org.junit.BeforeClass;++public class TestOptionalCondition extends StemmerTestBase {+ @BeforeClass+ public static void beforeClass() throws Exception {+ init("optional-condition.aff", "condition.dic");+ }++ public void testStemming() {+ assertStemsTo("hello", "hello");+ assertStemsTo("try", "try");+ assertStemsTo("tried", "try");+ assertStemsTo("work", "work");+ assertStemsTo("worked", "work");+ assertStemsTo("rework", "work");+ assertStemsTo("reworked", "work");+ assertStemsTo("retried");+ assertStemsTo("workied");+ assertStemsTo("tryed");+ assertStemsTo("tryied");+ assertStemsTo("helloed");+ }+}diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/broken.aff b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/broken.affindex 3b780cd1d7b..81741793719 100644--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/broken.aff+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/broken.aff@@ -19,6 +19,6 @@ SFX E 0 d oPFX B Y 1PFX B 0 s o-#wrong rule (only 4 elements)+#wrong rule (only 3 elements)PFX A0 Y 1-PFX A0 0 a\ No newline at end of file+PFX A0 0\ No newline at end of filediff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/escaped.aff b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/escaped.affnew file mode 100644index 00000000000..b42845175e2--- /dev/null+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/escaped.aff@@ -0,0 +1,4 @@+SET UTF-8++SFX A Y 1+SFX A 0 s . +PLURdiff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/escaped.dic b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/escaped.dicnew file mode 100644index 00000000000..93602944260--- /dev/null+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/escaped.dic@@ -0,0 +1,4 @@+3+work/A+R2\/D2/A+N\/Adiff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/optional-condition.aff b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/optional-condition.affnew file mode 100644index 00000000000..f2e3b9c2c7c--- /dev/null+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/optional-condition.aff@@ -0,0 +1,14 @@+SET UTF-8+TRY esianrtolcdugmphbyfvkwzESIANRTOLCDUGMPHBYFVKWZ’++REP 2+REP f ph+REP ph f++# has no condition+PFX A Y 1+PFX A 0 re++SFX B Y 2+SFX B 0 ed [^y]+SFX B y ied y\ No newline at end of file