dweiss commented on code in PR #11893: URL: https://github.com/apache/lucene/pull/11893#discussion_r1013880109
########## lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Suggester.java: ########## @@ -0,0 +1,221 @@ +/* + * 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. + */ +package org.apache.lucene.analysis.hunspell; + +import static org.apache.lucene.analysis.hunspell.Dictionary.FLAG_UNSET; +import static org.apache.lucene.analysis.hunspell.TimeoutPolicy.NO_TIMEOUT; +import static org.apache.lucene.analysis.hunspell.WordContext.COMPOUND_BEGIN; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import org.apache.lucene.util.CharsRef; + +/** + * A generator for misspelled word corrections based on Hunspell flags. The suggestions are searched + * for in two main ways: + * + * <ol> + * <li>Modification: trying to insert/remove/delete/swap parts of the word to get something + * acceptable. The performance of this part depends heavily on the contents of TRY, MAP, REP, + * KEY directives in the .aff file. + * <li>Enumeration: if the modification hasn't produced "good enough" suggestions, the whole + * dictionary is scanned and simple affixes are added onto the entries to check if that + * produces anything similar to the given misspelled word. This depends on the dictionary size + * and the affix count, and it can take noticeable amount of time. To speed this up, {@link + * #withSuggestibleEntryCache()} can be used. + * </ol> + */ +public class Suggester { + private final Dictionary dictionary; + private final SuggestibleEntryCache suggestibleCache; + + public Suggester(Dictionary dictionary) { + this(dictionary, null); + } + + private Suggester(Dictionary dictionary, SuggestibleEntryCache suggestibleCache) { + this.dictionary = dictionary; + this.suggestibleCache = suggestibleCache; + } + + /** + * Returns a copy of this suggester instance with better "Enumeration" phase performance (see + * {@link Suggester} documentation), but using more memory. With this option, the dictionary + * entries are stored as fast-to-iterate plain words instead of highly compressed prefix trees. + */ + public Suggester withSuggestibleEntryCache() { Review Comment: I like the Suggester as a separate entity but this bit seems a bit awkward to me - it's a chicken and egg (constructor accepting SuggestibleEntryCache, but SuggestibleEntryCache is created via non-static method on the suggester). If the cache's lifecycle can be longer than the Suggester's then perhaps it should just be left as an explicit argument for a public constructor, without the static factory/ modifier method on the Suggester? Then folks who need the cache can compile it (for the same dictionary) and pass it to one or more suggesters, however they like. Which also makes me think that buildCache should accept the dictionary reference and then a sanity-check could be added in the suggester to make sure the cache and the suggester's dictionary are the same... Or even modify the constructor to accept just the entry cache, which would piggyback the dictionary reference consistently. Finally - I'm not a native speaker, but wouldn't it be simpler to call the cache just an EntryCache (or a SuggestionsCache)? Suggestible sounds a bit awkward to my (slavic) ear, but maybe it's just me. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org