コード例 #1
0
        /// <summary>
        /// Creates a transducer that replaces escaped argument placeholders with the corresponding arguments.
        /// </summary>
        /// <param name="escapedArgs">The list of 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 GetPlaceholderReplacingTransducer(IList <StringAutomaton> escapedArgs, bool transpose)
        {
            var alternatives = new List <StringTransducer>();

            for (int i = 0; i < escapedArgs.Count; ++i)
            {
                StringTransducer alternative = StringTransducer.ConsumeElement((char)('0' + i));
                alternative.AppendInPlace(StringTransducer.Produce(escapedArgs[i]), (byte)(i + 1));
                alternatives.Add(alternative);
            }

            StringTransducer result = DisallowBraceReplacersTransducer.Clone();

            result.AppendInPlace(StringTransducer.ConsumeElement(LeftBraceReplacer));
            result.AppendInPlace(StringTransducer.Sum(alternatives));
            result.AppendInPlace(StringTransducer.ConsumeElement(RightBraceReplacer));
            result = StringTransducer.Repeat(result, minTimes: 0);
            result.AppendInPlace(DisallowBraceReplacersTransducer);

            if (transpose)
            {
                result.TransposeInPlace();
            }

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Computes the normalizer via transducers.
        /// </summary>
        /// <param name="automaton">The automaton.</param>
        /// <returns>The logarithm of the normalizer.</returns>
        private static double GetLogNormalizerByGetValueWithTransducers(StringAutomaton automaton)
        {
            var one = StringAutomaton.Constant(1.0);
            StringTransducer transducer = StringTransducer.Consume(automaton);

            transducer.AppendInPlace(StringTransducer.Produce(one));
            return(transducer.ProjectSource(one).GetLogValue("an arbitrary string")); // Now this will be exactly the normalizer
        }
コード例 #3
0
        public void ProduceSequence()
        {
            StringTransducer produce = StringTransducer.Produce("abc");

            StringInferenceTestUtilities.TestTransducerValue(produce, string.Empty, "abc", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(produce, string.Empty, "ab", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(produce, string.Empty, "abcd", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(produce, string.Empty, string.Empty, 0.0);
            StringInferenceTestUtilities.TestTransducerValue(produce, "abc", string.Empty, 0.0);
        }
コード例 #4
0
ファイル: SubstringOp.cs プロジェクト: mesgarpour/ERMER
        /// <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)));
        }
コード例 #5
0
        public void ProduceAutomaton()
        {
            StringAutomaton automaton = StringAutomaton.Constant(2.0, DiscreteChar.Lower());

            automaton = automaton.Sum(StringAutomaton.ConstantOnElement(3.0, 'a'));
            StringTransducer produce = StringTransducer.Produce(automaton);

            StringInferenceTestUtilities.TestTransducerValue(produce, string.Empty, "aaa", 2.0);
            StringInferenceTestUtilities.TestTransducerValue(produce, string.Empty, "bb", 2.0);
            StringInferenceTestUtilities.TestTransducerValue(produce, string.Empty, "a", 5.0);
            StringInferenceTestUtilities.TestTransducerValue(produce, string.Empty, string.Empty, 2.0);
            StringInferenceTestUtilities.TestTransducerValue(produce, "aaa", "bb", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(produce, "bb", "bb", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(produce, "bb", string.Empty, 0.0);
            StringInferenceTestUtilities.TestTransducerValue(produce, "a", string.Empty, 0.0);
        }
コード例 #6
0
        /// <summary>
        /// Creates a transducer that replaces argument placeholders with the corresponding arguments.
        /// </summary>
        /// <param name="args">The list of arguments.</param>
        /// <param name="argNames">The list of argument names.</param>
        /// <param name="forBackwardMessage">Specifies whether the created transducer should be transposed so that the backward message can be computed.</param>
        /// <param name="withGroups">Specifies whether filled in arguments should be labeled with different groups.</param>
        /// <returns>The created transducer.</returns>
        private static StringTransducer GetPlaceholderReplacingTransducer(
            IList <StringAutomaton> args, IReadOnlyList <string> argNames, bool forBackwardMessage, bool withGroups)
        {
            var alternatives = new List <StringTransducer>();

            for (int argumentIndex = 0; argumentIndex < args.Count; ++argumentIndex)
            {
                StringTransducer alternative;
                if (!forBackwardMessage)
                {
                    alternative = StringTransducer.Consume(argNames[argumentIndex]);
                    alternative.AppendInPlace(StringTransducer.Produce(args[argumentIndex]), withGroups ? argumentIndex + 1 : 0);
                }
                else
                {
                    // After transposing 'Produce' will become 'Consume',
                    // and starting from 'Consume' makes projection computations more efficeint because it allows to detect dead braches earlier
                    Debug.Assert(!withGroups, "Groups are never needed for backward message.");
                    alternative = StringTransducer.Produce(args[argumentIndex]);
                    alternative.AppendInPlace(StringTransducer.Consume(argNames[argumentIndex]));
                }

                alternatives.Add(alternative);
            }

            StringTransducer result = DisallowBracesTransducer.Clone();

            result.AppendInPlace(StringTransducer.ConsumeElement('{'));
            result.AppendInPlace(StringTransducer.Sum(alternatives));
            result.AppendInPlace(StringTransducer.ConsumeElement('}'));
            result = StringTransducer.Repeat(result, minTimes: 0);
            result.AppendInPlace(DisallowBracesTransducer);

            if (forBackwardMessage)
            {
                result = StringTransducer.Transpose(result);
            }

            return(result);
        }