public virtual void TestSingletonConcatenate() { Automaton singleton = BasicAutomata.MakeString("prefix"); Automaton expandedSingleton = singleton.CloneExpanded(); Automaton other = BasicAutomata.MakeCharRange('5', '7'); Automaton concat = BasicOperations.Concatenate(singleton, other); Assert.IsTrue(concat.IsDeterministic); Assert.IsTrue(BasicOperations.SameLanguage(BasicOperations.Concatenate(expandedSingleton, other), concat)); }
public virtual void TestSingletonNFAConcatenate() { Automaton singleton = BasicAutomata.MakeString("prefix"); Automaton expandedSingleton = singleton.CloneExpanded(); // an NFA (two transitions for 't' from initial state) Automaton nfa = BasicOperations.Union(BasicAutomata.MakeString("this"), BasicAutomata.MakeString("three")); Automaton concat = BasicOperations.Concatenate(singleton, nfa); Assert.IsFalse(concat.IsDeterministic); Assert.IsTrue(BasicOperations.SameLanguage(BasicOperations.Concatenate(expandedSingleton, nfa), concat)); }
public virtual void TestSingleton() { Automaton singleton = BasicAutomata.MakeString("foobar"); Automaton expandedSingleton = singleton.CloneExpanded(); Assert.IsTrue(BasicOperations.SameLanguage(singleton, expandedSingleton)); singleton = BasicAutomata.MakeString("\ud801\udc1c"); expandedSingleton = singleton.CloneExpanded(); Assert.IsTrue(BasicOperations.SameLanguage(singleton, expandedSingleton)); }
public virtual void Test() { int num = AtLeast(200); for (int i = 0; i < num; i++) { Automaton a = AutomatonTestUtil.RandomAutomaton(Random); Automaton b = (Automaton)a.Clone(); MinimizationOperations.Minimize(b); Assert.IsTrue(BasicOperations.SameLanguage(a, b)); } }
/// <summary> /// Builds a DFA for some string, and checks all Lev automata /// up to some maximum distance. /// </summary> private void AssertLev(string s, int maxDistance) { LevenshteinAutomata builder = new LevenshteinAutomata(s, false); LevenshteinAutomata tbuilder = new LevenshteinAutomata(s, true); Automaton[] automata = new Automaton[maxDistance + 1]; Automaton[] tautomata = new Automaton[maxDistance + 1]; for (int n = 0; n < automata.Length; n++) { automata[n] = builder.ToAutomaton(n); tautomata[n] = tbuilder.ToAutomaton(n); Assert.IsNotNull(automata[n]); Assert.IsNotNull(tautomata[n]); Assert.IsTrue(automata[n].Deterministic); Assert.IsTrue(tautomata[n].Deterministic); Assert.IsTrue(SpecialOperations.IsFinite(automata[n])); Assert.IsTrue(SpecialOperations.IsFinite(tautomata[n])); AutomatonTestUtil.AssertNoDetachedStates(automata[n]); AutomatonTestUtil.AssertNoDetachedStates(tautomata[n]); // check that the dfa for n-1 accepts a subset of the dfa for n if (n > 0) { Assert.IsTrue(automata[n - 1].SubsetOf(automata[n])); Assert.IsTrue(automata[n - 1].SubsetOf(tautomata[n])); Assert.IsTrue(tautomata[n - 1].SubsetOf(automata[n])); Assert.IsTrue(tautomata[n - 1].SubsetOf(tautomata[n])); Assert.AreNotSame(automata[n - 1], automata[n]); } // check that Lev(N) is a subset of LevT(N) Assert.IsTrue(automata[n].SubsetOf(tautomata[n])); // special checks for specific n switch (n) { case 0: // easy, matches the string itself Assert.IsTrue(BasicOperations.SameLanguage(BasicAutomata.MakeString(s), automata[0])); Assert.IsTrue(BasicOperations.SameLanguage(BasicAutomata.MakeString(s), tautomata[0])); break; case 1: // generate a lev1 naively, and check the accepted lang is the same. Assert.IsTrue(BasicOperations.SameLanguage(NaiveLev1(s), automata[1])); Assert.IsTrue(BasicOperations.SameLanguage(NaiveLev1T(s), tautomata[1])); break; default: AssertBruteForce(s, automata[n], n); AssertBruteForceT(s, tautomata[n], n); break; } } }
public virtual void TestStringUnion() { List <BytesRef> strings = new List <BytesRef>(); for (int i = RandomInts.RandomInt32Between(Random, 0, 1000); --i >= 0;) { strings.Add(new BytesRef(TestUtil.RandomUnicodeString(Random))); } strings.Sort(); Automaton union = BasicAutomata.MakeStringUnion(strings); Assert.IsTrue(union.IsDeterministic); Assert.IsTrue(BasicOperations.SameLanguage(union, NaiveUnion(strings))); }
public virtual void TestAgainstSimple() { int num = AtLeast(200); for (int i = 0; i < num; i++) { Automaton a = AutomatonTestUtil.RandomAutomaton(Random()); Automaton b = (Automaton)a.Clone(); AutomatonTestUtil.DeterminizeSimple(a); b.Deterministic = false; // force det b.Determinize(); // TODO: more verifications possible? Assert.IsTrue(BasicOperations.SameLanguage(a, b)); } }
public virtual void TestAgainstBrzozowski() { int num = AtLeast(200); for (int i = 0; i < num; i++) { Automaton a = AutomatonTestUtil.RandomAutomaton(Random); AutomatonTestUtil.MinimizeSimple(a); Automaton b = (Automaton)a.Clone(); MinimizationOperations.Minimize(b); Assert.IsTrue(BasicOperations.SameLanguage(a, b)); Assert.AreEqual(a.GetNumberOfStates(), b.GetNumberOfStates()); Assert.AreEqual(a.GetNumberOfTransitions(), b.GetNumberOfTransitions()); } }
public CompiledAutomaton(Automaton automaton, bool?finite, bool simplify) { if (simplify) { // Test whether the automaton is a "simple" form and // if so, don't create a runAutomaton. Note that on a // large automaton these tests could be costly: if (BasicOperations.IsEmpty(automaton)) { // matches nothing Type = AUTOMATON_TYPE.NONE; Term = null; CommonSuffixRef = null; RunAutomaton = null; sortedTransitions = null; this.Finite = null; return; } else if (BasicOperations.IsTotal(automaton)) { // matches all possible strings Type = AUTOMATON_TYPE.ALL; Term = null; CommonSuffixRef = null; RunAutomaton = null; sortedTransitions = null; this.Finite = null; return; } else { string commonPrefix; string singleton; if (automaton.Singleton == null) { commonPrefix = SpecialOperations.GetCommonPrefix(automaton); if (commonPrefix.Length > 0 && BasicOperations.SameLanguage(automaton, BasicAutomata.MakeString(commonPrefix))) { singleton = commonPrefix; } else { singleton = null; } } else { commonPrefix = null; singleton = automaton.Singleton; } if (singleton != null) { // matches a fixed string in singleton or expanded // representation Type = AUTOMATON_TYPE.SINGLE; Term = new BytesRef(singleton); CommonSuffixRef = null; RunAutomaton = null; sortedTransitions = null; this.Finite = null; return; } else if (BasicOperations.SameLanguage(automaton, BasicOperations.Concatenate(BasicAutomata.MakeString(commonPrefix), BasicAutomata.MakeAnyString()))) { // matches a constant prefix Type = AUTOMATON_TYPE.PREFIX; Term = new BytesRef(commonPrefix); CommonSuffixRef = null; RunAutomaton = null; sortedTransitions = null; this.Finite = null; return; } } } Type = AUTOMATON_TYPE.NORMAL; Term = null; if (finite == null) { this.Finite = SpecialOperations.IsFinite(automaton); } else { this.Finite = finite; } Automaton utf8 = (new UTF32ToUTF8()).Convert(automaton); if (this.Finite == true) { CommonSuffixRef = null; } else { CommonSuffixRef = SpecialOperations.GetCommonSuffixBytesRef(utf8); } RunAutomaton = new ByteRunAutomaton(utf8, true); sortedTransitions = utf8.GetSortedTransitions(); }