def test_transformShouldNotChange(self):
     g = Grammar(nonterminals=[S, A,B,C],
                 rules=[Rules])
     ContextFree.transform_to_chomsky_normal_form(g)
     self.assertEqual(g.rules_count(), 1)
     self.assertEqual(len(g.rules()), 1)
     self.assertEqual(g.rules()[0], Rules)
Exemplo n.º 2
0
 def test_transformShouldNotChange(self):
     g = Grammar(terminals=['a', 'b'],
                 nonterminals=[S, A, B],
                 rules=[Rules])
     ContextFree.transform_to_chomsky_normal_form(g)
     self.assertEqual(g.rules_count(), 7)
     self.assertEqual(len(g.rules()), 7)
     self.assertEqual(g.nonterms_count(), 3)
     self.assertEqual(len(g.nonterms()), 3)
     self.assertTrue(g.have_rule(Rules))
 def setUp(self):
     self.g = Grammar(terminals=[My],
                      nonterminals=[S],
                      rules=[R],
                      start_symbol=S)
     ContextFree.remove_useless_symbols(self.g, transform_grammar=True)
     ContextFree.remove_rules_with_epsilon(self.g, transform_grammar=True)
     ContextFree.remove_unit_rules(self.g, transform_grammar=True)
     ContextFree.remove_useless_symbols(self.g, transform_grammar=True)
     ContextFree.transform_to_chomsky_normal_form(self.g, transform_grammar=True)
 def test_transformShouldChange(self):
     g = Grammar(nonterminals=[S, A, B, C],
                 rules=[Rules])
     ContextFree.transform_to_chomsky_normal_form(g, transform_grammar=True)
     self.assertEqual(g.rules_count(), 2)
     self.assertEqual(len(g.rules()), 2)
     fromS = list(filter(lambda r: r.fromSymbol == S, g.rules()))[0]
     self.assertEqual(fromS.right[0], A)
     temp = fromS.right[1]
     fromTemp = list(filter(lambda r: r.right == [B, C], g.rules()))[0]
     self.assertEqual(temp, fromTemp.fromSymbol)
     self.assertEqual(g.nonterms_count(), 5)
     self.assertEqual(len(g.nonterms()), 5)
Exemplo n.º 5
0
 def test_transformShouldChange(self):
     g = Grammar(terminals=['a', 'b'],
                 nonterminals=[S, A, B],
                 rules=[Rules])
     ContextFree.transform_to_chomsky_normal_form(g, transform_grammar=True)
     self.assertEqual(g.rules_count(), 13)
     self.assertEqual(len(g.rules()), 13)
     self.assertEqual(g.nonterms_count(), 9)
     self.assertEqual(len(g.nonterms()), 9)
     # Old
     class AaRule(Rule): rule=([A], ['a'])
     self.assertTrue(g.have_rule(AaRule))
     class BAARule(Rule): rule=([B], [A, A])
     self.assertTrue(g.have_rule(BAARule))
     # New
     StoaB = list(filter(lambda r: r.fromSymbol == S and r.right[1] == B, g.rules()))[0]
     special_a = StoaB.right[0]
     StobA = list(filter(lambda r: r.fromSymbol == S and r.right[1] == A, g.rules()))[0]
     special_b = StobA.right[0]
     class StoaBRule(Rule): rule=([S], [special_a, B])
     self.assertTrue(g.have_rule(StoaBRule))
     class StobARule(Rule): rule=([S], [special_b, A])
     self.assertTrue(g.have_rule(StobARule))
     class AtoaSRule(Rule): rule=([A], [special_a, S])
     self.assertTrue(g.have_rule(AtoaSRule))
     AtobAA = list(filter(lambda r: r.fromSymbol == A and r.right[0] == special_b, g.rules()))[0]
     new_AA = AtobAA.right[1]
     class AtoabAARule(Rule): rule=([A], [special_b, new_AA])
     self.assertTrue(g.have_rule(AtoabAARule))
     BtoaBBaA = list(filter(lambda r: r.fromSymbol == B and r.right[0] == special_a, g.rules()))[0]
     new_BBaA = BtoaBBaA.right[1]
     class BtoaBBaA(Rule): rule=([B], [special_a, new_BBaA])
     self.assertTrue(g.have_rule(BtoaBBaA))
     class AAtoAARule(Rule): rule=([new_AA], [A, A])
     self.assertTrue(g.have_rule(AAtoAARule))
     BBaAtoBBaA = list(filter(lambda r: r.fromSymbol == new_BBaA, g.rules()))[0]
     self.assertEqual(BBaAtoBBaA.right[0], B)
     new_BaA = BBaAtoBBaA.right[1]
     BaAtoBaA = list(filter(lambda r: r.fromSymbol == new_BaA, g.rules()))[0]
     self.assertEqual(BaAtoBaA.right[0], B)
     new_aA = BaAtoBaA.right[1]
     class aAtoaARule(Rule): rule = ([new_aA], [special_a, A])
     self.assertTrue(g.have_rule(aAtoaARule))
     # Terminals
     class atoaRule(Rule): rule=([special_a], ['a'])
     self.assertTrue(g.have_rule(atoaRule))
     class btobRule(Rule): rule=([special_b], ['b'])
     self.assertTrue(g.have_rule(btobRule))
 def test_transformShouldChange(self):
     g = Grammar(terminals=[0, 1], nonterminals=[S], rules=[Rules])
     ContextFree.transform_to_chomsky_normal_form(g, transform_grammar=True)
     self.assertEqual(g.rules_count(), 3)
     self.assertEqual(len(g.rules()), 3)
     fromS = list(filter(lambda r: r.fromSymbol == S, g.rules()))[0]
     to0 = fromS.right[0]
     to1 = fromS.right[1]
     self.assertTrue(issubclass(to0, ContextFree.ChomskyTermNonterminal))
     self.assertTrue(issubclass(to1, ContextFree.ChomskyTermNonterminal))
     to0R = list(filter(lambda r: r.right == [0], g.rules()))[0]
     to1R = list(filter(lambda r: r.right == [1], g.rules()))[0]
     self.assertEqual(to0R.fromSymbol, to0)
     self.assertEqual(to1R.fromSymbol, to1)
     self.assertEqual(g.nonterms_count(), 3)
     self.assertEqual(len(g.nonterms()), 3)
Exemplo n.º 7
0
 def test_simpleTestOverUnitRule(self):
     g = Grammar(terminals=[0, 1],
                 nonterminals=[S, A, B, C, D],
                 rules=[RuleS1AB, RuleB0, RuleACD, RuleCD, RuleDEps],
                 start_symbol=S)
     gr = ContextFree.transform_to_chomsky_normal_form(ContextFree.remove_unit_rules(ContextFree.remove_rules_with_epsilon(g)))
     pars = cyk(gr, [1, 0])
     s = InverseContextFree.epsilon_rules_restore(InverseContextFree.unit_rules_restore(InverseContextFree.transform_from_chomsky_normal_form(pars)))
     self.assertIsInstance(s, S)
     self.assertIsInstance(s.to_rule, RuleS1AB)
     self.assertIsInstance(s.to_rule.to_symbols[0], Terminal)
     self.assertEqual(s.to_rule.to_symbols[0].s, 1)
     b = s.to_rule.to_symbols[2]
     self.assertIsInstance(b, B)
     self.assertIsInstance(b.to_rule, RuleB0)
     self.assertIsInstance(b.to_rule.to_symbols[0], Terminal)
     self.assertEqual(b.to_rule.to_symbols[0].s, 0)
     a = s.to_rule.to_symbols[1]
     self.assertIsInstance(a, A)
     self.assertIsInstance(a.to_rule, RuleACD)
     d = a.to_rule.to_symbols[1]
     self.assertIsInstance(d, D)
     self.assertIsInstance(d.to_rule, RuleDEps)
     self.assertIs(d.to_rule.to_symbols[0].s, EPS)
     c = a.to_rule.to_symbols[0]
     self.assertIsInstance(c, C)
     self.assertIsInstance(c.to_rule, RuleCD)
     d = c.to_rule.to_symbols[0]
     self.assertIsInstance(d, D)
     self.assertIsInstance(d.to_rule, RuleDEps)
     self.assertIs(d.to_rule.to_symbols[0].s, EPS)
 def test_repeatOfC(self):
     g = Grammar(terminals=[0, 1, 2, 3],
                 nonterminals=[S, A, B, C, D],
                 rules=[
                     RuleSA, RuleSB, RuleAC, RuleA0A, RuleBD, RuleB2B,
                     RuleB3S, RuleC1C, RuleC0, RuleD3D, RuleD2
                 ],
                 start_symbol=S)
     gr = ContextFree.transform_to_chomsky_normal_form(
         ContextFree.remove_unit_rules(g))
     pars = cyk(gr, [1, 1, 0])
     s = InverseContextFree.unit_rules_restore(
         InverseContextFree.transform_from_chomsky_normal_form(pars))
     self.assertIsInstance(s, S)
     self.assertIsInstance(s.to_rule, RuleSA)
     a = s.to_rule.to_symbols[0]
     self.assertIsInstance(a, A)
     self.assertIsInstance(a.to_rule, RuleAC)
     c = a.to_rule.to_symbols[0]
     self.assertIsInstance(c, C)
     self.assertIsInstance(c.to_rule, RuleC1C)
     self.assertIsInstance(c.to_rule.to_symbols[0], Terminal)
     self.assertEqual(c.to_rule.to_symbols[0].s, 1)
     c = c.to_rule.to_symbols[1]
     self.assertIsInstance(c, C)
     self.assertIsInstance(c.to_rule, RuleC1C)
     self.assertIsInstance(c.to_rule.to_symbols[0], Terminal)
     self.assertEqual(c.to_rule.to_symbols[0].s, 1)
     c = c.to_rule.to_symbols[1]
     self.assertIsInstance(c, C)
     self.assertIsInstance(c.to_rule, RuleC0)
     self.assertIsInstance(c.to_rule.to_symbols[0], Terminal)
     self.assertEqual(c.to_rule.to_symbols[0].s, 0)
 def test_transform(self):
     g = Grammar(terminals=[0, 1, 2],
                 nonterminals=[S, A, B, C],
                 rules=[RuleSABC, RuleA0, RuleB1, RuleC2],
                 start_symbol=S)
     com = ContextFree.transform_to_chomsky_normal_form(g)
     pars = cyk(com, [0, 1, 2])
     trans = InverseContextFree.transform_from_chomsky_normal_form(pars)
     self.assertIsInstance(trans, S)
     self.assertIsInstance(trans.to_rule, RuleSABC)
     a = trans.to_rule.to_symbols[0]
     b = trans.to_rule.to_symbols[1]
     c = trans.to_rule.to_symbols[2]
     self.assertIsInstance(a, A)
     self.assertIsInstance(a.to_rule, RuleA0)
     self.assertIsInstance(a.to_rule.to_symbols[0], Terminal)
     self.assertEqual(a.to_rule.to_symbols[0].s, 0)
     self.assertIsInstance(b, B)
     self.assertIsInstance(b.to_rule, RuleB1)
     self.assertIsInstance(b.to_rule.to_symbols[0], Terminal)
     self.assertEqual(b.to_rule.to_symbols[0].s, 1)
     self.assertIsInstance(c, C)
     self.assertIsInstance(c.to_rule, RuleC2)
     self.assertIsInstance(c.to_rule.to_symbols[0], Terminal)
     self.assertEqual(c.to_rule.to_symbols[0].s, 2)
 def test_transform(self):
     g = Grammar(
         terminals=[0, 1, 2, 3],
         nonterminals=[S, A, B, C],
         rules=[RuleS, RuleA, RuleB, RuleS0, RuleA1, RuleB2, RuleC3],
         start_symbol=S)
     gr = ContextFree.transform_to_chomsky_normal_form(g)
     pars = cyk(gr, [1, 3, 1, 2, 3, 1, 3])
     trans = InverseContextFree.transform_from_chomsky_normal_form(pars)
     self.assertIsInstance(trans, S)
     self.assertIsInstance(trans.to_rule, RuleS)
     a = trans.to_rule.to_symbols[0]
     self.assertIsInstance(a, A)
     self.assertIsInstance(a.to_rule, RuleA1)
     self.assertIsInstance(a.to_rule.to_symbols[0], Terminal)
     self.assertEqual(a.to_rule.to_symbols[0].s, 1)
     b = trans.to_rule.to_symbols[1]
     self.assertIsInstance(b, B)
     self.assertIsInstance(b.to_rule, RuleB)
     c = b.to_rule.to_symbols[0]
     self.assertIsInstance(c, C)
     self.assertIsInstance(c.to_rule, RuleC3)
     self.assertIsInstance(c.to_rule.to_symbols[0], Terminal)
     self.assertEqual(c.to_rule.to_symbols[0].s, 3)
     s = b.to_rule.to_symbols[1]
     self.assertIsInstance(s, S)
     self.assertIsInstance(s.to_rule, RuleS)
     self.assertIsInstance(s.to_rule.to_symbols[0], A)
     self.assertIsInstance(s.to_rule.to_symbols[0].to_rule, RuleA1)
     self.assertIsInstance(s.to_rule.to_symbols[0].to_rule.to_symbols[0],
                           Terminal)
     self.assertEqual(s.to_rule.to_symbols[0].to_rule.to_symbols[0].s, 1)
     self.assertIsInstance(s.to_rule.to_symbols[1], B)
     self.assertIsInstance(s.to_rule.to_symbols[1].to_rule, RuleB2)
     self.assertIsInstance(s.to_rule.to_symbols[1].to_rule.to_symbols[0],
                           Terminal)
     self.assertEqual(s.to_rule.to_symbols[1].to_rule.to_symbols[0].s, 2)
     self.assertIsInstance(s.to_rule.to_symbols[2], C)
     self.assertIsInstance(s.to_rule.to_symbols[2].to_rule, RuleC3)
     self.assertIsInstance(s.to_rule.to_symbols[2].to_rule.to_symbols[0],
                           Terminal)
     self.assertEqual(s.to_rule.to_symbols[2].to_rule.to_symbols[0].s, 3)
     self.assertIsInstance(b.to_rule.to_symbols[2], A)
     self.assertIsInstance(b.to_rule.to_symbols[2].to_rule, RuleA1)
     self.assertIsInstance(b.to_rule.to_symbols[2].to_rule.to_symbols[0],
                           Terminal)
     self.assertEqual(b.to_rule.to_symbols[2].to_rule.to_symbols[0].s, 1)
     self.assertIsInstance(pars.to_rule.to_symbols[2], C)
     self.assertIsInstance(pars.to_rule.to_symbols[2].to_rule, RuleC3)
     self.assertIsInstance(pars.to_rule.to_symbols[2].to_rule.to_symbols[0],
                           Terminal)
     self.assertEqual(pars.to_rule.to_symbols[2].to_rule.to_symbols[0].s, 3)
Exemplo n.º 11
0
 def test_transformShouldChange(self):
     g = Grammar(nonterminals=[S, A, B, C], rules=[Rules])
     ContextFree.transform_to_chomsky_normal_form(g, transform_grammar=True)
     self.assertEqual(g.rules_count(), 6)
     self.assertEqual(len(g.rules()), 6)
     fromS = list(filter(lambda r: r.fromSymbol == S, g.rules()))[0]
     self.assertEqual(fromS.right[0], A)
     tempS = fromS.right[1]
     fromSTemp = list(filter(lambda r: r.right == [B, C], g.rules()))[0]
     self.assertEqual(tempS, fromSTemp.fromSymbol)
     fromA = list(filter(lambda r: r.fromSymbol == A, g.rules()))[0]
     self.assertEqual(fromA.right[0], B)
     tempA = fromA.right[1]
     fromATemp = list(filter(lambda r: r.right == [C, S], g.rules()))[0]
     self.assertEqual(tempA, fromATemp.fromSymbol)
     fromB = list(filter(lambda r: r.fromSymbol == B, g.rules()))[0]
     self.assertEqual(fromB.right[0], C)
     tempB = fromB.right[1]
     fromBTemp = list(filter(lambda r: r.right == [S, A], g.rules()))[0]
     self.assertEqual(tempB, fromBTemp.fromSymbol)
     self.assertEqual(g.nonterms_count(), 7)
     self.assertEqual(len(g.nonterms()), 7)
Exemplo n.º 12
0
 def test_transform(self):
     g = Grammar(terminals=[0, 1],
                 nonterminals=[S],
                 rules=[Rules],
                 start_symbol=S)
     tr = ContextFree.transform_to_chomsky_normal_form(g)
     pars = cyk(tr, [0, 1])
     rest = InverseContextFree.transform_from_chomsky_normal_form(pars)
     self.assertIsInstance(rest, S)
     self.assertIsInstance(rest.to_rule, Rules)
     self.assertIsInstance(rest.to_rule.to_symbols[0], Terminal)
     self.assertIsInstance(rest.to_rule.to_symbols[1], Terminal)
     self.assertEqual(rest.to_rule.to_symbols[0].s, 0)
     self.assertEqual(rest.to_rule.to_symbols[1].s, 1)
Exemplo n.º 13
0
 def test_simpleTestOverUnitRule(self):
     g = Grammar(
         terminals=[0, 1, 2, 3],
         nonterminals=[S, A, B, C],
         rules=[RuleS0A, RuleAB, RuleAEps, RuleBEps, RuleB1C, RuleC11],
         start_symbol=S)
     gr = ContextFree.transform_to_chomsky_normal_form(
         ContextFree.remove_unit_rules(
             ContextFree.remove_rules_with_epsilon(g)))
     pars = cyk(gr, [0])
     s = InverseContextFree.epsilon_rules_restore(
         InverseContextFree.unit_rules_restore(
             InverseContextFree.transform_from_chomsky_normal_form(pars)))
     self.assertIsInstance(s, S)
     self.assertIsInstance(s.to_rule, RuleS0A)
     self.assertIsInstance(s.to_rule.to_symbols[0], Terminal)
     self.assertEqual(s.to_rule.to_symbols[0].s, 0)
     a = s.to_rule.to_symbols[1]
     self.assertIsInstance(a, A)
     self.assertIsInstance(a.to_rule, RuleAEps)
     self.assertIs(a.to_rule.to_symbols[0].s, EPS)
Exemplo n.º 14
0
 def test_directTo2(self):
     g = Grammar(terminals=[0, 1, 2, 3],
                 nonterminals=[S, A, B, C, D],
                 rules=[
                     RuleSA, RuleSB, RuleAC, RuleA0A, RuleA1S, RuleBD,
                     RuleB2B, RuleB3S, RuleC1C, RuleC0, RuleD3D, RuleD2
                 ],
                 start_symbol=S)
     gr = ContextFree.transform_to_chomsky_normal_form(
         ContextFree.remove_unit_rules(g))
     pars = cyk(gr, [2])
     s = InverseContextFree.unit_rules_restore(
         InverseContextFree.transform_from_chomsky_normal_form(pars))
     self.assertIsInstance(s, S)
     self.assertIsInstance(s.to_rule, RuleSB)
     b = s.to_rule.to_symbols[0]
     self.assertIsInstance(b, B)
     self.assertIsInstance(b.to_rule, RuleBD)
     d = b.to_rule.to_symbols[0]
     self.assertIsInstance(d, D)
     self.assertIsInstance(d.to_rule, RuleD2)
     self.assertIsInstance(d.to_rule.to_symbols[0], Terminal)
     self.assertEqual(d.to_rule.to_symbols[0].s, 2)