Ejemplo n.º 1
0
        public void Copy()
        {
            StringTransducer copy = StringTransducer.Copy();

            StringInferenceTestUtilities.TestTransducerValue(copy, "important", "important", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "important", "i", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "important", "imp", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "important", "t", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "important", "mpo", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, string.Empty, string.Empty, 1.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "important", string.Empty, 0.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, string.Empty, "important", 0.0);

            //// Test that projection on Copy() doesn't change the automaton

            StringAutomaton automaton = StringAutomaton.ConstantOn(2.0, "a", "ab", "ac");

            automaton = automaton.Sum(StringAutomaton.ConstantOn(1.0, "a"));
            automaton = automaton.Sum(StringAutomaton.Constant(2.0));
            automaton = automaton.Product(StringAutomaton.Constant(3.0));

            StringAutomaton automatonClone = copy.ProjectSource(automaton);

            Assert.Equal(automaton, automatonClone);
        }
Ejemplo n.º 2
0
        public void CopyElements()
        {
            StringTransducer copy = StringTransducer.Copy(DiscreteChar.OneOf('a', 'b'));

            StringInferenceTestUtilities.TestTransducerValue(copy, string.Empty, string.Empty, 1.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "a", "a", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "bb", "bb", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "bab", "bab", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "bab", "ba", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "a", "b", 0.0);

            //// Tests that projection on Copy(elements) shrinks the support

            StringAutomaton automaton = StringAutomaton.ConstantOn(2.0, "a", "ab", "ac");

            automaton = automaton.Sum(StringAutomaton.ConstantOn(1.0, "a"));
            automaton = automaton.Sum(StringAutomaton.Constant(2.0));
            automaton = automaton.Product(StringAutomaton.Constant(3.0));

            for (int i = 0; i < 2; ++i)
            {
                StringInferenceTestUtilities.TestValue(automaton, 15, "a");
                StringInferenceTestUtilities.TestValue(automaton, 12, "ab");
                StringInferenceTestUtilities.TestValue(automaton, 6.0, "b", string.Empty);
                StringInferenceTestUtilities.TestValue(automaton, i == 0 ? 12.0 : 0.0, "ac");

                automaton = copy.ProjectSource(automaton);
            }
        }
Ejemplo n.º 3
0
        public static StringDistribution SubAverageConditional(StringDistribution str, int start, int minLength, int maxLength)
        {
            Argument.CheckIfNotNull(str, "str");
            Argument.CheckIfInRange(start >= 0, "start", "Start index must be non-negative.");
            Argument.CheckIfInRange(minLength >= 0, "minLength", "Min length must be non-negative.");
            Argument.CheckIfInRange(maxLength >= 0, "maxLength", "Max length must be non-negative.");

            if (str.IsPointMass)
            {
                var strPoint = str.Point;
                var alts     = new HashSet <string>();
                for (int length = minLength; length <= maxLength; length++)
                {
                    var s = strPoint.Substring(start, Math.Min(length, strPoint.Length));
                    alts.Add(s);
                }
                return(StringDistribution.OneOf(alts));
            }

            var anyChar    = StringAutomaton.ConstantOnElement(1.0, ImmutableDiscreteChar.Any());
            var transducer = StringTransducer.Consume(StringAutomaton.Repeat(anyChar, minTimes: start, maxTimes: start));

            transducer.AppendInPlace(StringTransducer.Copy(StringAutomaton.Repeat(anyChar, minTimes: minLength, maxTimes: maxLength)));
            transducer.AppendInPlace(StringTransducer.Consume(StringAutomaton.Constant(1.0)));

            return(StringDistribution.FromWeightFunction(transducer.ProjectSource(str.ToAutomaton())));
        }
Ejemplo n.º 4
0
        public void ProjectSourceLargeAutomaton()
        {
            using (var unlimited = new StringAutomaton.UnlimitedStatesComputation())
            {
                const int StateCount = 100_000;

                var builder = new StringAutomaton.Builder();
                var state   = builder.Start;

                for (var i = 1; i < StateCount; ++i)
                {
                    state = state.AddTransition('a', Weight.One);
                }

                state.SetEndWeight(Weight.One);

                var automaton      = builder.GetAutomaton();
                var point          = new string('a', StateCount - 1);
                var copyTransducer = StringTransducer.Copy();

                var projectedAutomaton = copyTransducer.ProjectSource(automaton);
                var projectedPoint     = copyTransducer.ProjectSource(point);

                StringInferenceTestUtilities.TestValue(projectedAutomaton, 1.0, point);
                StringInferenceTestUtilities.TestValue(projectedPoint, 1.0, point);
            }
        }
Ejemplo n.º 5
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="StringConcatOp"]/message_doc[@name="Str1AverageConditional(StringDistribution, StringDistribution)"]/*'/>
        public static StringDistribution Str1AverageConditional(StringDistribution concat, StringDistribution str2)
        {
            Argument.CheckIfNotNull(concat, "concat");
            Argument.CheckIfNotNull(str2, "str2");

            StringTransducer transducer = StringTransducer.Copy();

            transducer.AppendInPlace(StringTransducer.Consume(str2.ToAutomaton()));
            return(StringDistribution.FromWeightFunction(transducer.ProjectSource(concat.ToAutomaton())));
        }
Ejemplo n.º 6
0
        /// <summary>EP message to <c>str2</c>.</summary>
        /// <param name="concat">Incoming message from <c>concat</c>.</param>
        /// <param name="str1">Incoming message from <c>str1</c>.</param>
        /// <returns>The outgoing EP message to the <c>str2</c> argument.</returns>
        /// <remarks>
        ///   <para>The outgoing message is a distribution matching the moments of <c>str2</c> as the random arguments are varied. The formula is <c>proj[p(str2) sum_(concat,str1) p(concat,str1) factor(concat,str1,str2)]/p(str2)</c>.</para>
        /// </remarks>
        public static StringDistribution Str2AverageConditional(StringDistribution concat, StringDistribution str1)
        {
            Argument.CheckIfNotNull(concat, "concat");
            Argument.CheckIfNotNull(str1, "str1");

            StringTransducer transducer = StringTransducer.Consume(str1.GetProbabilityFunction());

            transducer.AppendInPlace(StringTransducer.Copy());
            return(StringDistribution.FromWorkspace(transducer.ProjectSource(concat)));
        }
Ejemplo n.º 7
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="StringConcatOp"]/message_doc[@name="Str1AverageConditional(StringDistribution, StringDistribution)"]/*'/>
        public static StringDistribution Str1AverageConditional(StringDistribution concat, StringDistribution str2)
        {
            Argument.CheckIfNotNull(concat, "concat");
            Argument.CheckIfNotNull(str2, "str2");

            StringTransducer transducer = StringTransducer.Copy();

            transducer.AppendInPlace(StringTransducer.Consume(str2.GetWorkspaceOrPoint()));
            return(StringDistribution.FromWorkspace(transducer.ProjectSource(concat.GetWorkspaceOrPoint())));
        }
Ejemplo n.º 8
0
        public void Repeat2()
        {
            StringAutomaton  automaton = StringAutomaton.ConstantOn(2.0, "a", string.Empty);
            StringTransducer repeat    = StringTransducer.Repeat(StringTransducer.Copy(automaton), 1, 2);

            StringInferenceTestUtilities.TestTransducerValue(repeat, "a", "a", 10.0);
            StringInferenceTestUtilities.TestTransducerValue(repeat, "aa", "aa", 4.0);
            StringInferenceTestUtilities.TestTransducerValue(repeat, string.Empty, string.Empty, 6.0);
            StringInferenceTestUtilities.TestTransducerValue(repeat, "aaa", "aaa", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(repeat, "aa", "a", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(repeat, string.Empty, "a", 0.0);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Initializes static members of the <see cref="StringFormatOpBase{TThis}"/> class.
        /// </summary>
        static StringFormatOpBase()
        {
            // More general behavior by default
            RequirePlaceholderForEveryArgument = false;

            DiscreteChar noBraces = DiscreteChar.OneOf('{', '}').Complement();

            DisallowBracesAutomaton  = StringAutomaton.Constant(1.0, noBraces);
            DisallowBracesTransducer = StringTransducer.Copy(noBraces);

            // Make sure that the static constructor of TThis has been invoked so that TThis sets everything up
            new TThis();
        }
Ejemplo n.º 10
0
        /// <summary>EP message to <c>str</c>.</summary>
        /// <param name="sub">Incoming message from <c>sub</c>.</param>
        /// <param name="start">Constant value for <c>start</c>.</param>
        /// <param name="length">Constant value for <c>length</c>.</param>
        /// <returns>The outgoing EP message to the <c>str</c> argument.</returns>
        /// <remarks>
        ///   <para>The outgoing message is a distribution matching the moments of <c>str</c> as the random arguments are varied. The formula is <c>proj[p(str) sum_(sub) p(sub) factor(sub,str,start,length)]/p(str)</c>.</para>
        /// </remarks>
        public static StringDistribution StrAverageConditional(StringDistribution sub, int start, int length)
        {
            Argument.CheckIfNotNull(sub, "sub");
            Argument.CheckIfInRange(start >= 0, "start", "Start index must be non-negative.");
            Argument.CheckIfInRange(length >= 0, "length", "Length must be non-negative.");

            var anyChar    = StringAutomaton.ConstantOnElement(1.0, DiscreteChar.Any());
            var transducer = StringTransducer.Produce(StringAutomaton.Repeat(anyChar, minTimes: start, maxTimes: start));

            transducer.AppendInPlace(StringTransducer.Copy(StringAutomaton.Repeat(anyChar, minTimes: length, maxTimes: length)));
            transducer.AppendInPlace(StringTransducer.Produce(StringAutomaton.Constant(1.0)));

            return(StringDistribution.FromWorkspace(transducer.ProjectSource(sub)));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Creates a transducer that replaces braces surrounding the given argument placeholder
        /// with <see cref="LeftBraceReplacer"/> and <see cref="RightBraceReplacer"/>.
        /// </summary>
        /// <param name="argument">The index of the argument.</param>
        /// <param name="argumentCount">The total number of the arguments.</param>
        /// <param name="transpose">Specifies whether the created transducer should be transposed (useful for backward message computation).</param>
        /// <returns>The created transducer.</returns>
        private static StringTransducer GetArgumentEscapingTransducer(int argument, int argumentCount, bool transpose)
        {
            Debug.Assert(argumentCount >= 1 && argumentCount <= 10, "Up to 10 arguments currently supported.");
            Debug.Assert(argument >= 0 && argument < argumentCount, "Argument index must be less than the number of arguments.");

            List <Pair <StringTransducer, StringTransducer> > argumentToEscapingTransducers;

            if (!ArgumentCountToEscapingTransducers.TryGetValue(argumentCount, out argumentToEscapingTransducers))
            {
                argumentToEscapingTransducers = new List <Pair <StringTransducer, StringTransducer> >(argumentCount);
                ArgumentCountToEscapingTransducers[argumentCount] = argumentToEscapingTransducers;

                for (int i = 0; i < argumentCount; ++i)
                {
                    // Escapes braces in {i}
                    StringTransducer replaceBracesForDigit = StringTransducer.ConsumeElement('{');
                    replaceBracesForDigit.AppendInPlace(StringTransducer.ProduceElement(LeftBraceReplacer));
                    replaceBracesForDigit.AppendInPlace(StringTransducer.CopyElement((char)('0' + i)));
                    replaceBracesForDigit.AppendInPlace(StringTransducer.ConsumeElement('}'));
                    replaceBracesForDigit.AppendInPlace(StringTransducer.ProduceElement(RightBraceReplacer));

                    // Skips any number of placeholders which differ from {i}, with arbitrary intermediate text
                    DiscreteChar     noBraces      = DiscreteChar.UniformOver('{', '}').Complement();
                    StringTransducer braceReplacer = StringTransducer.Copy(noBraces);
                    if (argumentCount > 1)
                    {
                        // Skips every placeholder except {i}
                        StringTransducer skipOtherDigits = StringTransducer.CopyElement('{');
                        skipOtherDigits.AppendInPlace(StringTransducer.CopyElement(AllDigitsExcept(i, argumentCount - 1)));
                        skipOtherDigits.AppendInPlace(StringTransducer.CopyElement('}'));

                        braceReplacer.AppendInPlace(skipOtherDigits);
                        braceReplacer = StringTransducer.Repeat(braceReplacer, minTimes: 0);
                        braceReplacer.AppendInPlace(StringTransducer.Copy(noBraces));
                    }

                    // Skips placeholders, then escapes {i} and skips placeholders
                    StringTransducer escapeAndSkip = replaceBracesForDigit.Clone();
                    escapeAndSkip.AppendInPlace(braceReplacer);
                    StringTransducer transducer = braceReplacer.Clone();
                    transducer.AppendInPlace(escapeAndSkip);  // TODO: use Optional() here if {i} can be omitted

                    StringTransducer transducerTranspose = StringTransducer.Transpose(transducer);
                    argumentToEscapingTransducers.Add(Pair.Create(transducer, transducerTranspose));
                }
            }

            return(transpose ? argumentToEscapingTransducers[argument].Second : argumentToEscapingTransducers[argument].First);
        }
Ejemplo n.º 12
0
        public void Optional()
        {
            StringAutomaton  automaton    = StringAutomaton.Constant(1.0, DiscreteChar.Lower());
            StringTransducer copy         = StringTransducer.Copy(automaton);
            StringTransducer copyOptional = StringTransducer.Optional(copy);

            StringInferenceTestUtilities.TestTransducerValue(copy, "abc", "abc", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(copyOptional, "abc", "abc", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, string.Empty, string.Empty, 1.0);
            StringInferenceTestUtilities.TestTransducerValue(copyOptional, string.Empty, string.Empty, 2.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "abc", "ab", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(copyOptional, "abc", "ab", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "abc", string.Empty, 0.0);
            StringInferenceTestUtilities.TestTransducerValue(copyOptional, "abc", string.Empty, 0.0);
        }
Ejemplo n.º 13
0
        public void CopyAutomaton()
        {
            StringAutomaton automaton = StringAutomaton.ConstantOn(1.0, "prefix1", "prefix2");

            automaton.AppendInPlace(StringAutomaton.Constant(1.0, DiscreteChar.Lower()));
            automaton.AppendInPlace(StringAutomaton.Constant(1.0, DiscreteChar.Upper()));
            automaton.AppendInPlace("!");

            StringTransducer copy = StringTransducer.Copy(automaton);

            StringInferenceTestUtilities.TestTransducerValue(copy, "prefix1!", "prefix1!", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "prefix2!", "prefix2!", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "prefix1lower!", "prefix1lower!", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "prefix2UPPER!", "prefix2UPPER!", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(copy, "prefix1lowerUPPER!", "prefix1lowerUPPER!", 1.0);
            StringInferenceTestUtilities.TestIfTransducerRejects(copy, "prefix1lower", "prefix2UPPER", "!", "prefix1lowerUPPER");

            StringInferenceTestUtilities.TestTransducerProjection(copy, automaton, "prefix1!", 1.0);
            StringInferenceTestUtilities.TestTransducerProjection(copy, automaton, "prefix2!", 1.0);
            StringInferenceTestUtilities.TestTransducerProjection(copy, automaton, "prefix1lower!", 1.0);
            StringInferenceTestUtilities.TestTransducerProjection(copy, automaton, "prefix2UPPER!", 1.0);
            StringInferenceTestUtilities.TestTransducerProjection(copy, automaton, "prefix1lowerUPPER!", 1.0);

            StringAutomaton subsetAutomaton = StringAutomaton.ConstantOn(2.0, "prefix1");

            subsetAutomaton.AppendInPlace(StringAutomaton.ConstantOn(3.0, "lll", "mmm"));
            subsetAutomaton.AppendInPlace(StringAutomaton.ConstantOn(1.5, "!", "U!"));
            StringInferenceTestUtilities.TestTransducerProjection(copy, subsetAutomaton, "prefix1lll!", 9.0);
            StringInferenceTestUtilities.TestTransducerProjection(copy, subsetAutomaton, "prefix1mmmU!", 9.0);
            StringInferenceTestUtilities.TestTransducerProjection(copy, subsetAutomaton, "prefix1!", 0.0);
            StringInferenceTestUtilities.TestTransducerProjection(copy, subsetAutomaton, "prefix2lower!", 0.0);
            StringInferenceTestUtilities.TestTransducerProjection(copy, subsetAutomaton, "prefix2U!", 0.0);

            StringAutomaton supersetAutomaton = StringAutomaton.ConstantOn(1.5, "pr");

            supersetAutomaton.AppendInPlace(StringAutomaton.Constant(2.0));
            StringInferenceTestUtilities.TestTransducerProjection(copy, supersetAutomaton, "prefix1!", 3.0);
            StringInferenceTestUtilities.TestTransducerProjection(copy, supersetAutomaton, "prefix2!", 3.0);
            StringInferenceTestUtilities.TestTransducerProjection(copy, supersetAutomaton, "prefix1lower!", 3.0);
            StringInferenceTestUtilities.TestTransducerProjection(copy, supersetAutomaton, "prefix2UPPER!", 3.0);
            StringInferenceTestUtilities.TestTransducerProjection(copy, supersetAutomaton, "prefix1lowerUPPER!", 3.0);
            StringInferenceTestUtilities.TestTransducerProjection(copy, supersetAutomaton, "prefix11!", 0.0);
            StringInferenceTestUtilities.TestTransducerProjection(copy, supersetAutomaton, "prefix1lowerUPPERlower!", 0.0);
            StringInferenceTestUtilities.TestTransducerProjection(copy, supersetAutomaton, "prrrrr!", 0.0);
        }
Ejemplo n.º 14
0
        public void CopySequence2()
        {
            const int CopyCount   = 10;
            const int LetterCount = 20;

            StringTransducer copy = StringTransducer.Copy();

            for (int i = 0; i < CopyCount - 1; ++i)
            {
                copy.AppendInPlace(StringTransducer.Copy());
            }

            var             sequence         = new string(Enumerable.Repeat('a', LetterCount).ToArray());
            StringAutomaton result           = copy.ProjectSource(sequence);
            var             expectedLogValue = Math.Log(StringInferenceTestUtilities.Partitions(LetterCount, CopyCount));

            Assert.Equal(expectedLogValue, result.GetLogValue(sequence), 1e-8);
        }
Ejemplo n.º 15
0
        public void CopySequence1()
        {
            StringTransducer copySequence = StringTransducer.Copy("important");

            StringInferenceTestUtilities.TestTransducerValue(copySequence, "important", "important", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(copySequence, "important", "imp", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(copySequence, "important", "ortant", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(copySequence, "important", string.Empty, 0.0);
            StringInferenceTestUtilities.TestTransducerProjection(copySequence, StringAutomaton.ConstantOn(3.0, "important"), "important", 3.0);
            StringInferenceTestUtilities.TestTransducerProjection(copySequence, StringAutomaton.ConstantOn(1.5, "important", "not important"), "important", 1.5);
            StringInferenceTestUtilities.TestTransducerProjection(copySequence, StringAutomaton.Constant(2.0), "important", 2.0);
            StringInferenceTestUtilities.TestTransducerProjection(
                copySequence,
                StringAutomaton.Constant(2.0).Append(StringAutomaton.ConstantOn(3.0, "nt")),
                "important",
                6.0);
            StringInferenceTestUtilities.TestIfTransducerRejects(copySequence, string.Empty, "nothing is important", "importance", "imp", "ortant", "a");
        }
Ejemplo n.º 16
0
        public void WordModel()
        {
            // We want to build a word model as a reasonably simple StringDistribution. It
            // should satisfy the following:
            // (1) The probability of a word of moderate length should not be
            //     significantly less than the probability of a shorter word.
            // (2) The probability of a specific word conditioned on its length matches that of
            //     words in the target language.
            // We achieve this by putting non-normalized character distributions on the edges. The
            // StringDistribution is unaware that these are non-normalized.
            // The StringDistribution itself is non-normalizable.
            const double TargetProb1  = 0.05;
            const double Ratio1       = 0.4;
            const double TargetProb2  = TargetProb1 * Ratio1;
            const double Ratio2       = 0.2;
            const double TargetProb3  = TargetProb2 * Ratio2;
            const double TargetProb4  = TargetProb3 * Ratio2;
            const double TargetProb5  = TargetProb4 * Ratio2;
            const double Ratio3       = 0.999;
            const double TargetProb6  = TargetProb5 * Ratio3;
            const double TargetProb7  = TargetProb6 * Ratio3;
            const double TargetProb8  = TargetProb7 * Ratio3;
            const double Ratio4       = 0.9;
            const double TargetProb9  = TargetProb8 * Ratio4;
            const double TargetProb10 = TargetProb9 * Ratio4;

            var targetProbabilitiesPerLength = new double[]
            {
                TargetProb1, TargetProb2, TargetProb3, TargetProb4, TargetProb5, TargetProb6, TargetProb7, TargetProb8, TargetProb9, TargetProb10
            };

            var charDistUpper          = DiscreteChar.Upper();
            var charDistLower          = DiscreteChar.Lower();
            var charDistUpperNarrow    = DiscreteChar.OneOf('A', 'B');
            var charDistLowerNarrow    = DiscreteChar.OneOf('a', 'b');
            var charDistUpperScaled    = DiscreteChar.Uniform();
            var charDistLowerScaled1   = DiscreteChar.Uniform();
            var charDistLowerScaled2   = DiscreteChar.Uniform();
            var charDistLowerScaled3   = DiscreteChar.Uniform();
            var charDistLowerScaledEnd = DiscreteChar.Uniform();

            charDistUpperScaled.SetToPartialUniformOf(charDistUpper, Math.Log(TargetProb1));
            charDistLowerScaled1.SetToPartialUniformOf(charDistLower, Math.Log(Ratio1));
            charDistLowerScaled2.SetToPartialUniformOf(charDistLower, Math.Log(Ratio2));
            charDistLowerScaled3.SetToPartialUniformOf(charDistLower, Math.Log(Ratio3));
            charDistLowerScaledEnd.SetToPartialUniformOf(charDistLower, Math.Log(Ratio4));

            var wordModel = StringDistribution.Concatenate(
                new List <DiscreteChar>
            {
                charDistUpperScaled,
                charDistLowerScaled1,
                charDistLowerScaled2,
                charDistLowerScaled2,
                charDistLowerScaled2,
                charDistLowerScaled3,
                charDistLowerScaled3,
                charDistLowerScaled3,
                charDistLowerScaledEnd
            },
                true,
                true);

            const string Word = "Abcdefghij";

            const double Eps                   = 1e-5;
            var          broadDist             = StringDistribution.Char(charDistUpper);
            var          narrowDist            = StringDistribution.Char(charDistUpperNarrow);
            var          narrowWord            = "A";
            var          expectedProbForNarrow = 0.5;

            for (var i = 0; i < targetProbabilitiesPerLength.Length; i++)
            {
                var currentWord     = Word.Substring(0, i + 1);
                var probCurrentWord = Math.Exp(wordModel.GetLogProb(currentWord));
                Assert.Equal(targetProbabilitiesPerLength[i], probCurrentWord, Eps);

                var logAvg = Math.Exp(wordModel.GetLogAverageOf(broadDist));
                Assert.Equal(targetProbabilitiesPerLength[i], logAvg, Eps);

                var prod = StringDistribution.Zero();
                prod.SetToProduct(broadDist, wordModel);
                Xunit.Assert.True(prod.GetWorkspaceOrPoint().HasElementLogValueOverrides);
                probCurrentWord = Math.Exp(prod.GetLogProb(currentWord));
                Assert.Equal(targetProbabilitiesPerLength[i], probCurrentWord, Eps);

                prod.SetToProduct(narrowDist, wordModel);
                Xunit.Assert.False(prod.GetWorkspaceOrPoint().HasElementLogValueOverrides);
                var probNarrowWord = Math.Exp(prod.GetLogProb(narrowWord));
                Assert.Equal(expectedProbForNarrow, probNarrowWord, Eps);

                broadDist              = broadDist.Append(charDistLower);
                narrowDist             = narrowDist.Append(charDistLowerNarrow);
                narrowWord            += "a";
                expectedProbForNarrow *= 0.5;
            }

            // Copied model
            var copiedModel = StringDistribution.FromWorkspace(StringTransducer.Copy().ProjectSource(wordModel.GetWorkspaceOrPoint()));

            // Under transducer.
            for (var i = 0; i < targetProbabilitiesPerLength.Length; i++)
            {
                var currentWord     = Word.Substring(0, i + 1);
                var probCurrentWord = Math.Exp(copiedModel.GetLogProb(currentWord));
                Assert.Equal(targetProbabilitiesPerLength[i], probCurrentWord, Eps);
            }

            // Rescaled model
            var scale                = 0.5;
            var newTargetProb1       = TargetProb1 * scale;
            var charDistUpperScaled1 = DiscreteChar.Uniform();

            charDistUpperScaled1.SetToPartialUniformOf(charDistUpper, Math.Log(newTargetProb1));
            var reWeightingTransducer =
                StringTransducer.Replace(StringDistribution.Char(charDistUpper).GetWorkspaceOrPoint(), StringDistribution.Char(charDistUpperScaled1).GetWorkspaceOrPoint())
                .Append(StringTransducer.Copy());
            var reWeightedWordModel = StringDistribution.FromWorkspace(reWeightingTransducer.ProjectSource(wordModel.GetWorkspaceOrPoint()));

            for (var i = 0; i < targetProbabilitiesPerLength.Length; i++)
            {
                var currentWord     = Word.Substring(0, i + 1);
                var probCurrentWord = Math.Exp(reWeightedWordModel.GetLogProb(currentWord));
                Assert.Equal(scale * targetProbabilitiesPerLength[i], probCurrentWord, Eps);
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Initializes static members of the <see cref="StringFormatOp"/> class.
        /// </summary>
        static StringFormatOp()
        {
            DiscreteChar noBraceReplacers = DiscreteChar.UniformOver(LeftBraceReplacer, RightBraceReplacer).Complement();

            DisallowBraceReplacersTransducer = StringTransducer.Copy(noBraceReplacers);
        }