예제 #1
0
        /// <summary>EP message to <c>character</c>.</summary>
        /// <param name="probabilities">Incoming message from <c>probabilities</c>. Must be a proper distribution. If any element is uniform, the result will be uniform.</param>
        /// <returns>The outgoing EP message to the <c>character</c> argument.</returns>
        /// <remarks>
        ///   <para>The outgoing message is a distribution matching the moments of <c>character</c> as the random arguments are varied. The formula is <c>proj[p(character) sum_(probabilities) p(probabilities) factor(character,probabilities)]/p(character)</c>.</para>
        /// </remarks>
        /// <exception cref="ImproperMessageException">
        ///   <paramref name="probabilities" /> is not a proper distribution.</exception>
        public static DiscreteChar CharacterAverageConditional([SkipIfUniform] Dirichlet probabilities)
        {
            Discrete resultAsDiscrete = Discrete.Uniform(probabilities.Dimension, probabilities.Sparsity);

            DiscreteFromDirichletOp.SampleAverageConditional(probabilities, resultAsDiscrete);
            return(DiscreteChar.FromVector(resultAsDiscrete.GetProbs()));
        }
예제 #2
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())));
        }
예제 #3
0
        /// <summary>
        /// Creates a uniform distribution over any string starting and ending with a non-word character,
        /// with a length in given bounds.
        /// Characters other than the first and the last are restricted to be non-zero probability characters
        /// from a given distribution.
        /// </summary>
        /// <param name="minLength">The minimum allowed string length.</param>
        /// <param name="maxLength">The maximum allowed string length.</param>
        /// <param name="allowedChars">The distribution representing allowed characters.</param>
        /// <returns>The created distribution.</returns>
        public static StringDistribution WordMiddle(int minLength, int maxLength, DiscreteChar allowedChars)
        {
            if (maxLength < minLength)
            {
                throw new ArgumentException("The maximum length cannot be less than the minimum length.");
            }

            if (minLength < 1)
            {
                throw new ArgumentException("The minimum length must be at least one.");
            }

            var nonWordChar = StringDistribution.Char(NonWordCharacter);

            if ((minLength == 1) && (maxLength == 1))
            {
                return(nonWordChar);
            }

            // TODO: make a PartialUniform copy of allowedChars
            var suffix = StringDistribution.Repeat(allowedChars, minTimes: Math.Max(minLength - 2, 0), maxTimes: maxLength - 2);

            suffix.AppendInPlace(nonWordChar);

            if (minLength == 1)
            {
                var allowedChar   = allowedChars.GetMode();
                var allowedSuffix = new string(Enumerable.Repeat(allowedChar, Math.Max(minLength - 2, 0)).ToArray()) + ' ';
                var suffixLogProb = suffix.GetLogProb(allowedSuffix);
                suffix.SetToSumLog(suffixLogProb, StringDistribution.Empty(), 0.0, suffix);
            }

            return(nonWordChar + suffix);
        }
예제 #4
0
        public void LengthBounds()
        {
            var lengthDist1 = StringDistribution.Any(minLength: 1, maxLength: 3);

            Assert.True(lengthDist1.IsProper());
            StringInferenceTestUtilities.TestProbability(lengthDist1, StringInferenceTestUtilities.StringUniformProbability(1, 3, 65536), "a", "aa", "aaa");
            StringInferenceTestUtilities.TestProbability(lengthDist1, 0.0, string.Empty, "aaaa");

            var lengthDist2 = StringDistribution.Repeat(DiscreteChar.OneOf('a', 'b'), minTimes: 1, maxTimes: 3);

            Assert.True(lengthDist2.IsProper());
            StringInferenceTestUtilities.TestProbability(lengthDist2, StringInferenceTestUtilities.StringUniformProbability(1, 3, 2), "a", "ab", "aba");
            StringInferenceTestUtilities.TestProbability(lengthDist2, 0.0, string.Empty, "aaaa", "abab", "cc");

            var lengthDist3 = StringDistribution.Repeat(DiscreteChar.OneOf('a', 'b'), minTimes: 2, maxTimes: 2);

            Assert.True(lengthDist3.IsProper());
            StringInferenceTestUtilities.TestProbability(lengthDist3, StringInferenceTestUtilities.StringUniformProbability(2, 2, 2), "aa", "ab", "ba", "bb");
            StringInferenceTestUtilities.TestProbability(lengthDist3, 0.0, string.Empty, "a", "abab", "cc");

            var minLengthDist = StringDistribution.Any(minLength: 2);

            Assert.False(minLengthDist.IsProper());
            StringInferenceTestUtilities.TestProbability(minLengthDist, 1.0, "aa", "123", "@*(@*&(@)");
            StringInferenceTestUtilities.TestProbability(minLengthDist, 0.0, string.Empty, "a", "!");

            var maxLengthDist = StringDistribution.ZeroOrMore(DiscreteChar.Digit(), maxTimes: 3);

            Assert.True(maxLengthDist.IsProper());
            StringInferenceTestUtilities.TestProbability(maxLengthDist, StringInferenceTestUtilities.StringUniformProbability(0, 3, 10), string.Empty, "1", "32", "432");
            StringInferenceTestUtilities.TestProbability(maxLengthDist, 0.0, "abc", "1234");
        }
예제 #5
0
        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 void GetOutgoingTransitionsForDeterminization2()
        {
            var wrapper = new StringAutomatonWrapper();

            wrapper.Start.AddTransition(DiscreteChar.UniformInRange('a', 'z'), Weight.FromValue(2));
            wrapper.Start.AddTransition(DiscreteChar.UniformInRanges('a', 'z', 'A', 'Z'), Weight.FromValue(3));

            var outgoingTransitions =
                wrapper.GetOutgoingTransitionsForDeterminization(new Dictionary <int, Weight> {
                { 0, Weight.FromValue(5) }
            });
            var expectedOutgoingTransitions = new[]
            {
                new Tuple <DiscreteChar, Weight, IEnumerable <KeyValuePair <int, Weight> > >(
                    DiscreteChar.UniformInRange('A', 'Z'), Weight.FromValue(7.5), new Dictionary <int, Weight> {
                    { 2, Weight.FromValue(1) }
                }),
                new Tuple <DiscreteChar, Weight, IEnumerable <KeyValuePair <int, Weight> > >(
                    DiscreteChar.UniformInRange('a', 'z'), Weight.FromValue(17.5), new Dictionary <int, Weight> {
                    { 1, Weight.FromValue(10 / 17.5) }, { 2, Weight.FromValue(7.5 / 17.5) }
                }),
            };

            AssertCollectionsEqual(expectedOutgoingTransitions, outgoingTransitions, TransitionInfoEqualityComparer.Instance);
        }
예제 #7
0
        public void MessageOpsTest()
        {
            const double Eps = 1e-6;

            StringDistribution str1 = StringOfLengthOp.StrAverageConditional(DiscreteChar.Letter(), 10);

            Assert.Equal(StringDistribution.Repeat(DiscreteChar.Letter(), 10, 10), str1);

            StringDistribution str2 = StringOfLengthOp.StrAverageConditional(
                DiscreteChar.PointMass('a'), Discrete.UniformInRange(5, 2, 4));

            Assert.Equal(StringDistribution.OneOf("aa", "aaa", "aaaa"), str2);

            StringDistribution str3 = StringOfLengthOp.StrAverageConditional(
                DiscreteChar.OneOf('a', 'b'), new Discrete(0.1, 0.0, 0.6, 0.3));

            StringInferenceTestUtilities.TestProbability(str3, 0.1, string.Empty);
            StringInferenceTestUtilities.TestProbability(str3, 0.6 / 4, "aa", "ab", "ba", "bb");
            StringInferenceTestUtilities.TestProbability(str3, 0.3 / 8, "aaa", "bbb", "abb", "bab");

            Discrete length1 = StringOfLengthOp.LengthAverageConditional(
                StringDistribution.OneOf("aa", "bbb"), DiscreteChar.PointMass('a'), Discrete.Uniform(10));

            Assert.Equal(Discrete.PointMass(2, 10), length1);

            Discrete length2 = StringOfLengthOp.LengthAverageConditional(
                StringDistribution.OneOf("aab", "ab", "b", "bc"), DiscreteChar.OneOf('a', 'b'), Discrete.Uniform(10));

            Assert.Equal(4.0 / 7.0, length2[1], Eps);
            Assert.Equal(2.0 / 7.0, length2[2], Eps);
            Assert.Equal(1.0 / 7.0, length2[3], Eps);
        }
예제 #8
0
        /// <summary>EP message to <c>str</c>.</summary>
        /// <param name="allowedChars">Constant value for <c>allowedChars</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 the factor viewed as a function of <c>str</c> conditioned on the given values.</para>
        /// </remarks>
        public static StringDistribution StrAverageConditional(DiscreteChar allowedChars, int length)
        {
            Argument.CheckIfNotNull(allowedChars, "allowedChars");
            Argument.CheckIfValid(allowedChars.IsPartialUniform(), "allowedChars", "The set of allowed characters must be passed as a partial uniform distribution.");

            return(StringDistribution.Repeat(allowedChars, length, length));
        }
예제 #9
0
        public void SemanticWebTest3()
        {
            // Simplified name model
            var prop0 = Variable.Random(NamePrior()).Named("prop0");
            // Simplified date model any string ending in a digit
            var prop1 = Variable.Random(
                StringDistribution.Char(DiscreteChar.Digit()) + StringDistribution.Any() + StringDistribution.Char(DiscreteChar.Digit()))
                        .Named("prop1");
            var template = Variable.Random(StringDistribution.String("{0}") + WordStrings.WordMiddle() + StringDistribution.String("{1}"))
                           .Named("template");
            var text = Variable.StringFormat(template, prop0, prop1).Named("text");

            text.ObservedValue = "Tony Blair was born on 6 May 1953";

            var engine = new InferenceEngine();

            engine.Compiler.RecommendedQuality = QualityBand.Experimental;
            engine.NumberOfIterations          = 1;

            var prop0Dist    = engine.Infer <StringDistribution>(prop0);
            var prop1Dist    = engine.Infer <StringDistribution>(prop1);
            var templateDist = engine.Infer <StringDistribution>(template);

            StringInferenceTestUtilities.TestProbability(prop0Dist, 1.0, "Tony Blair");
            StringInferenceTestUtilities.TestProbability(prop1Dist, 0.5, "1953", "6 May 1953");
            StringInferenceTestUtilities.TestProbability(templateDist, 0.5, "{0} was born on {1}", "{0} was born on 6 May {1}");
        }
예제 #10
0
        public void ProductWithLogOverrideNarrow()
        {
            for (var i = 0; i < 2; i++)
            {
                var dist1 = DiscreteChar.LetterOrDigit();
                var dist2 = DiscreteChar.OneOf('1', '3', '5', '6');

                var logOverrideProbability = Math.Log(0.9);
                dist1.SetToPartialUniformOf(dist1, logOverrideProbability);
                Xunit.Assert.True(dist1.HasLogProbabilityOverride);
                Xunit.Assert.False(dist2.IsBroad);

                if (i == 1)
                {
                    Util.Swap(ref dist1, ref dist2);
                }

                var dist3 = DiscreteChar.Uniform();
                dist3.SetToProduct(dist1, dist2);

                Xunit.Assert.False(dist3.HasLogProbabilityOverride);
                Assert.Equal(Math.Log(0.25), dist3.GetLogProb('5'), Eps);
                Xunit.Assert.True(double.IsNegativeInfinity(dist3.GetLogProb('a')));
            }
        }
예제 #11
0
        public void PartialUniformWithLogProbabilityOverride()
        {
            var dist       = DiscreteChar.LetterOrDigit();
            var probLetter = Math.Exp(dist.GetLogProb('j'));
            var probNumber = Math.Exp(dist.GetLogProb('5'));

            var logProbabilityOverride = Math.Log(0.7);
            var scaledDist             = DiscreteChar.Uniform();

            scaledDist.SetToPartialUniformOf(dist, logProbabilityOverride);
            var scaledLogProbLetter = scaledDist.GetLogProb('j');
            var scaledLogProbNumber = scaledDist.GetLogProb('5');

            Assert.Equal(scaledLogProbLetter, logProbabilityOverride, Eps);
            Assert.Equal(scaledLogProbNumber, logProbabilityOverride, Eps);

            // Check that cache has not been compromised.
            Assert.Equal(probLetter, Math.Exp(dist.GetLogProb('j')), Eps);
            Assert.Equal(probNumber, Math.Exp(dist.GetLogProb('5')), Eps);

            // Check that an exception is thrown if a bad maximumProbability is passed down.
            Xunit.Assert.Throws <ArgumentException>(() =>
            {
                var badDist = DiscreteChar.Uniform();
                badDist.SetToPartialUniformOf(dist, Math.Log(1.2));
            });
        }
예제 #12
0
 /// <summary>
 /// Samples the data from the model.
 /// </summary>
 /// <param name="sequenceCount">The number of sequences to sample.</param>
 /// <param name="sequenceLength">The length of a sequence.</param>
 /// <param name="motifPresenceProbability">The probability that a sequence will contain the motif.</param>
 /// <param name="motif">The position frequency matrix defining the motif.</param>
 /// <param name="backgroundDist">The background nucleobase distribution.</param>
 /// <param name="sequenceData">The sampled sequences.</param>
 /// <param name="motifPositionData">
 /// The motif positions in the sampled sequences.
 /// If the sequence doesn't contain the motif, the position is set to -1.
 /// </param>
 private static void SampleMotifData(
     int sequenceCount,
     int sequenceLength,
     double motifPresenceProbability,
     DiscreteChar[] motif,
     DiscreteChar backgroundDist,
     out string[] sequenceData,
     out int[] motifPositionData)
 {
     sequenceData      = new string[sequenceCount];
     motifPositionData = new int[sequenceCount];
     for (int i = 0; i < sequenceCount; ++i)
     {
         if (Rand.Double() < motifPresenceProbability)
         {
             motifPositionData[i] = Rand.Int(sequenceLength - motif.Length + 1);
             var backgroundBeforeChars = Util.ArrayInit(motifPositionData[i], j => backgroundDist.Sample());
             var backgroundAfterChars  = Util.ArrayInit(sequenceLength - motif.Length - motifPositionData[i], j => backgroundDist.Sample());
             var sampledMotifChars     = Util.ArrayInit(motif.Length, j => motif[j].Sample());
             sequenceData[i] = new string(backgroundBeforeChars) + new string(sampledMotifChars) + new string(backgroundAfterChars);
         }
         else
         {
             motifPositionData[i] = -1;
             var background = Util.ArrayInit(sequenceLength, j => backgroundDist.Sample());
             sequenceData[i] = new string(background);
         }
     }
 }
예제 #13
0
        public void Repeat2()
        {
            var baseDist = StringDistribution.OneOf("a", "b");
            var dist1    = StringDistribution.Repeat(baseDist, minTimes: 1, maxTimes: 3);
            var dist2    = StringDistribution.Repeat(DiscreteChar.OneOf('a', 'b'), minTimes: 1, maxTimes: 3);

            Assert.Equal(dist2, dist1);
        }
예제 #14
0
        public void Product2()
        {
            var ab   = StringDistribution.ZeroOrMore(DiscreteChar.OneOf('a', 'b'));
            var a    = StringDistribution.ZeroOrMore('a');
            var prod = ab.Product(a);

            StringInferenceTestUtilities.TestProbability(prod, 1.0, string.Empty, "a", "aa", "aaa");
            StringInferenceTestUtilities.TestProbability(prod, 0.0, "b", "bb", "ab", "ba");
        }
예제 #15
0
        public void HasLogOverride()
        {
            var dist1 = DiscreteChar.LetterOrDigit();

            Xunit.Assert.False(dist1.HasLogProbabilityOverride);

            dist1.SetToPartialUniformOf(dist1, Math.Log(0.9));
            Xunit.Assert.True(dist1.HasLogProbabilityOverride);
        }
예제 #16
0
        public void BroadAndNarrow()
        {
            var dist1 = DiscreteChar.Digit();

            Xunit.Assert.True(dist1.IsBroad);

            var dist2 = DiscreteChar.OneOf('1', '3', '5', '6');

            Xunit.Assert.False(dist2.IsBroad);
        }
예제 #17
0
 public void CommonChars()
 {
     TestSupport("digit", DiscreteChar.Digit(), "0123456789", "Ab !Ј");
     TestSupport("lower", DiscreteChar.Lower(), "abcdefghixyz", "ABC0123, ");
     TestSupport("upper", DiscreteChar.Upper(), "ABCDEFGHUXYZ", "abc0123, ");
     TestSupport("letter", DiscreteChar.Letter(), "aBcDeFgGhxyzXYZ", "0123! ,");
     TestSupport("letterOrDigit", DiscreteChar.LetterOrDigit(), "abcABC0123xyzXYZ789", " !Ј$,");
     TestSupport("wordChar", DiscreteChar.WordChar(), "abc_ABC_0123s", " !:.,");
     TestSupport("whitespace", DiscreteChar.Whitespace(), " \t", "abcABC0123,:!");
 }
예제 #18
0
        private static StringDistribution NamePrior()
        {
            //TODO: make this closer to:
            // NP([\s\-]NP)*(\s[""\(]NP[""\)])?([\s\-]NP)+
            var result = NamePart();

            result.AppendInPlace(DiscreteChar.OneOf(' ', '-'));
            result.AppendInPlace(NamePart());

            return(result);
        }
예제 #19
0
        public static DiscreteChar CharacterAverageConditional(string str)
        {
            Argument.CheckIfNotNull(str, "str");

            if (str.Length == 1)
            {
                return(DiscreteChar.PointMass(str[0]));
            }

            throw new AllZeroException("The length of the given string is not 1.");
        }
예제 #20
0
        /// <include file='FactorDocs.xml' path='factor_docs/message_op_class[@name="StringOfLengthOp"]/message_doc[@name="StrAverageConditional(DiscreteChar, Discrete)"]/*'/>
        public static StringDistribution StrAverageConditional(DiscreteChar allowedChars, Discrete length)
        {
            Argument.CheckIfNotNull(length, "length");
            Argument.CheckIfValid(allowedChars.IsPartialUniform(), "allowedChars", "The set of allowed characters must be passed as a partial uniform distribution.");

            double logNormalizer  = allowedChars.GetLogAverageOf(allowedChars);
            var    oneCharacter   = StringAutomaton.ConstantOnElementLog(logNormalizer, allowedChars.WrappedDistribution);
            var    manyCharacters = StringAutomaton.Repeat(oneCharacter, length.GetWorkspace());

            return(StringDistribution.FromWeightFunction(manyCharacters));
        }
예제 #21
0
        /// <summary>
        /// Creates a string distribution <c>P(s) = \prod_i P_i(s_i)^I[i != j]</c>,
        /// where <c>P_i(c)</c> is a given array of character distributions and <c>j</c> is a given position in the array.
        /// </summary>
        /// <param name="characters">The distributions over individual characters.</param>
        /// <param name="excludedPos">The character to skip.</param>
        /// <returns>The created distribution.</returns>
        private static StringDistribution GetCharWeighter(IList <DiscreteChar> characters, int excludedPos)
        {
            StringDistribution result = StringDistribution.Empty();

            for (int i = 0; i < characters.Count; ++i)
            {
                result.AppendInPlace(i == excludedPos ? DiscreteChar.Uniform() : characters[i]);
            }

            return(result);
        }
예제 #22
0
        public void GetOutgoingTransitionsForDeterminization1()
        {
            var builder = new StringAutomaton.Builder();

            builder.Start.AddTransition(DiscreteChar.Uniform(), Weight.FromValue(2));

            var wrapper = new StringAutomatonWrapper(builder);

            var outgoingTransitions =
                wrapper.GetOutgoingTransitionsForDeterminization(0, Weight.FromValue(3));
            var expectedOutgoingTransitions = new[]
예제 #23
0
        /// <summary>
        /// Creates a distribution over characters that correspond to nucleobases.
        /// </summary>
        /// <param name="a">The probability of adenine.</param>
        /// <param name="c">The probability of cytosine.</param>
        /// <param name="g">The probability of guanine.</param>
        /// <param name="t">The probability of thymine.</param>
        /// <returns>The created distribution.</returns>
        private static DiscreteChar NucleobaseDist(double a, double c, double g, double t)
        {
            Vector probs = PiecewiseVector.Zero(char.MaxValue + 1);

            probs['A'] = a;
            probs['C'] = c;
            probs['G'] = g;
            probs['T'] = t;

            return(DiscreteChar.FromVector(probs));
        }
예제 #24
0
        public void CharDistribution()
        {
            var rng  = DiscreteChar.UniformInRanges("bdgi");
            var unif = DiscreteChar.Uniform();
            var mix  = new DiscreteChar();

            mix.SetToSum(0.8, rng, 0.2, unif);

            DistributionTests.DistributionTest(unif, mix, false);
            DistributionTests.PointMassTest(mix, 'b');
            DistributionTests.UniformTest(rng, 'b');
        }
예제 #25
0
        /// <summary>EP message to <c>length</c>.</summary>
        /// <param name="str">Incoming message from <c>str</c>.</param>
        /// <param name="allowedChars">Constant value for <c>allowedChars</c>.</param>
        /// <param name="result">Modified to contain the outgoing message.</param>
        /// <returns>
        ///   <paramref name="result" />
        /// </returns>
        /// <remarks>
        ///   <para>The outgoing message is a distribution matching the moments of <c>length</c> as the random arguments are varied. The formula is <c>proj[p(length) sum_(str) p(str) factor(str,length,allowedChars)]/p(length)</c>.</para>
        /// </remarks>
        public static Discrete LengthAverageConditional(StringDistribution str, DiscreteChar allowedChars, Discrete result)
        {
            Vector resultProbabilities = result.GetWorkspace();

            for (int length = 0; length < result.Dimension; ++length)
            {
                StringDistribution factor = StringDistribution.Repeat(allowedChars, length, length);
                resultProbabilities[length] = Math.Exp(factor.GetLogAverageOf(str));
            }

            result.SetProbs(resultProbabilities);
            return(result);
        }
예제 #26
0
        public void ReplaceElements()
        {
            StringTransducer replace = StringTransducer.Replace(DiscreteChar.Lower(), DiscreteChar.Digit());

            StringInferenceTestUtilities.TestTransducerValue(replace, "hello", "123", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, "w", "1337", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, "w", string.Empty, 1.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, string.Empty, "17", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, string.Empty, string.Empty, 1.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, "123", "worlds", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, "123", "123", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, "1", string.Empty, 0.0);
        }
예제 #27
0
        public void BadRanges()
        {
            try
            {
                var a = DiscreteChar.UniformInRanges("aавz");
            }
            catch
            {
                return;
            }

            Assert.True(false);
        }
예제 #28
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();
        }
예제 #29
0
        public static StringDistribution EmptyOrStartsWith(DiscreteChar charsInMainString, DiscreteChar startsWith)
        {
            // TODO: fix equality and then use factory methods to create this
            var result = new StringAutomaton.Builder();

            result.Start.SetEndWeight(Weight.One);
            var otherState = result.Start.AddTransition(startsWith, Weight.FromLogValue(-startsWith.GetLogAverageOf(startsWith)));

            otherState.AddSelfTransition(charsInMainString, Weight.FromLogValue(-charsInMainString.GetLogAverageOf(charsInMainString)));
            otherState.SetEndWeight(Weight.One);

            return(StringDistribution.FromWeightFunction(result.GetAutomaton()));
        }
예제 #30
0
        public void GetOutgoingTransitionsForDeterminization3()
        {
            var builder = new StringAutomaton.Builder();

            builder.Start.AddTransition(DiscreteChar.UniformInRange('a', 'b'), Weight.FromValue(2));
            builder.Start.AddTransition(DiscreteChar.UniformInRanges('b', 'd'), Weight.FromValue(3));
            builder.Start.AddTransition(DiscreteChar.UniformInRanges('e', 'g'), Weight.FromValue(4));
            builder.Start.AddTransition(DiscreteChar.UniformInRanges(char.MinValue, 'a'), Weight.FromValue(5));

            var wrapper = new StringAutomatonWrapper(builder);

            var outgoingTransitions =
                wrapper.GetOutgoingTransitionsForDeterminization(new Dictionary <int, Weight> {
                { 0, Weight.FromValue(6) }
            });
            var expectedOutgoingTransitions = new[]
            {
                new ValueTuple <DiscreteChar, Weight, IEnumerable <KeyValuePair <int, Weight> > >(
                    DiscreteChar.UniformInRange(char.MinValue, (char)('a' - 1)),
                    Weight.FromValue(30.0 * 97.0 / 98.0),
                    new Dictionary <int, Weight> {
                    { 4, Weight.FromValue(1) }
                }),
                new ValueTuple <DiscreteChar, Weight, IEnumerable <KeyValuePair <int, Weight> > >(
                    DiscreteChar.PointMass('a'),
                    Weight.FromValue((30.0 / 98.0) + 6.0),
                    new Dictionary <int, Weight> {
                    { 1, Weight.FromValue(6.0 / ((30.0 / 98.0) + 6.0)) }, { 4, Weight.FromValue((30.0 / 98.0) / ((30.0 / 98.0) + 6.0)) }
                }),
                new ValueTuple <DiscreteChar, Weight, IEnumerable <KeyValuePair <int, Weight> > >(
                    DiscreteChar.PointMass('b'),
                    Weight.FromValue(12.0),
                    new Dictionary <int, Weight> {
                    { 1, Weight.FromValue(0.5) }, { 2, Weight.FromValue(0.5) }
                }),
                new ValueTuple <DiscreteChar, Weight, IEnumerable <KeyValuePair <int, Weight> > >(
                    DiscreteChar.UniformInRange('c', 'd'),
                    Weight.FromValue(12.0),
                    new Dictionary <int, Weight> {
                    { 2, Weight.FromValue(1.0) }
                }),
                new ValueTuple <DiscreteChar, Weight, IEnumerable <KeyValuePair <int, Weight> > >(
                    DiscreteChar.UniformInRange('e', 'g'),
                    Weight.FromValue(24.0),
                    new Dictionary <int, Weight> {
                    { 3, Weight.FromValue(1.0) }
                }),
            };

            AssertCollectionsEqual(expectedOutgoingTransitions, outgoingTransitions, TransitionInfoEqualityComparer.Instance);
        }