Apache Solr open-source search software
LUCENE-5505: hunspell SET/FLAG whitespace and BOM handling
Robert Muir8e3048d27093
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 7314f808973..ebe9df3d6d1 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -150,6 +150,9 @@ Bug fixes* LUCENE-5497: HunspellStemFilter properly handles escaped terms and affixes without conditions.(Robert Muir)+* LUCENE-5505: HunspellStemFilter ignores BOM markers in dictionaries and handles varying+ types of whitespace in SET/FLAG commands. (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 c2e8add8fb0..68a4b457749 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@@ -54,6 +54,7 @@ import java.util.List;import java.util.Locale;import java.util.Map;import java.util.TreeMap;+import java.util.regex.Matcher;import java.util.regex.Pattern;/**@@ -251,6 +252,10 @@ public class Dictionary {LineNumberReader reader = new LineNumberReader(new InputStreamReader(affixStream, decoder));String line = null;while ((line = reader.readLine()) != null) {+ // ignore any BOM marker on first line+ if (reader.getLineNumber() == 1 && line.startsWith("\uFEFF")) {+ line = line.substring(1);+ }if (line.startsWith(ALIAS_KEY)) {parseAlias(line);} else if (line.startsWith(PREFIX_KEY)) {@@ -466,6 +471,9 @@ public class Dictionary {return builder.finish();}++ /** pattern accepts optional BOM + SET + any whitespace */+ final static Pattern ENCODING_PATTERN = Pattern.compile("^(\u00EF\u00BB\u00BF)?SET\\s+");/*** Parses the encoding specified in the affix file readable through the provided InputStream@@ -475,7 +483,7 @@ public class Dictionary {* @throws IOException Can be thrown while reading from the InputStream* @throws ParseException Thrown if the first non-empty non-comment line read from the file does not adhere to the format {@code SET <encoding>}*/- private String getDictionaryEncoding(InputStream affix) throws IOException, ParseException {+ static String getDictionaryEncoding(InputStream affix) throws IOException, ParseException {final StringBuilder encoding = new StringBuilder();for (;;) {encoding.setLength(0);@@ -498,9 +506,10 @@ public class Dictionary {}continue;}- if (encoding.length() > 4 && "SET ".equals(encoding.substring(0, 4))) {- // cleanup the encoding string, too (whitespace)- return encoding.substring(4).trim();+ Matcher matcher = ENCODING_PATTERN.matcher(encoding);+ if (matcher.find()) {+ int last = matcher.end();+ return encoding.substring(last).trim();}}}@@ -538,8 +547,12 @@ public class Dictionary {* @param flagLine Line containing the flag information* @return FlagParsingStrategy that handles parsing flags in the way specified in the FLAG definition*/- private FlagParsingStrategy getFlagParsingStrategy(String flagLine) {- String flagType = flagLine.substring(5);+ static FlagParsingStrategy getFlagParsingStrategy(String flagLine) {+ String parts[] = flagLine.split("\\s+");+ if (parts.length != 2) {+ throw new IllegalArgumentException("Illegal FLAG specification: " + flagLine);+ }+ String flagType = parts[1];if (NUM_FLAG_TYPE.equals(flagType)) {return new NumFlagParsingStrategy();@@ -774,7 +787,7 @@ public class Dictionary {/*** Abstraction of the process of parsing flags taken from the affix and dic files*/- private static abstract class FlagParsingStrategy {+ static abstract class FlagParsingStrategy {/*** Parses the given String into a single flagdiff --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 98a9c6eadc2..5e7935a66d8 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@@ -48,7 +48,7 @@ public class TestAllDictionaries2 extends LuceneTestCase {"albanisches_worterbuch-1.6.9-fx+tb+sm+fn.xpi", "dictionaries/sq.dic", "dictionaries/sq.aff","amharic_spell_checker-0.4-fx+fn+tb+sm.xpi", "dictionaries/am_ET.dic", "dictionaries/am_ET.aff",//BUG! "arabic_spell_checking_dictionary-3.2.20120321-fx+tb.xpi", "dictionaries/ar.dic", "dictionaries/ar.aff",-//BUG! "armenian_spell_checker_dictionary-0.32-fx+tb+sm.xpi", "dictionaries/hy_AM.dic", "dictionaries/hy_AM.aff",+ "armenian_spell_checker_dictionary-0.32-fx+tb+sm.xpi", "dictionaries/hy_AM.dic", "dictionaries/hy_AM.aff","azerbaijani_spell_checker-0.3-fx+tb+fn+sm+sb.xpi", "dictionaries/az-Latn-AZ.dic", "dictionaries/az-Latn-AZ.aff","belarusian_classic_dictionary-0.1.2-tb+fx+sm.xpi", "dictionaries/be-classic.dic", "dictionaries/be-classic.aff","belarusian_dictionary-0.1.2-fx+sm+tb.xpi", "dictionaries/be.dic", "dictionaries/be.aff",@@ -72,13 +72,13 @@ public class TestAllDictionaries2 extends LuceneTestCase {"diccionario_espanol_argentina-2.5.1-tb+fx+sm.xpi", "dictionaries/es_AR.dic", "dictionaries/es_AR.aff","diccionario_espanol_mexico-1.1.3-fn+tb+fx+sm.xpi", "dictionaries/es_MX.dic", "dictionaries/es_MX.aff","diccionario_ortografico_valenciano-2.2.0-fx+tb+fn+sm.xpi", "dictionaries/roa-ES-val.dic", "dictionaries/roa-ES-val.aff",-//BUG! "diccionario_papiamentoaruba-0.2-fn+sm+tb+fx.xpi", "dictionaries/Papiamento.dic", "dictionaries/Papiamento.aff",+ "diccionario_papiamentoaruba-0.2-fn+sm+tb+fx.xpi", "dictionaries/Papiamento.dic", "dictionaries/Papiamento.aff","dictionnaires_francais-5.0.2-fx+tb+sm.xpi", "dictionaries/fr-classic-reform.dic", "dictionaries/fr-classic-reform.aff","dictionnaires_francais-5.0.2-fx+tb+sm.xpi", "dictionaries/fr-classic.dic", "dictionaries/fr-classic.aff","dictionnaires_francais-5.0.2-fx+tb+sm.xpi", "dictionaries/fr-modern.dic", "dictionaries/fr-modern.aff","dictionnaires_francais-5.0.2-fx+tb+sm.xpi", "dictionaries/fr-reform.dic", "dictionaries/fr-reform.aff","difazier_an_drouizig-0.12-tb+sm+fx.xpi", "dictionaries/br.dic", "dictionaries/br.aff",-//BUG! "dikshonario_papiamentuantia_hulandes-0.5-fx+tb+fn+sb+sm.xpi", "dictionaries/Papiamentu.dic", "dictionaries/Papiamentu.aff",+ "dikshonario_papiamentuantia_hulandes-0.5-fx+tb+fn+sb+sm.xpi", "dictionaries/Papiamentu.dic", "dictionaries/Papiamentu.aff","dizionari_furlan-3.1-tb+fx+sm.xpi", "dictionaries/fur-IT.dic", "dictionaries/fur-IT.aff","dizionario_italiano-3.3.2-fx+sm+tb.xpi", "dictionaries/it_IT.dic", "dictionaries/it_IT.aff","eesti_keele_speller-3.2-fx+tb+sm.xpi", "dictionaries/et-EE.dic", "dictionaries/et-EE.aff",@@ -113,8 +113,8 @@ public class TestAllDictionaries2 extends LuceneTestCase {"lithuanian_spelling_check_dictionary-1.3-fx+tb+sm+fn.xpi", "dictionaries/lt.dic", "dictionaries/lt.aff","litreoir_gaelspell_do_mhozilla-4.7-tb+fx+sm+fn.xpi", "dictionaries/ga.dic", "dictionaries/ga.aff","litreoir_na_liongailise-0.03-fx+sm+tb.xpi", "dictionaries/ln-CD.dic", "dictionaries/ln-CD.aff",-//BUG! "macedonian_mk_mk_spellchecker-1.2-fn+tb+fx+sm+sb.xpi", "dictionaries/mk-MK-Cyrl.dic", "dictionaries/mk-MK-Cyrl.aff",-//BUG! "macedonian_mk_mk_spellchecker-1.2-fn+tb+fx+sm+sb.xpi", "dictionaries/mk-MK-Latn.dic", "dictionaries/mk-MK-Latn.aff",+ "macedonian_mk_mk_spellchecker-1.2-fn+tb+fx+sm+sb.xpi", "dictionaries/mk-MK-Cyrl.dic", "dictionaries/mk-MK-Cyrl.aff",+ "macedonian_mk_mk_spellchecker-1.2-fn+tb+fx+sm+sb.xpi", "dictionaries/mk-MK-Latn.dic", "dictionaries/mk-MK-Latn.aff","malagasy_spell_checker-0.3-fn+tb+fx+sm+sb.xpi", "dictionaries/mg_MG.dic", "dictionaries/mg_MG.aff","marathi_dictionary-9.3-sm+tb+sb+fx.xpi", "dictionaries/mr-IN.dic", "dictionaries/mr-IN.aff","ndebele_south_spell_checker-20110323-tb+fn+fx+sm.xpi", "dictionaries/nr-ZA.dic", "dictionaries/nr-ZA.aff",@@ -126,7 +126,7 @@ public class TestAllDictionaries2 extends LuceneTestCase {"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","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",+ "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","serbian_dictionary-0.18-fx+tb+sm.xpi", "dictionaries/sr-RS-Cyrl.dic", "dictionaries/sr-RS-Cyrl.aff",@@ -154,7 +154,7 @@ public class TestAllDictionaries2 extends LuceneTestCase {"ukrainian_dictionary-1.7.0-sm+an+fx+fn+tb.xpi", "dictionaries/uk-UA.dic", "dictionaries/uk-UA.aff","united_states_english_spellchecker-7.0.1-sm+tb+fx+an.xpi", "dictionaries/en-US.dic", "dictionaries/en-US.aff","upper_sorbian_spelling_dictionary-0.0.20060327.3-tb+fx+sm.xpi", "dictionaries/hsb.dic", "dictionaries/hsb.aff",-//BUG! "urdu_dictionary-0.64-fx+tb+sm+sb.xpi", "dictionaries/ur.dic", "dictionaries/ur.aff",+ "urdu_dictionary-0.64-fx+tb+sm+sb.xpi", "dictionaries/ur.dic", "dictionaries/ur.aff","uzbek_spell_checker-0.3-fn+tb+fx+sm+sb.xpi", "dictionaries/uz.dic", "dictionaries/uz.aff","valencian_catalan_dictionary-2.5.0-tb+fn+sm+fx.xpi", "dictionaries/ca-ES-valencia.dic", "dictionaries/ca-ES-valencia.aff","venda_spell_checker-20110323-tb+fn+fx+sm.xpi", "dictionaries/ve-ZA.dic", "dictionaries/ve-ZA.aff",@@ -196,7 +196,7 @@ public class TestAllDictionaries2 extends LuceneTestCase {}public void testOneDictionary() throws Exception {- String toTest = "woordenboek_nederlands-3.1.1-sm+tb+fx+fn.xpi";+ String toTest = "turkmen_spell_checker_dictionary-0.1.6-tb+fx+sm.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 bb81ed18629..bf7da6bf7d1 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@@ -17,6 +17,7 @@ package org.apache.lucene.analysis.hunspell;* limitations under the License.*/+import java.io.ByteArrayInputStream;import java.io.FilterInputStream;import java.io.IOException;import java.io.InputStream;@@ -24,6 +25,7 @@ import java.text.ParseException;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.CharsRef;+import org.apache.lucene.util.IOUtils;import org.apache.lucene.util.IntsRef;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.fst.Builder;@@ -178,4 +180,16 @@ public class TestDictionary extends LuceneTestCase {Dictionary.applyMappings(fst, sb);assertEquals("ghghghde", sb.toString());}++ public void testSetWithCrazyWhitespaceAndBOMs() throws Exception {+ assertEquals("UTF-8", Dictionary.getDictionaryEncoding(new ByteArrayInputStream("SET\tUTF-8\n".getBytes(IOUtils.CHARSET_UTF_8))));+ assertEquals("UTF-8", Dictionary.getDictionaryEncoding(new ByteArrayInputStream("SET\t UTF-8\n".getBytes(IOUtils.CHARSET_UTF_8))));+ assertEquals("UTF-8", Dictionary.getDictionaryEncoding(new ByteArrayInputStream("\uFEFFSET\tUTF-8\n".getBytes(IOUtils.CHARSET_UTF_8))));+ assertEquals("UTF-8", Dictionary.getDictionaryEncoding(new ByteArrayInputStream("\uFEFFSET\tUTF-8\r\n".getBytes(IOUtils.CHARSET_UTF_8))));+ }++ public void testFlagWithCrazyWhitespace() throws Exception {+ assertNotNull(Dictionary.getFlagParsingStrategy("FLAG\tUTF-8"));+ assertNotNull(Dictionary.getFlagParsingStrategy("FLAG UTF-8"));+ }}