romseygeek commented on a change in pull request #1467: URL: https://github.com/apache/lucene-solr/pull/1467#discussion_r417810173
########## File path: lucene/core/src/java/org/apache/lucene/search/FuzzyAutomatonBuilder.java ########## @@ -0,0 +1,88 @@ +/* + * 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.search; + +import org.apache.lucene.util.UnicodeUtil; +import org.apache.lucene.util.automaton.CompiledAutomaton; +import org.apache.lucene.util.automaton.LevenshteinAutomata; +import org.apache.lucene.util.automaton.TooComplexToDeterminizeException; + +/** + * Builds a set of CompiledAutomaton for fuzzy matching on a given term, + * with specified maximum edit distance, fixed prefix and whether or not + * to allow transpositions. + */ +class FuzzyAutomatonBuilder { + + private final String term; + private final int maxEdits; + private final LevenshteinAutomata levBuilder; + private final String prefix; + private final int termLength; + + FuzzyAutomatonBuilder(String term, int maxEdits, int prefixLength, boolean transpositions) { + if (maxEdits < 0 || maxEdits > LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE) { + throw new IllegalArgumentException("max edits must be 0.." + LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE + ", inclusive; got: " + maxEdits); + } + if (prefixLength < 0) { + throw new IllegalArgumentException("prefixLength cannot be less than 0"); + } + this.term = term; + this.maxEdits = maxEdits; + int[] codePoints = stringToUTF32(term); + this.termLength = codePoints.length; + prefixLength = Math.min(prefixLength, codePoints.length); + int[] suffix = new int[codePoints.length - prefixLength]; + System.arraycopy(codePoints, prefixLength, suffix, 0, suffix.length); + this.levBuilder = new LevenshteinAutomata(suffix, Character.MAX_CODE_POINT, transpositions); + this.prefix = UnicodeUtil.newString(codePoints, 0, prefixLength); + } + + CompiledAutomaton[] buildAutomatonSet() { + CompiledAutomaton[] compiled = new CompiledAutomaton[maxEdits + 1]; + for (int i = 0; i <= maxEdits; i++) { + try { + compiled[i] = new CompiledAutomaton(levBuilder.toAutomaton(i, prefix), true, false); + } + catch (TooComplexToDeterminizeException e) { + throw new FuzzyTermsEnum.FuzzyTermsException(term, e); + } + } + return compiled; + } + + CompiledAutomaton buildMaxEditAutomaton() { Review comment: Just the max is useful for highlighting or for other fuzzy searches that don't do the TopTerms pruning (we use it for a fuzzy interval query in elasticsearch, for example) ---------------------------------------------------------------- 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. 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