Skip to content

Commit

Permalink
feat(WordBreakSpellChecker): Override spellcheck's SuggestMode by Wor…
Browse files Browse the repository at this point in the history
…dBreakSolrSpellChecker

Now `suggestMode` can be specified for WordBreakSolrSpellChecker, if this value is not null, it will

override the `suggestMode` of SpellCheckComponent.

Fixes Performance and Relevancy
  • Loading branch information
abhidemon committed Mar 28, 2017
1 parent 390ef9a commit 484e3ee
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Arrays;


import java.util.regex.Pattern;

import org.apache.lucene.analysis.Token;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.spell.CombineSuggestion;
import org.apache.lucene.search.spell.SuggestMode;
import org.apache.lucene.search.spell.SuggestWord;
import org.apache.lucene.search.spell.WordBreakSpellChecker;
import org.apache.lucene.search.spell.WordBreakSpellChecker.BreakSuggestionSortMethod;
Expand Down Expand Up @@ -90,7 +94,10 @@ public class WordBreakSolrSpellChecker extends SolrSpellChecker {
* See {@link WordBreakSpellChecker#setMinSuggestionFrequency}
*/
public static final String PARAM_MIN_SUGGESTION_FREQUENCY = "minSuggestionFreq";


public static final String PARAM_SUGGEST_MODE = "suggestMode";


/**
* <p>
* Specify a value on the "breakSugestionTieBreaker" parameter.
Expand All @@ -114,6 +121,7 @@ public enum BreakSuggestionTieBreaker {
private WordBreakSpellChecker wbsp = null;
private boolean combineWords = false;
private boolean breakWords = false;
private SuggestMode suggestMode = null;
private BreakSuggestionSortMethod sortMethod = BreakSuggestionSortMethod.NUM_CHANGES_THEN_MAX_FREQUENCY;
private static final Pattern spacePattern = Pattern.compile("\\s+");

Expand All @@ -123,6 +131,16 @@ public String init(@SuppressWarnings("unchecked") NamedList config,
String name = super.init(config, core);
combineWords = boolParam(config, PARAM_COMBINE_WORDS);
breakWords = boolParam(config, PARAM_BREAK_WORDS);
if (config.get(PARAM_SUGGEST_MODE)!=null){
try{
suggestMode = SuggestMode.valueOf(strParam(config,PARAM_SUGGEST_MODE));
} catch (IllegalArgumentException iae){
String errMsg = "Error initialising " + this.getClass().getName()
+ ". Invalid `suggestMode` : '" + strParam(config, PARAM_SUGGEST_MODE)
+ "' . Permitted Values: " + Arrays.asList(SuggestMode.values());
throw new IllegalArgumentException(errMsg);
}
}
wbsp = new WordBreakSpellChecker();
String bstb = strParam(config, PARAM_BREAK_SUGGESTION_TIE_BREAKER);
if (bstb != null) {
Expand Down Expand Up @@ -195,7 +213,9 @@ public SpellingResult getSuggestions(SpellingOptions options)
throws IOException {
IndexReader ir = options.reader;
int numSuggestions = options.count;

if ( suggestMode == null ){
suggestMode = options.suggestMode;
}
StringBuilder sb = new StringBuilder();
Token[] tokenArr = options.tokens.toArray(new Token[options.tokens.size()]);
List<Token> tokenArrWithSeparators = new ArrayList<>(options.tokens.size() + 2);
Expand Down Expand Up @@ -229,7 +249,7 @@ public SpellingResult getSuggestions(SpellingOptions options)
tokenArrWithSeparators.add(tokenArr[i]);
if (breakWords) {
SuggestWord[][] breakSuggestions = wbsp.suggestWordBreaks(thisTerm,
numSuggestions, ir, options.suggestMode, sortMethod);
numSuggestions, ir, suggestMode, sortMethod);
if(breakSuggestions.length==0) {
noBreakSuggestionList.add(new ResultEntry(tokenArr[i], null, 0));
}
Expand Down Expand Up @@ -258,7 +278,7 @@ public SpellingResult getSuggestions(SpellingOptions options)

List<ResultEntry> combineSuggestionList = Collections.emptyList();
CombineSuggestion[] combineSuggestions = wbsp.suggestWordCombinations(
termArr.toArray(new Term[termArr.size()]), numSuggestions, ir, options.suggestMode);
termArr.toArray(new Term[termArr.size()]), numSuggestions, ir, suggestMode);
if (combineWords) {
combineSuggestionList = new ArrayList<>(
combineSuggestions.length);
Expand Down

0 comments on commit 484e3ee

Please sign in to comment.