ToCNF() публичный Метод

Returns a new grammar that is the CNF equivalent of this grammar. WARNING: currently this does not always preserve probabilities!
public ToCNF ( ) : CNFGrammar
Результат CNFGrammar
Пример #1
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();
		}
Пример #2
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);
			}
		}
Пример #3
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")));
		}
Пример #4
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")));
		}
Пример #5
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")));
		}
Пример #6
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")));
		}
Пример #7
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")));
		}
Пример #8
0
		public void TestUnitProductions02() {
			var g = new Grammar(new List<Production>{
				CFGParser.Production("<X_5> → 'x0' [22.103242747999378]"),
				CFGParser.Production("<X_5> → <X_4> <X_2> 'x3' 'x3' <X_5> 'x4' <X_5> [13.799233343824387]"),
				CFGParser.Production("<X_7> → 'x1' <X_0> 'x3' <X_9> 'x1' 'x0' <X_9> <X_5> [95.444187448566865]"),
				CFGParser.Production("<X_7> → ε [92.294680857702474]"),
				CFGParser.Production("<X_2> → <X_9> <X_4> [67.541615066370753]"),
				CFGParser.Production("<X_9> → <X_0> <X_7> 'x3' <X_4> 'x2' 'x1' 'x3' [36.206671634319555]"),
				CFGParser.Production("<X_0> → <X_4> [41.7635276861319]"),
				CFGParser.Production("<X_4> → <X_0> [85.572940545423393]"),
				CFGParser.Production("<X_4> → <X_9> 'x2' 'x1' <X_9> 'x2' 'x0' [19.93626149694261]"),
				CFGParser.Production("<X_0> → 'x3' [13.794410955530783]"),
				CFGParser.Production("<X_9> → ε [86.73295911109679]")
			}, Nonterminal.Of("X_0"));
			var h = g.ToCNF();
		}
Пример #9
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")));
		}
Пример #10
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);
		}
Пример #11
0
		public void TestAccepts06() {
			var productions = new HashSet<Production> {
				CFGParser.Production("<X_3> -> ε"),
				CFGParser.Production("<X_2> -> ε"),
				CFGParser.Production("<X_0> -> <X_2> <X_1>"),
				CFGParser.Production("<X_1> -> <X_3> <X_3>"),
			};
			var g = new Grammar(productions, Nonterminal.Of("X_0"));
			var h = g.ToCNF();

			Assert.IsTrue(h.Accepts(Sentence.FromWords("")));
		}
Пример #12
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("")));
		}
Пример #13
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("")));
		}
Пример #14
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")));
		}
Пример #15
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")));
		}
Пример #16
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")));
		}
Пример #17
0
		public void TestAccepts04() {
			var productions = new HashSet<Production> {
				CFGParser.Production("<X_2> -> <X_3> <X_1>"),
				CFGParser.Production("<X_3> -> <X_1> <X_1>"),
				CFGParser.Production("<X_0> -> 'x2' 'x0' <X_3> <X_2> <X_2>"),
				CFGParser.Production("<X_2> -> 'x0' 'x3' <X_1> 'x0' 'x0'"),
				CFGParser.Production("<X_3> -> <X_1>"),
				CFGParser.Production("<X_2> -> <X_1> 'x4'"),
				CFGParser.Production("<X_1> -> ε"),
				CFGParser.Production("<X_0> -> <X_2>"),
				CFGParser.Production("<X_1> -> 'x4' <X_2> <X_1> 'x0' 'x1'"),
				CFGParser.Production("<X_1> -> <X_2> 'x0' 'x1' <X_2> <X_2>"),
				CFGParser.Production("<X_2> -> 'x1' <X_2> 'x3'"),
				CFGParser.Production("<X_1> -> <X_3> <X_0> <X_2> <X_3>"),
			};
			var g = new Grammar(productions, Nonterminal.Of("X_0"));
			var h = g.ToCNF();

			Assert.IsTrue(h.Accepts(Sentence.FromWords("x2 x0 x0 x3 x0 x0 x4")));
			Assert.IsTrue(h.Accepts(Sentence.FromWords("x2 x0 x4 x4")));
			Assert.IsTrue(h.Accepts(Sentence.FromWords("x0 x3 x0 x0")));
			Assert.IsTrue(h.Accepts(Sentence.FromWords("x2 x0")));
			Assert.IsTrue(h.Accepts(Sentence.FromWords("x4")));
		}
Пример #18
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")));
		}
Пример #19
0
		public void TestProbabilityUnit01() {
			var productions = new HashSet<Production> {
				CFGParser.Production("<A> -> 'a'"),
				CFGParser.Production("<A> -> <B>"),
				CFGParser.Production("<B> -> 'b'"),
				CFGParser.Production("<B> -> <A>"),
			};
			var g = new Grammar(productions, Nonterminal.Of("A"));
			var h = g.ToCNF();

			Helpers.IsNear(2.0 / 3, h.Cyk(Sentence.FromLetters("a")));
			Helpers.IsNear(1.0 / 3, h.Cyk(Sentence.FromLetters("b")));
		}
Пример #20
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("")));
		}
Пример #21
0
		public void TestAccepts07() {
			var productions = new HashSet<Production> {
				CFGParser.Production("<A> -> 'a'"),
				CFGParser.Production("<A> -> <B>"),
				CFGParser.Production("<B> -> 'b'"),
				CFGParser.Production("<B> -> <A>"),
			};
			var g = new Grammar(productions, Nonterminal.Of("A"));
			var h = g.ToCNF();

			Assert.IsTrue(h.Accepts(Sentence.FromWords("a")));
			Assert.IsTrue(h.Accepts(Sentence.FromWords("b")));
		}