Apache Lucene open-source search software
Add Case Insensitive makeChar and makeString to Automata (#15753)
John Wagstere43c6e909492
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex aedb0d49601..7a68265fac0 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -209,7 +209,7 @@ New FeaturesImprovements----------------------(No changes)+* GITHUB#15753: Add Case Insensitive makeChar and makeString to Automata. (John Wagster)Optimizations---------------------@@ -863,7 +863,6 @@ Improvements* GITHUB#14239: Hunspell's option to tolerate affix rule count mismatches wasimproved to tolerate more instances of this problem. (Robert Muir)-* GITHUB#14238: Improve test coverage of Dynamic Range Faceting. (John Houser)* GITHUB#14213: Allowing indexing stored-only StoredField directly from DataInput. (Tim Brooks)diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/Automata.java b/lucene/core/src/java/org/apache/lucene/util/automaton/Automata.javaindex 6b9a7fcc556..e359b6ddb63 100644--- a/lucene/core/src/java/org/apache/lucene/util/automaton/Automata.java+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/Automata.java@@ -33,6 +33,7 @@ import java.io.IOException;import java.util.ArrayList;import java.util.Collection;import java.util.Objects;+import org.apache.lucene.internal.hppc.IntArrayList;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.BytesRefIterator;import org.apache.lucene.util.StringHelper;@@ -117,6 +118,14 @@ public final class Automata {return makeCharRange(c, c);}+ /**+ * Returns a new (deterministic and minimal) automaton that accepts potentially multiple+ * codepoints of the given value that are case-insensitive equivalents.+ */+ public static Automaton makeCaseInsensitiveChar(int c) {+ return makeCharSet(toCaseInsensitiveChar(c));+ }+/** Appends the specified character to the specified state, returning a new state. */public static int appendChar(Automaton a, int state, int c) {int newState = a.createState();@@ -557,6 +566,31 @@ public final class Automata {return a;}+ /**+ * Returns a new (deterministic and minimal) automaton that accepts the single given string and+ * the case-insensitive equivalents.+ */+ public static Automaton makeCaseInsensitiveString(String s) {+ Automaton a = new Automaton();+ int lastState = a.createState();+ for (int i = 0, cp = 0; i < s.length(); i += Character.charCount(cp)) {+ int state = a.createState();+ cp = s.codePointAt(i);+ for (int alt : toCaseInsensitiveChar(cp)) {+ a.addTransition(lastState, state, alt);+ }+ lastState = state;+ }++ a.setAccept(lastState, true);+ a.finishState();++ assert a.isDeterministic();+ assert Operations.hasDeadStates(a) == false;++ return a;+ }+/** Returns a new (deterministic) automaton that accepts the single given binary term. */public static Automaton makeBinary(BytesRef term) {Automaton a = new Automaton();@@ -653,4 +687,24 @@ public final class Automata {public static Automaton makeBinaryStringUnion(BytesRefIterator utf8Strings) throws IOException {return StringsToAutomaton.build(utf8Strings, true);}++ /**+ * This function handles uses the Unicode spec for generating case-insensitive alternates.+ *+ * <p>See the {@link RegExp#CASE_INSENSITIVE} flag for details on case folding within the Unicode+ * spec.+ *+ * @param codepoint the Character code point to encode as an Automaton+ * @return the original codepoint and the set of alternates+ */+ private static int[] toCaseInsensitiveChar(int codepoint) {+ IntArrayList list = new IntArrayList();+ CaseFolding.expand(+ codepoint,+ (int variant) -> {+ list.add(variant);+ });+ list.sort();+ return list.toArray();+ }}diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.java b/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.javaindex 0d2e78ee167..ce4d2d2d951 100644--- a/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.java+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.java@@ -32,7 +32,6 @@ package org.apache.lucene.util.automaton;import java.io.IOException;import java.util.ArrayList;import java.util.HashSet;-import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;@@ -682,7 +681,7 @@ public class RegExp {break;case REGEXP_CHAR:if (check(ASCII_CASE_INSENSITIVE | CASE_INSENSITIVE)) {- a = Automata.makeCharSet(toCaseInsensitiveChar(c));+ a = Automata.makeCaseInsensitiveChar(c);} else {a = Automata.makeChar(c);}@@ -701,7 +700,7 @@ public class RegExp {break;case REGEXP_STRING:if (check(ASCII_CASE_INSENSITIVE | CASE_INSENSITIVE)) {- a = toCaseInsensitiveString();+ a = Automata.makeCaseInsensitiveString(s);} else {a = Automata.makeString(s);}@@ -791,17 +790,6 @@ public class RegExp {}}- private Automaton toCaseInsensitiveString() {- List<Automaton> list = new ArrayList<>();-- Iterator<Integer> iter = s.codePoints().iterator();- while (iter.hasNext()) {- int[] points = toCaseInsensitiveChar(iter.next());- list.add(Automata.makeCharSet(points));- }- return Operations.concatenate(list);- }-private void findLeaves(RegExp exp,Kind kind,diff --git a/lucene/core/src/test/org/apache/lucene/util/automaton/TestAutomaton.java b/lucene/core/src/test/org/apache/lucene/util/automaton/TestAutomaton.javaindex f713414fe05..e6b73a1c62a 100644--- a/lucene/core/src/test/org/apache/lucene/util/automaton/TestAutomaton.java+++ b/lucene/core/src/test/org/apache/lucene/util/automaton/TestAutomaton.java@@ -1728,4 +1728,47 @@ public class TestAutomaton extends LuceneTestCase {AutomatonTestUtil.assertMinimalDFA(actual);assertTrue(AutomatonTestUtil.sameLanguage(expected, actual));}++ public void testMakeCharCaseInsensitive() {+ Automaton a = Automata.makeCaseInsensitiveChar('a');+ assertTrue(Operations.run(a, "a"));+ assertTrue(Operations.run(a, "A"));+ assertFalse(Operations.run(a, "b"));++ a = Automata.makeCaseInsensitiveChar('A');+ assertTrue(Operations.run(a, "a"));+ assertTrue(Operations.run(a, "A"));+ assertFalse(Operations.run(a, "b"));++ a = Automata.makeCaseInsensitiveChar('Σ');+ assertTrue(Operations.run(a, "Σ"));+ assertTrue(Operations.run(a, "σ"));+ assertTrue(Operations.run(a, "ς"));++ // German sharp S: 'ß' (U+00DF) and 'ẞ' (U+1E9E)+ a = Automata.makeCaseInsensitiveChar(223);+ assertTrue(Operations.run(a, Character.toString(223)));+ assertTrue(Operations.run(a, Character.toString(7838)));++ AutomatonTestUtil.assertMinimalDFA(a);+ AutomatonTestUtil.assertNoDetachedStates(a);+ assertTrue(AutomatonTestUtil.isFinite(a));+ }++ public void testMakeStringCaseInsensitiveSimple() {+ Automaton a = Automata.makeCaseInsensitiveString("abc");+ assertTrue(Operations.run(a, "abc"));+ assertTrue(Operations.run(a, "AbC"));+ assertFalse(Operations.run(a, "ab"));+ assertFalse(Operations.run(a, "xyz"));++ a = Automata.makeCaseInsensitiveString("Σίγμα");+ assertTrue(Operations.run(a, "Σίγμα"));+ assertTrue(Operations.run(a, "σίγμα"));+ assertTrue(Operations.run(a, "ςίγμα"));++ AutomatonTestUtil.assertMinimalDFA(a);+ AutomatonTestUtil.assertNoDetachedStates(a);+ assertTrue(AutomatonTestUtil.isFinite(a));+ }}