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()))); }
public void Transpose() { StringTransducer transducer = StringTransducer.Consume(StringAutomaton.Constant(3.0)); StringTransducer transpose1 = StringTransducer.Transpose(transducer); StringTransducer transpose2 = transducer.Clone(); transpose2.TransposeInPlace(); var pairs = new[] { new[] { "a", string.Empty }, new[] { string.Empty, string.Empty }, new[] { "a", "bc" }, new[] { "a", "a" } }; foreach (string[] valuePair in pairs) { double referenceValue1 = transducer.GetValue(valuePair[0], valuePair[1]); Assert.Equal(referenceValue1, transpose1.GetValue(valuePair[1], valuePair[0])); Assert.Equal(referenceValue1, transpose2.GetValue(valuePair[1], valuePair[0])); double referenceValue2 = transducer.GetValue(valuePair[1], valuePair[0]); Assert.Equal(referenceValue2, transpose1.GetValue(valuePair[0], valuePair[1])); Assert.Equal(referenceValue2, transpose2.GetValue(valuePair[0], valuePair[1])); } }
/// <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 }
public void ConsumeSequence() { StringTransducer consume = StringTransducer.Consume("abc"); StringInferenceTestUtilities.TestTransducerValue(consume, "abc", string.Empty, 1.0); StringInferenceTestUtilities.TestTransducerValue(consume, "ab", string.Empty, 0.0); StringInferenceTestUtilities.TestTransducerValue(consume, "abcd", string.Empty, 0.0); StringInferenceTestUtilities.TestTransducerValue(consume, string.Empty, string.Empty, 0.0); StringInferenceTestUtilities.TestTransducerValue(consume, string.Empty, "abc", 0.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))); }
/// <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()))); }
/// <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()))); }
public void Repeat1() { StringAutomaton automaton = StringAutomaton.ConstantOn(2.0, "a"); StringTransducer repeat = StringTransducer.Repeat(StringTransducer.Consume(automaton), 1, 3); StringInferenceTestUtilities.TestTransducerValue(repeat, "a", string.Empty, 2.0); StringInferenceTestUtilities.TestTransducerValue(repeat, "aa", string.Empty, 4.0); StringInferenceTestUtilities.TestTransducerValue(repeat, "aaa", string.Empty, 8.0); StringInferenceTestUtilities.TestTransducerValue(repeat, "aaaa", string.Empty, 0.0); StringInferenceTestUtilities.TestTransducerValue(repeat, string.Empty, string.Empty, 0.0); StringInferenceTestUtilities.TestTransducerValue(repeat, string.Empty, "aaa", 0.0); }
public void ConsumeAutomaton() { StringAutomaton automaton = StringAutomaton.Constant(2.0, DiscreteChar.Lower()); automaton = automaton.Sum(StringAutomaton.ConstantOnElement(3.0, 'a')); StringTransducer consume = StringTransducer.Consume(automaton); StringInferenceTestUtilities.TestTransducerValue(consume, "aaa", string.Empty, 2.0); StringInferenceTestUtilities.TestTransducerValue(consume, "bb", string.Empty, 2.0); StringInferenceTestUtilities.TestTransducerValue(consume, "a", string.Empty, 5.0); StringInferenceTestUtilities.TestTransducerValue(consume, string.Empty, string.Empty, 2.0); StringInferenceTestUtilities.TestTransducerValue(consume, "bb", "aaa", 0.0); StringInferenceTestUtilities.TestTransducerValue(consume, "bb", "bb", 0.0); StringInferenceTestUtilities.TestTransducerValue(consume, string.Empty, "bb", 0.0); StringInferenceTestUtilities.TestTransducerValue(consume, string.Empty, "a", 0.0); }
/// <summary>EP message to <c>sub</c>.</summary> /// <param name="str">Incoming message from <c>str</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>sub</c> argument.</returns> /// <remarks> /// <para>The outgoing message is a distribution matching the moments of <c>sub</c> as the random arguments are varied. The formula is <c>proj[p(sub) sum_(str) p(str) factor(sub,str,start,length)]/p(sub)</c>.</para> /// </remarks> public static StringDistribution SubAverageConditional(StringDistribution str, int start, int length) { Argument.CheckIfNotNull(str, "str"); Argument.CheckIfInRange(start >= 0, "start", "Start index must be non-negative."); Argument.CheckIfInRange(length >= 0, "length", "Length must be non-negative."); if (str.IsPointMass) { return(SubAverageConditional(str.Point, start, length)); } 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: length, maxTimes: length))); transducer.AppendInPlace(StringTransducer.Consume(StringAutomaton.Constant(1.0))); return(StringDistribution.FromWorkspace(transducer.ProjectSource(str))); }
/// <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); }