Esempio n. 1
0
        public void InferArgumentsFromConcatenationTest2()
        {
            var str1 = Variable.StringUpper(minLength: 0);
            var str2 = Variable.StringLower(minLength: 0);
            var str3 = Variable.StringUpper(minLength: 0);
            var s    = str1 + str2 + str3;

            s.ObservedValue = "ABC";
            var engine = new InferenceEngine();

            var posteriorOverStr1 = engine.Infer <StringDistribution>(str1);

            StringInferenceTestUtilities.TestIfIncludes(posteriorOverStr1, string.Empty, "A", "AB", "ABC");
            StringInferenceTestUtilities.TestIfExcludes(posteriorOverStr1, "B", "BC", "C");

            var posteriorOverStr2 = engine.Infer <StringDistribution>(str2);

            Assert.True(posteriorOverStr2.IsPointMass);
            Assert.Equal(string.Empty, posteriorOverStr2.Point);

            var posteriorOverStr3 = engine.Infer <StringDistribution>(str3);

            StringInferenceTestUtilities.TestIfIncludes(posteriorOverStr3, "ABC", "BC", "C", string.Empty);
            StringInferenceTestUtilities.TestIfExcludes(posteriorOverStr3, "A", "AB", "B");
        }
Esempio n. 2
0
        public void WordMiddleLimitedLength()
        {
            const double Eps = 1e-6;

            var wordMiddleLimitedLength1 = WordStrings.WordMiddle(3, 5);

            StringInferenceTestUtilities.TestIfIncludes(wordMiddleLimitedLength1, " h w ", ". ab.", "...");
            StringInferenceTestUtilities.TestIfExcludes(
                wordMiddleLimitedLength1,
                "hello",
                " w",
                "hi ",
                "1 2",
                ".",
                "..",
                " hello world ",
                string.Empty);
            Assert.Equal(wordMiddleLimitedLength1.GetLogProb(" h w "), wordMiddleLimitedLength1.GetLogProb(" h "), Eps);

            var wordMiddleLimitedLength2 = WordStrings.WordMiddle(1, 5);

            StringInferenceTestUtilities.TestIfIncludes(wordMiddleLimitedLength2, " ", " h w ", ". ab.", "...");
            StringInferenceTestUtilities.TestIfExcludes(wordMiddleLimitedLength2, "hello", " w", "hi ", "1 2", " hello world ", string.Empty);
            Assert.Equal(wordMiddleLimitedLength2.GetLogProb("."), wordMiddleLimitedLength2.GetLogProb(" h "), Eps);

            var wordMiddleLimitedLength3 = WordStrings.WordMiddle(1, 2);

            StringInferenceTestUtilities.TestIfIncludes(wordMiddleLimitedLength3, " ", " .");
            StringInferenceTestUtilities.TestIfExcludes(wordMiddleLimitedLength3, "hw", " w", "h ", "12", " hello world ", string.Empty);
            Assert.Equal(wordMiddleLimitedLength3.GetLogProb("."), wordMiddleLimitedLength3.GetLogProb(".."), Eps);
        }
Esempio n. 3
0
        public void WordMiddle()
        {
            var wordMiddle = WordStrings.WordMiddle();

            StringInferenceTestUtilities.TestIfIncludes(wordMiddle, " hello world ", ". abc.", ". abc. def gh. ", " ", "..");
            StringInferenceTestUtilities.TestIfExcludes(wordMiddle, "hello", " world", "hi ", "1 2", string.Empty);
            Assert.Equal(wordMiddle.GetLogProb(" "), wordMiddle.GetLogProb(" world "));
        }
Esempio n. 4
0
        public void WordSuffix()
        {
            var wordSuffix = WordStrings.WordSuffix();

            StringInferenceTestUtilities.TestIfIncludes(wordSuffix, " hello", string.Empty, ".abc");
            StringInferenceTestUtilities.TestIfExcludes(wordSuffix, "hello", "hello world", "1 2");
            Assert.Equal(wordSuffix.GetLogProb(string.Empty), wordSuffix.GetLogProb(" world"));
            Assert.Equal(wordSuffix.GetLogProb(string.Empty), wordSuffix.GetLogProb(". and that's it"));
        }
Esempio n. 5
0
        public void WordPrefix()
        {
            var wordPrefix = WordStrings.WordPrefix();

            StringInferenceTestUtilities.TestIfIncludes(wordPrefix, "hello ", string.Empty, "abc.");
            StringInferenceTestUtilities.TestIfExcludes(wordPrefix, "hello", "hello world", "1 2");
            Assert.Equal(wordPrefix.GetLogProb(string.Empty), wordPrefix.GetLogProb("hello "));
            Assert.Equal(wordPrefix.GetLogProb(string.Empty), wordPrefix.GetLogProb("hello world and people in it."));
        }
        public void InferStringFromSubstringTest()
        {
            var str    = Variable.StringUniform();
            var substr = Variable.Substring(str, 0, 2);

            substr.ObservedValue = "He";

            var engine       = new InferenceEngine();
            var strPosterior = engine.Infer <StringDistribution>(str);

            StringInferenceTestUtilities.TestIfIncludes(strPosterior, "Hello", "Hell", "He", "He is great");
            StringInferenceTestUtilities.TestIfExcludes(strPosterior, "abc", " ", "H", string.Empty);
        }
        public void InferSubstringFromStringTest()
        {
            Variable <string> str    = Variable.Observed("Hello");
            Variable <string> substr = Variable.Substring(str, 0, 2);

            var engine          = new InferenceEngine();
            var substrPosterior = engine.Infer <StringDistribution>(substr);

            StringInferenceTestUtilities.TestLogProbability(substrPosterior, 0, "He");

            Variable <string> str2    = Variable.Random(StringDistribution.Any());
            Variable <string> substr2 = Variable.Substring(str2, 0, 2);

            var substr2Posterior = engine.Infer <StringDistribution>(substr2);

            StringInferenceTestUtilities.TestIfIncludes(substr2Posterior, "ab", "  ", "dd");
            StringInferenceTestUtilities.TestIfExcludes(substr2Posterior, "abc", " ", string.Empty);
        }
Esempio n. 8
0
        public void InefficientPermutationRepresentation()
        {
            StringDistribution placeholder = StringDistribution.Char('{') + StringDistribution.Char(DiscreteChar.Digit()) + StringDistribution.Char('}');
            StringDistribution formatPrior = placeholder + StringDistribution.String(" ");

            formatPrior = StringDistribution.ZeroOrMore(formatPrior) + placeholder;

            const int ArgCount  = 6;
            const int ArgLength = 3;

            StringDistribution[] args = Util.ArrayInit(ArgCount, i => StringDistribution.String(new string((char)('0' + i), ArgLength)));

            StringDistribution str = StringFormatOp_AllowMissingPlaceholders_NoArgumentNames.StrAverageConditional(formatPrior, args);

            Console.WriteLine(str.GetWorkspaceOrPoint().States.Count);

            StringInferenceTestUtilities.TestIfIncludes(str, "111 333 222", "111 222", "222 111", "111", "222", "333");
            StringInferenceTestUtilities.TestIfExcludes(str, string.Empty, "111 111", "222 222 111", "333 111 222 333", "112", "1");
        }
Esempio n. 9
0
        public void InferArgumentsFromConcatenationTest3()
        {
            var str1 = Variable.StringUpper(minLength: 0);
            var str2 = Variable.StringLower(minLength: 0);
            var str3 = Variable.StringUpper(minLength: 1, maxLength: 1);
            var s    = str1 + str2 + str3;

            Variable.ConstrainEqualRandom(s, StringDistribution.OneOf("AB", "aB"));
            var engine = new InferenceEngine();

            var posteriorOverStr1 = engine.Infer <StringDistribution>(str1);

            StringInferenceTestUtilities.TestIfIncludes(posteriorOverStr1, string.Empty, "A");
            StringInferenceTestUtilities.TestIfExcludes(posteriorOverStr1, "AB", "B");

            var posteriorOverStr2 = engine.Infer <StringDistribution>(str2);

            StringInferenceTestUtilities.TestIfIncludes(posteriorOverStr2, string.Empty, "a");

            var posteriorOverStr3 = engine.Infer <StringDistribution>(str3);

            Assert.True(posteriorOverStr3.IsPointMass);
            Assert.Equal("B", posteriorOverStr3.Point);
        }
Esempio n. 10
0
        public void MessageOperatorsTest()
        {
            //// Messages to concatenation results

            StringDistribution concat1 = StringConcatOp.ConcatAverageConditional(StringDistribution.String("ab"), StringDistribution.String("cd"));

            Assert.True(concat1.IsPointMass && concat1.Point == "abcd");

            StringDistribution concat2 = StringConcatOp.ConcatAverageConditional(StringDistribution.Upper(), StringDistribution.String("cd"));

            StringInferenceTestUtilities.TestIfIncludes(concat2, "Acd", "ABcd");
            StringInferenceTestUtilities.TestIfExcludes(concat2, "cd", "Abcd", "ABc");

            StringDistribution concat3 = StringConcatOp.ConcatAverageConditional(StringDistribution.OneOf("a", "ab"), StringDistribution.OneOf("b", string.Empty));

            StringInferenceTestUtilities.TestProbability(concat3, 0.5, "ab");
            StringInferenceTestUtilities.TestProbability(concat3, 0.25, "a", "abb");

            //// Messages to the first argument

            StringDistribution str11 = StringConcatOp.Str1AverageConditional(StringDistribution.String("abc"), StringDistribution.OneOf("abc", "bc"));

            StringInferenceTestUtilities.TestProbability(str11, 0.5, string.Empty, "a");

            StringDistribution str12 = StringConcatOp.Str1AverageConditional(StringDistribution.String("abc"), StringDistribution.OneOf("abc", "bc", "a", "b"));

            StringInferenceTestUtilities.TestProbability(str12, 0.5, string.Empty, "a");

            StringDistribution str13 = StringConcatOp.Str1AverageConditional(StringDistribution.OneOf("aa", "aaa"), StringDistribution.OneOf("a", "aa"));

            StringInferenceTestUtilities.TestProbability(str13, 0.5, "a");
            StringInferenceTestUtilities.TestProbability(str13, 0.25, string.Empty, "aa");

            StringDistribution str14 = StringConcatOp.Str1AverageConditional(StringDistribution.OneOf("abc", "abd"), StringDistribution.OneOf("bc", "d"));

            StringInferenceTestUtilities.TestProbability(str14, 0.5, "a", "ab");

            StringDistribution str15 = StringConcatOp.Str1AverageConditional(StringDistribution.OneOf("abc", "abd"), StringDistribution.OneOf("bc", "bd"));

            Assert.True(str15.IsPointMass && str15.Point == "a");

            //// Messages to the second argument

            StringDistribution str21 = StringConcatOp.Str2AverageConditional(StringDistribution.String("abc"), StringDistribution.OneOf("abc", "ab"));

            StringInferenceTestUtilities.TestProbability(str21, 0.5, string.Empty, "c");

            StringDistribution str22 = StringConcatOp.Str2AverageConditional(StringDistribution.String("abc"), StringDistribution.OneOf("abc", "ab", "c", "b"));

            StringInferenceTestUtilities.TestProbability(str22, 0.5, string.Empty, "c");

            StringDistribution str23 = StringConcatOp.Str2AverageConditional(StringDistribution.OneOf("aa", "aaa"), StringDistribution.OneOf("a", "aa"));

            StringInferenceTestUtilities.TestProbability(str23, 0.5, "a");
            StringInferenceTestUtilities.TestProbability(str23, 0.25, string.Empty, "aa");

            StringDistribution str24 = StringConcatOp.Str2AverageConditional(StringDistribution.OneOf("abc", "dbc"), StringDistribution.OneOf("ab", "d"));

            StringInferenceTestUtilities.TestProbability(str24, 0.5, "c", "bc");

            StringDistribution str25 = StringConcatOp.Str2AverageConditional(StringDistribution.OneOf("abc", "dbc"), StringDistribution.OneOf("ab", "db"));

            Assert.True(str25.IsPointMass && str25.Point == "c");

            //// Evidence messages

            const double EvidenceEps = 1e-6;

            Assert.Equal(0.0, StringConcatOp.LogEvidenceRatio(StringDistribution.Any(minLength: 1, maxLength: 5), StringDistribution.Capitalized(minLength: 2, maxLength: 2), StringDistribution.Upper()), EvidenceEps);
            Assert.Equal(
                Math.Log(1.0 / 3.0),
                StringConcatOp.LogEvidenceRatio("aaba", StringDistribution.OneOf("a", "aa"), StringDistribution.OneOf("a", "ba", "aba")),
                EvidenceEps);
            Assert.True(double.IsNegativeInfinity(
                            StringConcatOp.LogEvidenceRatio("aaba", StringDistribution.OneOf("a", "aa"), StringDistribution.OneOf("a", "bd", "abd"))));

            //// Incompatible message parameters

            Assert.True(StringConcatOp.Str1AverageConditional(StringDistribution.OneOf("abc", "abd"), StringDistribution.OneOf("ab", "b")).IsZero());
            Assert.True(StringConcatOp.Str2AverageConditional(StringDistribution.OneOf("abc", "abd"), StringDistribution.OneOf("bc", "b")).IsZero());
        }
Esempio n. 11
0
        public void MessageOperatorsTest()
        {
            //// Messages to substring

            StringDistribution substr1 = SubstringOp.SubAverageConditional("abc", 1, 1);

            Assert.True(substr1.IsPointMass && substr1.Point == "b");

            StringDistribution substr2 = SubstringOp.SubAverageConditional("abc", 0, 3);

            Assert.True(substr2.IsPointMass && substr2.Point == "abc");

            StringDistribution substr3 = SubstringOp.SubAverageConditional(StringDistribution.String("abc"), 1, 1);

            Assert.True(substr3.IsPointMass && substr3.Point == "b");

            StringDistribution substr4 = SubstringOp.SubAverageConditional(StringDistribution.String("abc"), 0, 3);

            Assert.True(substr4.IsPointMass && substr4.Point == "abc");

            StringDistribution substr5 = SubstringOp.SubAverageConditional(StringDistribution.Any(), 0, 2);

            StringInferenceTestUtilities.TestIfIncludes(substr5, "ab", "  ", "17");
            StringInferenceTestUtilities.TestIfExcludes(substr5, "abb", " ", "177", string.Empty);

            StringDistribution substr6 = SubstringOp.SubAverageConditional(StringDistribution.OneOf("abc", "abd", "de"), 0, 2);

            StringInferenceTestUtilities.TestProbability(substr6, 2.0 / 3.0, "ab");
            StringInferenceTestUtilities.TestProbability(substr6, 1.0 / 3.0, "de");

            StringDistribution substr7 = SubstringOp.SubAverageConditional(StringDistribution.OneOf("abc", "abd", "de", "d", "c", string.Empty), 0, 2);

            StringInferenceTestUtilities.TestProbability(substr7, 2.0 / 3.0, "ab");
            StringInferenceTestUtilities.TestProbability(substr7, 1.0 / 3.0, "de");

            //// Messages to string

            StringDistribution str1 = SubstringOp.StrAverageConditional("sss", 1, 3);

            StringInferenceTestUtilities.TestIfIncludes(str1, "asss", "asssa", "bsssa");
            StringInferenceTestUtilities.TestIfExcludes(str1, "sss", "assa", "basssa", string.Empty);

            StringDistribution str2 = SubstringOp.StrAverageConditional("sss", 0, 3);

            StringInferenceTestUtilities.TestIfIncludes(str2, "sss", "sssa", "sssab");
            StringInferenceTestUtilities.TestIfExcludes(str2, "asss", "basssa", "ssa", string.Empty);

            StringDistribution str3 = SubstringOp.StrAverageConditional(StringDistribution.String("sss"), 1, 3);

            StringInferenceTestUtilities.TestIfIncludes(str3, "asss", "asssa", "bsssa");
            StringInferenceTestUtilities.TestIfExcludes(str3, "sss", "assa", "basssa", string.Empty);

            StringDistribution str4 = SubstringOp.StrAverageConditional(StringDistribution.String("sss"), 0, 3);

            StringInferenceTestUtilities.TestIfIncludes(str4, "sss", "sssa", "sssab");
            StringInferenceTestUtilities.TestIfExcludes(str4, "asss", "basssa", "ssa", string.Empty);

            StringDistribution str5 = SubstringOp.StrAverageConditional(StringDistribution.Capitalized(minLength: 3, maxLength: 3), 0, 3);

            StringInferenceTestUtilities.TestIfIncludes(str5, "Bbb", "Baba", "Bbb ab");
            StringInferenceTestUtilities.TestIfExcludes(str5, "BAba", "aaaB", "ABab", "Bb ab", string.Empty);

            StringDistribution str6 = SubstringOp.StrAverageConditional(StringDistribution.Upper(minLength: 0, maxLength: 5), 0, 2);

            StringInferenceTestUtilities.TestIfIncludes(str6, "BBb", "BAba", "BBB ab");
            StringInferenceTestUtilities.TestIfExcludes(str6, "Baba", "aaaB", "aBab", "bb ab", string.Empty);

            //// Evidence messages

            const double EvidenceEps = 1e-6;

            Assert.Equal(0.0, SubstringOp.LogEvidenceRatio(StringDistribution.Any(minLength: 1, maxLength: 5), StringDistribution.Capitalized(2, 2), 1, 2), EvidenceEps);
            Assert.Equal(Math.Log(2.0 / 3.0), SubstringOp.LogEvidenceRatio(StringDistribution.OneOf("baa", "bab", "baab"), "aa", 1, 2), EvidenceEps);
            Assert.True(double.IsNegativeInfinity(SubstringOp.LogEvidenceRatio(StringDistribution.Any(minLength: 1, maxLength: 5), "string", 1, 3)));
            Assert.True(double.IsNegativeInfinity(SubstringOp.LogEvidenceRatio(StringDistribution.Any(minLength: 1, maxLength: 2), "str", 1, 3)));

            //// Incompatible message parameters

            Assert.True(SubstringOp.SubAverageConditional("abc", 1, 3).IsZero());
            Assert.True(SubstringOp.SubAverageConditional(StringDistribution.Any(minLength: 1, maxLength: 2), 1, 3).IsZero());
            Assert.True(SubstringOp.StrAverageConditional("abc", 1, 4).IsZero());
            Assert.True(SubstringOp.StrAverageConditional(StringDistribution.Any(minLength: 1, maxLength: 2), 1, 3).IsZero());
        }