Exemple #1
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, DiscreteChar.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.FromWorkspace(transducer.ProjectSource(str.GetWorkspaceOrPoint())));
        }
Exemple #2
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)));
        }
Exemple #3
0
        public void StringFromItems()
        {
            Variable <string> str   = Variable.StringOfLength(2);
            Variable <char>   char1 = Variable.GetItem(str, 0);
            Variable <char>   char2 = Variable.GetItem(str, 1);

            var engine = new InferenceEngine();

            char1.ObservedValue = 'a';
            char2.ObservedValue = 'b';

            Assert.Equal(StringDistribution.PointMass("ab"), engine.Infer <StringDistribution>(str), ProbEps);

            char1.ClearObservedValue();

            Assert.Equal(
                StringDistribution.Char(DiscreteChar.Any()) + StringDistribution.Char('b'),
                engine.Infer <StringDistribution>(str),
                ProbEps);
        }
Exemple #4
0
 /// <summary>
 /// Creates a uniform distribution over any string starting and ending with a non-word character (possibly of length 1).
 /// </summary>
 /// <returns>The created distribution.</returns>
 public static StringDistribution WordMiddle() => WordMiddle(DiscreteChar.Any());
Exemple #5
0
 /// <summary>
 /// Creates a uniform distribution over either the empty string or any string starting with a non-word character.
 /// </summary>
 /// <returns>The created distribution.</returns>
 public static StringDistribution WordSuffix() => WordSuffix(DiscreteChar.Any());
Exemple #6
0
 /// <summary>
 /// Creates a uniform distribution over any string starting and ending with a non-word character,
 /// with a length in given bounds.
 /// </summary>
 /// <param name="minLength">The minimum allowed string length.</param>
 /// <param name="maxLength">The maximum allowed string length.</param>
 /// <returns>The created distribution.</returns>
 public static StringDistribution WordMiddle(int minLength, int maxLength) => WordMiddle(minLength, maxLength, DiscreteChar.Any());