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])); } }
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); }
public void CopyElement() { StringTransducer copy = StringTransducer.CopyElement(DiscreteChar.OneOf('a', 'b')); StringInferenceTestUtilities.TestTransducerValue(copy, "a", "a", 1.0); StringInferenceTestUtilities.TestTransducerValue(copy, "b", "b", 1.0); StringInferenceTestUtilities.TestTransducerValue(copy, "a", "b", 0.0); StringInferenceTestUtilities.TestTransducerValue(copy, "b", "a", 0.0); StringInferenceTestUtilities.TestTransducerValue(copy, string.Empty, string.Empty, 0.0); StringInferenceTestUtilities.TestTransducerValue(copy, "bb", "bb", 0.0); StringInferenceTestUtilities.TestTransducerValue(copy, "bab", "bab", 0.0); StringInferenceTestUtilities.TestTransducerValue(copy, "bab", "ba", 0.0); //// Tests that projection on CopyElement(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, 6.0, "b"); StringInferenceTestUtilities.TestValue(automaton, i == 0 ? 6.0 : 0.0, string.Empty); StringInferenceTestUtilities.TestValue(automaton, i == 0 ? 12.0 : 0.0, "ac", "ab"); automaton = copy.ProjectSource(automaton); } }
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()))); }
/// <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 }
/// <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(); }
public void Repeat2() { StringAutomaton automaton = StringAutomaton.Constant(1.0, DiscreteChar.Lower()); StringInferenceTestUtilities.TestValue(automaton, 1.0, string.Empty, "ab", "abcab"); automaton = StringAutomaton.Repeat(automaton); // Can't use StringInferenceTestUtilities.TestValue here since the value is not infinite in log-domain // due to approximate closure computations for epsilon-loops Assert.True(automaton.GetValue(string.Empty) > 1000); Assert.True(automaton.GetValue("ab") > 1000); Assert.True(automaton.GetValue("abcab") > 1000); }
/// <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))); }
public void Repeat1() { StringAutomaton automaton = StringAutomaton.ConstantOn(1.0, "abc"); automaton.AppendInPlace(StringAutomaton.Constant(2.0, DiscreteChar.Upper())); StringInferenceTestUtilities.TestValue(automaton, 0.0, string.Empty, "ab", "abcab", "XYZ"); StringInferenceTestUtilities.TestValue(automaton, 2.0, "abc", "abcX", "abcXXYZ"); StringAutomaton loopyAutomaton = StringAutomaton.Repeat(automaton); StringInferenceTestUtilities.TestValue(loopyAutomaton, 0.0, string.Empty, "ab", "abcab", "XYZ"); StringInferenceTestUtilities.TestValue(loopyAutomaton, 2.0, "abc", "abcA"); StringInferenceTestUtilities.TestValue(loopyAutomaton, 4.0, "abcabc", "abcabcX", "abcABCabc", "abcXabcYZ"); StringInferenceTestUtilities.TestValue(loopyAutomaton, 8.0, "abcabcabc", "abcXabcYabcZZ"); }
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); }
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); }
public void ProduceAutomaton() { StringAutomaton automaton = StringAutomaton.Constant(2.0, ImmutableDiscreteChar.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); }
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); }
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"); }
public void ReplaceAutomaton() { StringAutomaton automaton1 = StringAutomaton.Constant(2.0, DiscreteChar.Lower()); automaton1 = automaton1.Sum(StringAutomaton.ConstantOnElement(3.0, 'a')); StringAutomaton automaton2 = StringAutomaton.Constant(0.5, DiscreteChar.Digit()); automaton2 = automaton2.Sum(StringAutomaton.Constant(2.5, DiscreteChar.LetterOrDigit())); StringTransducer replace = StringTransducer.Replace(automaton1, automaton2); StringInferenceTestUtilities.TestTransducerValue(replace, string.Empty, "123", 6.0); StringInferenceTestUtilities.TestTransducerValue(replace, "a", "123", 15.0); StringInferenceTestUtilities.TestTransducerValue(replace, "ax", "AbC", 5.0); StringInferenceTestUtilities.TestTransducerValue(replace, "a", "a", 12.5); StringInferenceTestUtilities.TestTransducerValue(replace, string.Empty, string.Empty, 6.0); StringInferenceTestUtilities.TestTransducerValue(replace, "123", string.Empty, 0.0); StringInferenceTestUtilities.TestTransducerValue(replace, "AbC", "ax", 0.0); StringInferenceTestUtilities.TestTransducerValue(replace, "1", "1", 0.0); }