Represents a concrete (probabilistic) context free grammar in Chomsky normal form (CNF)
Inheritance: BaseGrammar
Example #1
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")));
		}
Example #2
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);
		}
Example #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")));
		}
Example #4
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")));
		}
Example #5
0
 private static void TestGrammar(CNFGrammar rg)
 {
     Console.WriteLine("=====================");
     for (int i = 0; i < 5; i++)
     {
         var swps = rg.ProduceToDepth(i);
         Console.WriteLine("------Depth {0}------", i);
         foreach (var swp in swps)
         {
             var actual   = rg.Cyk(swp.Value);
             var expected = swp.Probability;
             if (actual < expected)
             {
                 Console.WriteLine("{0}, {1}", actual, expected);
                 Console.WriteLine(rg);
                 Console.WriteLine(swp);
             }
             // Console.WriteLine(swp.Sentence.AsTerminals());
             // Console.WriteLine(sentence);
         }
     }
 }
Example #6
0
		public override BaseGrammar ShallowClone() {
			var clone = new CNFGrammar(this.Productions, this.Start);
			return clone;
		}
Example #7
0
		private static void TestGrammar(CNFGrammar rg) {
			Console.WriteLine("=====================");
			for (int i = 0; i < 5; i++) {
				var swps = rg.ProduceToDepth(i);
				Console.WriteLine("------Depth {0}------", i);
				foreach (var swp in swps) {
					var actual = rg.Cyk(swp.Value);
					var expected = swp.Probability;
					if (actual < expected) {
						Console.WriteLine("{0}, {1}", actual, expected);
						Console.WriteLine(rg);
						Console.WriteLine(swp);

					}
					// Console.WriteLine(swp.Sentence.AsTerminals());
					// Console.WriteLine(sentence);
				}
			}
		}
Example #8
0
        public override BaseGrammar ShallowClone()
        {
            var clone = new CNFGrammar(this.Productions, this.Start);

            return(clone);
        }