Apache Solr open-source search software
LUCENE-5468: make Affix fixed-width
Robert Muird7cc40858536
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell2/Affix.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell2/Affix.javaindex 47a81480d6c..eb67f60e763 100644--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell2/Affix.java+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell2/Affix.java@@ -17,38 +17,23 @@ package org.apache.lucene.analysis.hunspell2;* limitations under the License.*/-import java.util.regex.Pattern;-/*** Wrapper class representing a hunspell affix*/final class Affix {- private char appendFlags[]; // continuation class flags- private String strip;-- private Pattern conditionPattern;-- private char flag;-+ private int appendFlags; // continuation class flags+ private int condition; // check conditionprivate boolean crossProduct;-- /**- * Checks whether the given text matches the conditional pattern on this affix- *- * @param text Text to check if it matches the affix's conditional pattern- * @return {@code true} if the text meets the condition, {@code false} otherwise- */- public boolean checkCondition(CharSequence text) {- return conditionPattern.matcher(text).matches();- }+ private char flag;+ private int strip;/*** Returns the flags defined for the affix append** @return Flags defined for the affix append*/- public char[] getAppendFlags() {+ public int getAppendFlags() {return appendFlags;}@@ -57,7 +42,7 @@ final class Affix {** @param appendFlags Flags defined for the affix append*/- public void setAppendFlags(char[] appendFlags) {+ public void setAppendFlags(int appendFlags) {this.appendFlags = appendFlags;}@@ -66,7 +51,7 @@ final class Affix {** @return Stripping characters defined for the affix*/- public String getStrip() {+ public int getStrip() {return strip;}@@ -75,17 +60,19 @@ final class Affix {** @param strip Stripping characters defined for the affix*/- public void setStrip(String strip) {+ public void setStrip(int strip) {this.strip = strip;}/*** Sets the condition that must be met before the affix can be applied- *- * @param pattern Condition as a regular expression pattern*/- public void setCondition(Pattern pattern) {- this.conditionPattern = pattern;+ public void setCondition(int condition) {+ this.condition = condition;+ }++ public int getCondition() {+ return condition;}/**diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell2/Dictionary.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell2/Dictionary.javaindex 713bc92210c..35c7aee6081 100644--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell2/Dictionary.java+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell2/Dictionary.java@@ -81,6 +81,9 @@ public class Dictionary {// the list of unique flagsets (wordforms). theoretically huge, but practically// small (e.g. for polish this is 756), otherwise humans wouldn't be able to deal with it either.public BytesRefHash flagLookup = new BytesRefHash();++ // the list of unique strip affixes.+ public BytesRefHash stripLookup = new BytesRefHash();private FlagParsingStrategy flagParsingStrategy = new SimpleFlagParsingStrategy(); // Default flag parsing strategy@@ -107,6 +110,7 @@ public class Dictionary {CharsetDecoder decoder = getJavaEncoding(encoding);readAffixFile(buffered, decoder);flagLookup.add(new BytesRef()); // no flags -> ord 0+ stripLookup.add(new BytesRef()); // no strip -> ord 0PositiveIntOutputs o = PositiveIntOutputs.getSingleton();Builder<Long> b = new Builder<Long>(FST.INPUT_TYPE.BYTE4, o);readDictionaryFile(dictionary, decoder, b);@@ -226,6 +230,8 @@ public class Dictionary {LineNumberReader reader,String conditionPattern,Map<String,Integer> seenPatterns) throws IOException, ParseException {++ BytesRef scratch = new BytesRef();String args[] = header.split("\\s+");boolean crossProduct = args[2].equals("Y");@@ -239,25 +245,23 @@ public class Dictionary {throw new ParseException("The affix file contains a rule with less than five elements", reader.getLineNumber());}- Affix affix = new Affix();- affix.setFlag(flagParsingStrategy.parseFlag(ruleArgs[1]));- affix.setStrip(ruleArgs[2].equals("0") ? "" : ruleArgs[2]);-+ char flag = flagParsingStrategy.parseFlag(ruleArgs[1]);+ String strip = ruleArgs[2].equals("0") ? "" : ruleArgs[2];String affixArg = ruleArgs[3];+ char appendFlags[] = null;int flagSep = affixArg.lastIndexOf('/');if (flagSep != -1) {String flagPart = affixArg.substring(flagSep + 1);-+ affixArg = affixArg.substring(0, flagSep);+if (aliasCount > 0) {flagPart = getAliasValue(Integer.parseInt(flagPart));}- char appendFlags[] = flagParsingStrategy.parseFlags(flagPart);+ appendFlags = flagParsingStrategy.parseFlags(flagPart);Arrays.sort(appendFlags);- affix.setAppendFlags(appendFlags);- affixArg = affixArg.substring(0, flagSep);}String condition = ruleArgs[4];@@ -269,8 +273,10 @@ public class Dictionary {if (condition.indexOf('-') >= 0) {condition = condition.replace("-", "\\-");}- // deduplicate patterns+String regex = String.format(Locale.ROOT, conditionPattern, condition);++ // deduplicate patternsInteger patternIndex = seenPatterns.get(regex);if (patternIndex == null) {patternIndex = patterns.size();@@ -278,8 +284,29 @@ public class Dictionary {Pattern pattern = Pattern.compile(regex);patterns.add(pattern);}- affix.setCondition(patterns.get(patternIndex));++ Affix affix = new Affix();+ scratch.copyChars(strip);+ int ord = stripLookup.add(scratch);+ if (ord < 0) {+ // already exists in our hash+ ord = (-ord)-1;+ }+ affix.setStrip(ord);+ affix.setFlag(flag);+ affix.setCondition(patternIndex);affix.setCrossProduct(crossProduct);+ if (appendFlags == null) {+ appendFlags = NOFLAGS;+ }++ final int hashCode = encodeFlagsWithHash(scratch, appendFlags);+ ord = flagLookup.add(scratch, hashCode);+ if (ord < 0) {+ // already exists in our hash+ ord = (-ord)-1;+ }+ affix.setAppendFlags(ord);List<Affix> list = affixes.get(affixArg);if (list == null) {diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell2/Stemmer.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell2/Stemmer.javaindex 7919ad56be7..b2057c501b2 100644--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell2/Stemmer.java+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell2/Stemmer.java@@ -21,6 +21,7 @@ import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;+import java.util.regex.Pattern;import org.apache.lucene.analysis.util.CharArraySet;import org.apache.lucene.util.BytesRef;@@ -79,7 +80,7 @@ final class Stemmer {if (dictionary.lookupWord(word, 0, length, scratch) != null) {stems.add(new CharsRef(word, 0, length));}- stems.addAll(stem(word, length, null, 0));+ stems.addAll(stem(word, length, Dictionary.NOFLAGS, 0));return stems;}@@ -96,7 +97,7 @@ final class Stemmer {stems.add(new CharsRef(word, 0, length));terms.add(word);}- List<CharsRef> otherStems = stem(word, length, null, 0);+ List<CharsRef> otherStems = stem(word, length, Dictionary.NOFLAGS, 0);for (CharsRef s : otherStems) {if (!terms.contains(s)) {stems.add(s);@@ -117,7 +118,9 @@ final class Stemmer {* @return List of stems, or empty list if no stems are found*/private List<CharsRef> stem(char word[], int length, char[] flags, int recursionDepth) {+ // TODO: allow this stuff to be reused by tokenfilterList<CharsRef> stems = new ArrayList<CharsRef>();+ BytesRef scratch = new BytesRef();for (int i = 0; i < length; i++) {List<Affix> suffixes = dictionary.lookupSuffix(word, i, length - i);@@ -130,7 +133,8 @@ final class Stemmer {int appendLength = length - i;int deAffixedLength = length - appendLength;// TODO: can we do this in-place?- String strippedWord = new StringBuilder().append(word, 0, deAffixedLength).append(suffix.getStrip()).toString();+ dictionary.stripLookup.get(suffix.getStrip(), scratch);+ String strippedWord = new StringBuilder().append(word, 0, deAffixedLength).append(scratch.utf8ToString()).toString();List<CharsRef> stemList = applyAffix(strippedWord.toCharArray(), strippedWord.length(), suffix, recursionDepth);@@ -150,7 +154,8 @@ final class Stemmer {int deAffixedStart = i;int deAffixedLength = length - deAffixedStart;- String strippedWord = new StringBuilder().append(prefix.getStrip())+ dictionary.stripLookup.get(prefix.getStrip(), scratch);+ String strippedWord = new StringBuilder().append(scratch.utf8ToString()).append(word, deAffixedStart, deAffixedLength).toString();@@ -175,7 +180,9 @@ final class Stemmer {public List<CharsRef> applyAffix(char strippedWord[], int length, Affix affix, int recursionDepth) {segment.setLength(0);segment.append(strippedWord, 0, length);- if (!affix.checkCondition(segment)) {++ Pattern pattern = dictionary.patterns.get(affix.getCondition());+ if (!pattern.matcher(segment).matches()) {return Collections.emptyList();}@@ -187,7 +194,10 @@ final class Stemmer {}if (affix.isCrossProduct() && recursionDepth < recursionCap) {- stems.addAll(stem(strippedWord, length, affix.getAppendFlags(), ++recursionDepth));+ BytesRef scratch = new BytesRef();+ dictionary.flagLookup.get(affix.getAppendFlags(), scratch);+ char appendFlags[] = Dictionary.decodeFlags(scratch);+ stems.addAll(stem(strippedWord, length, appendFlags, ++recursionDepth));}return stems;@@ -201,6 +211,6 @@ final class Stemmer {* @return {@code true} if the flag is found in the array or the array is {@code null}, {@code false} otherwise*/private boolean hasCrossCheckedFlag(char flag, char[] flags) {- return flags == null || Arrays.binarySearch(flags, flag) >= 0;+ return flags.length == 0 || Arrays.binarySearch(flags, flag) >= 0;}}