Exemplo n.º 1
0
        public void ZeroDetectionWithEpsilonLoop1()
        {
            var builder = new StringAutomaton.Builder();

            AddEpsilonLoop(builder.Start, 5, 0);

            var f = builder.GetAutomaton();

            Assert.False(f.IsCanonicZero());
            Assert.True(f.IsZero());
        }
Exemplo n.º 2
0
        public void ConvertToStringWithLoops4()
        {
            var builder = new StringAutomaton.Builder();

            builder.Start.AddTransitionsForSequence("xyz", builder.Start.Index);
            builder.Start.AddTransition('!', Weight.One).SetEndWeight(Weight.One);
            var automaton = builder.GetAutomaton();

            Assert.Equal("(xyz)*!", automaton.ToString(AutomatonFormats.Friendly));
            Assert.Equal("(xyz)*?!", automaton.ToString(AutomatonFormats.Regexp));
        }
Exemplo n.º 3
0
        public void ConvertToStringWithDeadTransitions2()
        {
            var builder = new StringAutomaton.Builder();

            builder.Start.AddSelfTransition('x', Weight.Zero);
            builder.Start.AddTransition('y', Weight.Zero).SetEndWeight(Weight.One);

            var automaton = builder.GetAutomaton();

            Assert.Equal("Ø", automaton.ToString(AutomatonFormats.Friendly));
            Assert.Equal("Ø", automaton.ToString(AutomatonFormats.Regexp));
        }
Exemplo n.º 4
0
        public void ZeroDetectionWithEpsilonLoop2()
        {
            var builder = new StringAutomaton.Builder();

            AddEpsilonLoop(builder.Start, 5, 2.0);
            builder.Start.AddTransition('a', Weight.One);

            var f = builder.GetAutomaton();

            Assert.False(f.IsCanonicZero());
            Assert.True(f.IsZero());
        }
Exemplo n.º 5
0
        public void NormalizeWithInfiniteEpsilon1()
        {
            var builder = new StringAutomaton.Builder();

            builder.Start.AddTransition('a', Weight.One).AddSelfTransition(Option.None, Weight.FromValue(3)).SetEndWeight(Weight.One);

            var automaton = builder.GetAutomaton();

            // The automaton takes an infinite value on "a", and yet the normalization must work
            Assert.True(automaton.TryNormalizeValues());
            StringInferenceTestUtilities.TestValue(automaton, 1, "a");
            StringInferenceTestUtilities.TestValue(automaton, 0, "b");
        }
Exemplo n.º 6
0
        public static StringDistribution EmptyOrStartsWith(ImmutableDiscreteChar charsInMainString, ImmutableDiscreteChar startsWith)
        {
            // TODO: fix equality and then use factory methods to create this
            var result = new StringAutomaton.Builder();

            result.Start.SetEndWeight(Weight.One);
            var otherState = result.Start.AddTransition(startsWith, Weight.FromLogValue(-startsWith.GetLogAverageOf(startsWith)));

            otherState.AddSelfTransition(charsInMainString, Weight.FromLogValue(-charsInMainString.GetLogAverageOf(charsInMainString)));
            otherState.SetEndWeight(Weight.One);

            return(StringDistribution.FromWeightFunction(result.GetAutomaton()));
        }
Exemplo n.º 7
0
        public void ComputeNormalizerSimple1()
        {
            var builder = new StringAutomaton.Builder();

            builder.Start.AddSelfTransition('a', Weight.FromValue(0.7));
            builder.Start.SetEndWeight(Weight.FromValue(0.3));
            var automaton = builder.GetAutomaton();

            AssertStochastic(automaton);
            Assert.Equal(0.0, automaton.GetLogNormalizer(), 1e-6);
            Assert.Equal(0.0, GetLogNormalizerByGetValue(automaton), 1e-6);
            Assert.Equal(0.0, GetLogNormalizerByGetValueWithTransducers(automaton), 1e-6);
        }
Exemplo n.º 8
0
        public void ConvertToStringWithLoops2()
        {
            var builder = new StringAutomaton.Builder();

            builder.Start.AddSelfTransition('a', Weight.One);
            builder.Start.AddSelfTransition('b', Weight.One);
            builder.Start.SetEndWeight(Weight.One);

            var automaton = builder.GetAutomaton();

            Assert.Equal("(a|b)*", automaton.ToString(AutomatonFormats.Friendly));
            Assert.Equal("(a|b)*?", automaton.ToString(AutomatonFormats.Regexp));
        }
Exemplo n.º 9
0
        public void ConvertToStringWithLoops1()
        {
            var builder    = new StringAutomaton.Builder();
            var middleNode = builder.Start.AddTransition('a', Weight.One);

            middleNode.AddTransitionsForSequence("bbb", builder.Start.Index);
            middleNode.AddTransition('c', Weight.One, builder.Start.Index);
            builder.Start.SetEndWeight(Weight.One);

            var automaton = builder.GetAutomaton();

            Assert.Equal("(a(c|bbb))*", automaton.ToString(AutomatonFormats.Friendly));
            Assert.Equal("(a(c|bbb))*?", automaton.ToString(AutomatonFormats.Regexp));
        }
Exemplo n.º 10
0
        public void NormalizeWithInfiniteEpsilon2()
        {
            var builder = new StringAutomaton.Builder();

            builder.Start.AddTransition('a', Weight.One).AddSelfTransition(Option.None, Weight.FromValue(2)).SetEndWeight(Weight.One);
            builder.Start.AddTransition('b', Weight.One).AddSelfTransition(Option.None, Weight.FromValue(1)).SetEndWeight(Weight.One);

            var automaton = builder.GetAutomaton();

            // "a" branch infinitely dominates over the "b" branch
            Assert.True(automaton.TryNormalizeValues());
            StringInferenceTestUtilities.TestValue(automaton, 1, "a");
            Assert.True(automaton.GetValue("b") < 1e-50);
        }
Exemplo n.º 11
0
        public void NonNormalizableLoop3()
        {
            var builder = new StringAutomaton.Builder();

            builder.Start.AddTransition('a', Weight.FromValue(2.0), builder.Start.Index);
            builder.Start.SetEndWeight(Weight.FromValue(5.0));

            var automaton = builder.GetAutomaton();

            StringAutomaton copyOfAutomaton = automaton.Clone();

            Assert.Throws <InvalidOperationException>(() => automaton.NormalizeValues());
            Assert.False(copyOfAutomaton.TryNormalizeValues());
            ////Assert.Equal(f, copyOfF); // TODO: fix equality first
        }
Exemplo n.º 12
0
        public void AutomatonNormalizationPerformance1()
        {
            AssertTimeout(() =>
            {
                var builder   = new StringAutomaton.Builder();
                var nextState = builder.Start.AddTransitionsForSequence("abc");
                nextState.AddSelfTransition('d', Weight.FromValue(0.1));
                nextState.AddTransitionsForSequence("efg").SetEndWeight(Weight.One);
                nextState.AddTransitionsForSequence("hejfhoenmf").SetEndWeight(Weight.One);

                var automaton = builder.GetAutomaton();

                ProfileAction(() => automaton.GetLogNormalizer(), 100000);
            }, 10000);
        }
Exemplo n.º 13
0
        public void ConvertToStringWithLoops3()
        {
            var builder = new StringAutomaton.Builder();
            var state   = builder.Start.AddTransition('x', Weight.One);

            builder.Start.AddTransition('y', Weight.One, state.Index);
            state.AddSelfTransition('a', Weight.One);
            state.AddSelfTransition('b', Weight.One);
            state.SetEndWeight(Weight.One);
            state.AddTransitionsForSequence("zzz").SetEndWeight(Weight.One);

            var automaton = builder.GetAutomaton();

            Assert.Equal("(x|y)(a|b)*[zzz]", automaton.ToString(AutomatonFormats.Friendly));
            Assert.Equal("(x|y)(a|b)*(|zzz)", automaton.ToString(AutomatonFormats.Regexp));
        }
Exemplo n.º 14
0
        public void ComputeNormalizerWithNonTrivialLoop2()
        {
            var builder = new StringAutomaton.Builder();

            var endState = builder.Start.AddTransition('a', Weight.FromValue(2.0));

            endState.SetEndWeight(Weight.FromValue(5.0));
            endState.AddTransition('b', Weight.FromValue(0.25), builder.Start.Index);
            endState.AddTransition('c', Weight.FromValue(0.2), builder.Start.Index);

            var automaton = builder.GetAutomaton();

            Assert.Equal(Math.Log(100.0), automaton.GetLogNormalizer(), 1e-6);
            Assert.Equal(Math.Log(100.0), GetLogNormalizerByGetValue(automaton), 1e-6);
            Assert.Equal(Math.Log(100.0), GetLogNormalizerByGetValueWithTransducers(automaton), 1e-6);
        }
Exemplo n.º 15
0
        public void NonNormalizableLoop1()
        {
            var builder = new StringAutomaton.Builder();

            var endState = builder.Start.AddTransition('a', Weight.FromValue(3.5));

            endState.SetEndWeight(Weight.FromValue(5.0));
            endState.AddTransition('b', Weight.FromValue(0.1), builder.Start.Index);
            endState.AddTransition('c', Weight.FromValue(0.05), builder.Start.Index);
            endState.AddSelfTransition('!', Weight.FromValue(0.5));

            var automaton = builder.GetAutomaton();

            StringAutomaton copyOfAutomaton = automaton.Clone();

            Assert.Throws <InvalidOperationException>(() => copyOfAutomaton.NormalizeValues());
            Assert.False(copyOfAutomaton.TryNormalizeValues(out var _));
            ////Assert.Equal(f, copyOfF); // TODO: fix equality first
        }
Exemplo n.º 16
0
        public void ComputeNormalizerWithNonTrivialLoop1()
        {
            var builder = new StringAutomaton.Builder();

            var state = builder.Start.AddTransition('a', Weight.FromValue(0.9));

            state.AddTransition('a', Weight.FromValue(0.1)).SetEndWeight(Weight.One);
            state = state.AddTransition('a', Weight.FromValue(0.9));
            state.AddTransition('a', Weight.FromValue(0.1)).SetEndWeight(Weight.One);
            state = state.AddTransition('a', Weight.FromValue(0.9), builder.Start.Index);
            state.AddTransition('a', Weight.FromValue(0.1)).SetEndWeight(Weight.One);

            var automaton = builder.GetAutomaton();

            AssertStochastic(automaton);
            Assert.Equal(0.0, automaton.GetLogNormalizer(), 1e-6);
            Assert.Equal(0.0, GetLogNormalizerByGetValue(automaton), 1e-6);
            Assert.Equal(0.0, GetLogNormalizerByGetValueWithTransducers(automaton), 1e-6);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Creates a uniform distribution over any string starting and ending with a non-word character.
        /// Characters other than the first and the last are restricted to be non-zero probability characters
        /// from a given distribution.
        /// </summary>
        /// <param name="allowedChars">The distribution representing allowed characters.</param>
        /// <param name="nonWordCharacter">The word separating characters.</param>
        /// <returns>The created distribution.</returns>
        public static StringDistribution WordMiddle(ImmutableDiscreteChar allowedChars, ImmutableDiscreteChar?nonWordCharacter = null)
        {
            // TODO: fix equality and then use factory methods to create this
            nonWordCharacter = nonWordCharacter ?? NonWordCharacter;
            var result      = new StringAutomaton.Builder();
            var otherState1 = result.Start.AddTransition(
                Option.FromNullable(nonWordCharacter),
                Weight.FromLogValue(-nonWordCharacter.Value.GetLogAverageOf(nonWordCharacter.Value)));

            otherState1.SetEndWeight(Weight.One);
            var otherState2 = otherState1.AddEpsilonTransition(Weight.One)
                              .AddSelfTransition(allowedChars, Weight.FromLogValue(-allowedChars.GetLogAverageOf(allowedChars))).AddTransition(
                Option.FromNullable(nonWordCharacter),
                Weight.FromLogValue(-nonWordCharacter.Value.GetLogAverageOf(nonWordCharacter.Value)));

            otherState2.SetEndWeight(Weight.One);

            return(StringDistribution.FromWeightFunction(result.GetAutomaton()));
        }
Exemplo n.º 18
0
        public void AutomatonNormalizationPerformance3()
        {
            AssertTimeout(() =>
            {
                var builder = new StringAutomaton.Builder();
                builder.Start.AddSelfTransition('a', Weight.FromValue(0.5));
                builder.Start.SetEndWeight(Weight.One);
                var nextState = builder.Start.AddTransitionsForSequence("aa");
                nextState.AddSelfTransition('a', Weight.FromValue(0.5));
                nextState.SetEndWeight(Weight.One);

                var automaton = builder.GetAutomaton();
                for (int i = 0; i < 3; ++i)
                {
                    automaton = automaton.Product(automaton);
                }

                ProfileAction(() => automaton.GetLogNormalizer(), 100);
            }, 120000);
        }
Exemplo n.º 19
0
        public void LoopyArithmetic()
        {
            var builder1 = new StringAutomaton.Builder();

            builder1.Start
            .AddTransition('a', Weight.FromValue(4.0))
            .AddTransition('b', Weight.One, builder1.Start.Index)
            .SetEndWeight(Weight.One);
            var automaton1 = builder1.GetAutomaton();

            var builder2 = new StringAutomaton.Builder();

            builder2.Start
            .AddSelfTransition('a', Weight.FromValue(2))
            .AddSelfTransition('b', Weight.FromValue(3))
            .SetEndWeight(Weight.One);
            var automaton2 = builder2.GetAutomaton();

            StringAutomaton sum = automaton1.Sum(automaton2);

            StringInferenceTestUtilities.TestValue(sum, 2.0, string.Empty);
            StringInferenceTestUtilities.TestValue(sum, 4.0 + 6.0, "ab");
            StringInferenceTestUtilities.TestValue(sum, 16.0 + 36.0, "abab");
            StringInferenceTestUtilities.TestValue(sum, 12.0, "aab", "aba", "baa");
            StringInferenceTestUtilities.TestValue(sum, 18.0, "abb", "bab", "bba");
            StringInferenceTestUtilities.TestValue(sum, 8.0, "aaa");
            StringInferenceTestUtilities.TestValue(sum, 27.0, "bbb");

            StringAutomaton product = automaton1.Product(automaton2);

            StringInferenceTestUtilities.TestValue(product, 1.0, string.Empty);
            StringInferenceTestUtilities.TestValue(product, 4 * 6, "ab");
            StringInferenceTestUtilities.TestValue(product, 16 * 36, "abab");
            StringInferenceTestUtilities.TestValue(product, 0.0, "aba", "bbb", "a", "b");

            product.SetToProduct(product, product);
            StringInferenceTestUtilities.TestValue(product, 1.0, string.Empty);
            StringInferenceTestUtilities.TestValue(product, 4 * 4 * 6 * 6, "ab");
            StringInferenceTestUtilities.TestValue(product, 16 * 16 * 36 * 36, "abab");
            StringInferenceTestUtilities.TestValue(product, 0.0, "aba", "bbb", "a", "b");
        }
Exemplo n.º 20
0
        public void ComputeNormalizerWithManyNonTrivialLoops1()
        {
            var builder = new StringAutomaton.Builder();

            AddEpsilonLoop(builder.Start, 3, 0.2);
            AddEpsilonLoop(builder.Start, 5, 0.3);
            builder.Start.SetEndWeight(Weight.FromValue(0.1));
            var nextState = builder.Start.AddTransition('a', Weight.FromValue(0.4));

            nextState.SetEndWeight(Weight.FromValue(0.6));
            AddEpsilonLoop(nextState, 0, 0.3);
            nextState = nextState.AddTransition('b', Weight.FromValue(0.1));
            AddEpsilonLoop(nextState, 1, 0.9);
            nextState.SetEndWeight(Weight.FromValue(0.1));

            var automaton = builder.GetAutomaton();

            AssertStochastic(automaton);
            Assert.Equal(0.0, automaton.GetLogNormalizer(), 1e-6);
            Assert.Equal(0.0, GetLogNormalizerByGetValue(automaton), 1e-6);
            Assert.Equal(0.0, GetLogNormalizerByGetValueWithTransducers(automaton), 1e-6);
        }