Beispiel #1
0
        public void TestAccepts03()
        {
            var productions = new HashSet <Production> {
                CFGParser.Production("<S> -> <S> <S> <S>"),
                CFGParser.Production("<S> -> ε"),
                CFGParser.Production("<S> -> 'x2' 'x0'"),
                CFGParser.Production("<S> -> 'x0' <S>"),
                CFGParser.Production("<S> -> 'x4'"),
                CFGParser.Production("<S> -> <S> 'x0' 'x4'"),
                CFGParser.Production("<S> -> 'x3' <S> 'x3' <S> 'x0'"),
                CFGParser.Production("<S> -> 'x2' <S> <S> <S> <S>"),
                CFGParser.Production("<S> -> <S> <S> <S> <S> <S>"),
                CFGParser.Production("<S> -> 'x0' <S> <S>"),
                CFGParser.Production("<S> -> <S>"),
                CFGParser.Production("<S> -> 'x0' <S> <S> 'x1'"),
                CFGParser.Production("<S> -> 'x3' 'x2' 'x1'"),
                CFGParser.Production("<S> -> 'x0' 'x0' 'x2'"),
            };
            Grammar    g = new Grammar(productions, Nonterminal.Of("S"));
            CNFGrammar h = g.ToCNF();

            Assert.IsTrue(h.Accepts(Sentence.FromLetters("")));
            Assert.IsTrue(h.Accepts(Sentence.FromWords("x4")));
            Assert.IsTrue(h.Accepts(Sentence.FromWords("x4 x0 x4")));
        }
Beispiel #2
0
 public void TestMissingStart01()
 {
     var productions = new List <Production> {
         CFGParser.Production(@"<X_0> -> <X_0> <X_0>"),
         CFGParser.Production(@"<X_0> -> 'a'"),
     };
     Grammar    g = new Grammar(productions, Nonterminal.Of("S"));
     CNFGrammar h = g.ToCNF();
 }
Beispiel #3
0
        public void TestMissingStart02()
        {
            var productions = new List <Production> {
                CFGParser.Production(@"<X_0> -> 'a'"),
            };
            CNFGrammar h = new CNFGrammar(productions, Nonterminal.Of("S"));

            Helpers.IsNear(0.0, h.Cyk(Sentence.FromLetters("a")));
        }
Beispiel #4
0
        public void TestProduceToDepth()
        {
            var g = new CNFGrammar(
                Enumerable.Empty <Production>(),
                Nonterminal.Of("S")
                );

            Assert.IsTrue(g.ProduceToDepth(1).Count == 0);
            Assert.IsTrue(g.ProduceToDepth(2).Count == 0);
        }
Beispiel #5
0
        public void TestToCNF06()
        {
            Grammar g = new Grammar(new List <Production> {
                CFGParser.Production("<S> → ε"),
                CFGParser.Production("<S> → 'x' <A>")
            }, Nonterminal.Of("S"));
            CNFGrammar h = g.ToCNF();

            Helpers.AssertNear(1.0, h.Cyk(Sentence.FromWords("")));
        }
Beispiel #6
0
        public void TestFreshNames()
        {
            var productions = new List <Production> {
                CFGParser.Production(@"<S> -> 'a'"),
                CFGParser.Production(@"<X_0> -> 'b'"),
            };
            Grammar    g = new Grammar(productions, Nonterminal.Of("S"));
            CNFGrammar h = g.ToCNF();

            Assert.IsTrue(h.Accepts(Sentence.FromLetters("a")));
            Assert.IsFalse(h.Accepts(Sentence.FromLetters("b")));
        }
Beispiel #7
0
        public void TestAccepts09()
        {
            var productions = new HashSet <Production> {
                CFGParser.Production("<S> -> ε"),
                // CFGParser.Production("<S> -> 'x'"),
                CFGParser.Production("<S> -> <S> <S> <S>"),
            };
            Grammar    g = new Grammar(productions, Nonterminal.Of("S"));
            CNFGrammar h = g.ToCNF();

            Assert.IsTrue(h.Accepts(Sentence.FromLetters("")));
        }
Beispiel #8
0
        public void TestToCNF01()
        {
            var productions = new List <Production> {
                CFGParser.Production(@"<X_0> -> 'x3' <X_0> [23]"),
                CFGParser.Production(@"<X_0> -> ε [85]"),
            };
            Grammar    g = new Grammar(productions, Nonterminal.Of("X_0"));
            CNFGrammar h = g.ToCNF();

            Helpers.AssertNear(85.0 / 108.0, h.Cyk(Sentence.FromLetters("")));
            Helpers.AssertNear((23.0 / 108.0) * (85.0 / 108.0), h.Cyk(Sentence.FromWords("x3")));
            Helpers.AssertNear((23.0 / 108.0) * (23.0 / 108.0) * (85.0 / 108.0), h.Cyk(Sentence.FromWords("x3 x3")));
        }
Beispiel #9
0
        private static void ExecuteTest(Grammar g, List <Sentence> sentences)
        {
            CNFGrammar h      = g.ToCNF();
            var        earley = new EarleyParser(g);
            var        cyk    = new CykParser(h);

            foreach (var sentence in sentences)
            {
                var p1 = cyk.ParseGetProbability(sentence);
                var p2 = earley.ParseGetProbability(sentence);
                Helpers.AssertNear(p1, p2);
            }
        }
Beispiel #10
0
        public void TestCNFNoNull02()
        {
            var productions = new HashSet <Production> {
                CFGParser.Production("<A> -> <A>"),
                CFGParser.Production("<A> -> 'a'"),
                CFGParser.Production("<A> -> 'b'"),
            };

            Grammar    g = new Grammar(productions, Nonterminal.Of("A"));
            CNFGrammar h = g.ToCNF();

            Helpers.AssertNear(0, h.Cyk(Sentence.FromLetters("")));
            Helpers.AssertNear(0.5, h.Cyk(Sentence.FromLetters("a")));
            Helpers.AssertNear(0.5, h.Cyk(Sentence.FromLetters("b")));
        }
Beispiel #11
0
        public void TestUnitProductions01()
        {
            var productions = new List <Production> {
                CFGParser.Production(@"<S> -> <A> [3]"),
                CFGParser.Production(@"<S> -> <B> [1]"),
                CFGParser.Production(@"<A> -> 'a'"),
                CFGParser.Production(@"<B> -> 'b'"),
            };
            Grammar    g = new Grammar(productions, Nonterminal.Of("S"));
            CNFGrammar h = g.ToCNF();

            Helpers.AssertNear(0.0, h.Cyk(Sentence.FromLetters("")));
            Helpers.AssertNear(0.75, h.Cyk(Sentence.FromLetters("a")));
            Helpers.AssertNear(0.25, h.Cyk(Sentence.FromLetters("b")));
        }
Beispiel #12
0
        public void TestAccepts02()
        {
            var productions = new HashSet <Production> {
                CFGParser.Production("<X_0> -> <X_0> 'x4' <X_0> 'x0'"),
                CFGParser.Production("<X_0> -> <X_0> <X_0> 'x2' <X_0> 'x3'"),
                CFGParser.Production("<X_0> -> <X_0> 'x1' <X_0>"),
                CFGParser.Production("<X_0> -> <X_0> 'x1' 'x1' 'x1' 'x3'"),
                CFGParser.Production("<X_0> -> ε"),
            };
            Grammar    g = new Grammar(productions, Nonterminal.Of("X_0"));
            CNFGrammar h = g.ToCNF();

            Assert.IsTrue(h.Accepts(Sentence.FromLetters("")));
            Assert.IsTrue(h.Accepts(Sentence.FromWords("x4 x0")));
            Assert.IsTrue(h.Accepts(Sentence.FromWords("x4 x0 x4 x2 x3 x0")));
        }
Beispiel #13
0
        public void TestHugeWeights()
        {
            var productions = new List <Production> {
                CFGParser.Production(@"<S> -> <A> <B> [3000000000]"),
                CFGParser.Production(@"<S> -> <C> <A> [3000000000]"),
                CFGParser.Production(@"<S> -> ε [3000000000]"),
                CFGParser.Production(@"<A> -> 'a'"),
                CFGParser.Production(@"<B> -> 'b'"),
                CFGParser.Production(@"<C> -> 'c'"),
            };
            var g = new CNFGrammar(productions, Nonterminal.Of("S"));

            Helpers.AssertNear(1.0 / 3.0, g.Cyk(Sentence.FromLetters("")));
            Helpers.AssertNear(1.0 / 3.0, g.Cyk(Sentence.FromLetters("ab")));
            Helpers.AssertNear(1.0 / 3.0, g.Cyk(Sentence.FromLetters("ca")));
        }
Beispiel #14
0
        public void TestCFGToCNFBadProb01()
        {
            // S -> aSa | bSb | ε
            var productions = new List <Production> {
                CFGParser.Production(@"<S> -> 'a' <S> 'a' [1]"),
                CFGParser.Production(@"<S> -> 'b' <S> 'b' [3]"),
                CFGParser.Production(@"<S> -> ε [4]"),
            };
            Grammar    g = new Grammar(productions, Nonterminal.Of("S"));
            CNFGrammar h = g.ToCNF();

            Helpers.AssertNear(0.5, h.Cyk(Sentence.FromLetters("")));
            Helpers.AssertNear((1.0 / 8) * 0.5, h.Cyk(Sentence.FromLetters("aa")));
            Helpers.AssertNear((3.0 / 8) * 0.5, h.Cyk(Sentence.FromLetters("bb")));
            Helpers.AssertNear((1.0 / 8) * (3.0 / 8) * 0.5, h.Cyk(Sentence.FromLetters("abba")));
        }
Beispiel #15
0
        public void TestToCNF05()
        {
            Grammar g = new Grammar(new List <Production> {
                CFGParser.Production("<X_0> → 'x4' <X_4> [16.517998587115667]"),
                CFGParser.Production("<X_4> → 'x3' [49.290950734303777]"),
                CFGParser.Production("<X_0> → 'x4' 'x1' [23.628313965456705]")
            }, Nonterminal.Of("X_0"));
            CNFGrammar h = g.ToCNF();

            var first = 16.517998587115667;
            var third = 23.628313965456705;
            var sum   = first + third;

            Helpers.AssertNear(third / sum, h.Cyk(Sentence.FromWords("x4 x1")));
            Helpers.AssertNear(first / sum, h.Cyk(Sentence.FromWords("x4 x3")));
        }
Beispiel #16
0
        public void TestToCNF04()
        {
            var productions = new HashSet <Production> {
                CFGParser.Production("<A> -> <A> <B>"),
                CFGParser.Production("<A> -> ε"),
                CFGParser.Production("<B> -> 'b'"),
                CFGParser.Production("<B> -> ε"),
            };

            Grammar    g = new Grammar(productions, Nonterminal.Of("A"));
            CNFGrammar h = g.ToCNF();

            var third = 1.0 / 3.0;

            Helpers.AssertNear(0.5 + third * 0.5, h.Cyk(Sentence.FromLetters("")));
            Helpers.AssertNear(third * 2.0 / 3.0, h.Cyk(Sentence.FromLetters("b")));
        }
Beispiel #17
0
        public void TestToCNF02()
        {
            var productions = new List <Production> {
                CFGParser.Production(@"<S> -> 'a' <B> <B>"),
                CFGParser.Production(@"<B> -> 'b'"),
                CFGParser.Production(@"<B> -> ε"),
            };
            Grammar    g = new Grammar(productions, Nonterminal.Of("S"));
            CNFGrammar h = g.ToCNF();

            var pa   = h.Cyk(Sentence.FromLetters("a"));
            var pab  = h.Cyk(Sentence.FromLetters("ab"));
            var pabb = h.Cyk(Sentence.FromLetters("abb"));

            Assert.IsTrue(pa > 0.0);
            Assert.IsTrue(pab > 0.0);
            Assert.IsTrue(pabb > 0.0);
            Helpers.AssertNear(1.0, pa + pab + pabb);
        }
Beispiel #18
0
        public void TestToCNF07()
        {
            Grammar g = new Grammar(new List <Production> {
                CFGParser.Production("<X_0> → 'x1' 'x4' <X_2> <X_6> [48.024797111295534]"),
                CFGParser.Production("<X_1> → <X_0> 'x0' [28.859845304796398]"),
                CFGParser.Production("<X_4> → 'x4' 'x4' 'x4' <X_3> <X_6> [32.396577754708275]"),
                CFGParser.Production("<X_4> → ε [46.519217869974312]"),
                CFGParser.Production("<X_0> → 'x4' 'x0' 'x3' 'x3' [90.918005973993814]"),
                CFGParser.Production("<X_6> → <X_4> <X_0> 'x3' <X_3> [31.319837867431264]"),
                CFGParser.Production("<X_2> → <X_2> [72.917730323932]"),
                CFGParser.Production("<X_3> → <X_1> [36.901735786302822]"),
                CFGParser.Production("<X_6> → ε [40.092343899464396]"),
                CFGParser.Production("<X_2> → 'x1' [16.801839537826293]"),
                CFGParser.Production("<X_0> → ε [136.892430380868]")
            }, Nonterminal.Of("X_0"));
            CNFGrammar h = g.ToCNF();

            // Helpers.AssertNear(1.0, h.Cyk(Sentence.FromWords("")));
        }
Beispiel #19
0
        public void TestCYK01()
        {
            var productions = new List <Production> {
                new Production(
                    Nonterminal.Of("S"),
                    new Sentence {
                    Nonterminal.Of("X"), Nonterminal.Of("X")
                },
                    2
                    ),
                new Production(
                    Nonterminal.Of("X"),
                    new Sentence {
                    Nonterminal.Of("X"), Nonterminal.Of("X")
                },
                    2
                    ),
                new Production(
                    Nonterminal.Of("S"),
                    new Sentence {
                    Terminal.Of("a")
                },
                    8
                    ),
                new Production(
                    Nonterminal.Of("X"),
                    new Sentence {
                    Terminal.Of("a")
                },
                    8
                    )
            };

            var g = new CNFGrammar(productions, Nonterminal.Of("S"));

            Helpers.AssertNear(0.8, g.Cyk(Sentence.FromLetters("a")));
            Helpers.AssertNear(0.128, g.Cyk(Sentence.FromLetters("aa")));
            Helpers.AssertNear(0.04096, g.Cyk(Sentence.FromLetters("aaa")));
            Helpers.AssertNear(0.016384, g.Cyk(Sentence.FromLetters("aaaa")));
            Helpers.AssertNear(0.007340032, g.Cyk(Sentence.FromLetters("aaaaa")));
        }
Beispiel #20
0
        public void TestToCNF03()
        {
            var productions = new HashSet <Production> {
                CFGParser.Production("<A> -> <B> <C>"),
                CFGParser.Production("<B> -> <C>"),
                CFGParser.Production("<B> -> 'b'"),
                CFGParser.Production("<B> -> ε"),
                CFGParser.Production("<C> -> <B>"),
                CFGParser.Production("<C> -> 'c'"),
                CFGParser.Production("<C> -> ε"),
            };

            Grammar    g = new Grammar(productions, Nonterminal.Of("A"));
            CNFGrammar h = g.ToCNF();

            Helpers.AssertNear(0.25, h.Cyk(Sentence.FromLetters("")));
            Helpers.AssertNear(0.25, h.Cyk(Sentence.FromLetters("b")));
            Helpers.AssertNear(0.25, h.Cyk(Sentence.FromLetters("c")));
            Helpers.AssertNear(0.140625, h.Cyk(Sentence.FromLetters("bc")));
            Helpers.AssertNear(0.046875, h.Cyk(Sentence.FromLetters("cc")));
            Helpers.AssertNear(0.046875, h.Cyk(Sentence.FromLetters("bb")));
            Helpers.AssertNear(0.015625, h.Cyk(Sentence.FromLetters("cb")));
        }
Beispiel #21
0
 public CykParser(CNFGrammar grammar)
 {
     _grammar = grammar;
 }
Beispiel #22
0
		public CykParser(CNFGrammar grammar) {
			_grammar = grammar;
		}