def test_simpleChainingTestShouldNotChange(self): g = Grammar(terminals=[0, 1], nonterminals=[S, A, B, C], rules=[Rules], start_symbol=S) ContextFree.remove_rules_with_epsilon(g) self.assertEqual(len(g.rules()), 8) class RuleNewSto0(Rule): rule = ([S], [0]) class RuleNewSto1(Rule): rule = ([S], [1]) class RuleNewStoC(Rule): rule = ([S], [C]) class RuleNewStoEPS(Rule): rule = ([S], [EPS]) self.assertFalse(g.have_rule(RuleNewSto0)) self.assertFalse(g.have_rule(RuleNewSto1)) self.assertFalse(g.have_rule(RuleNewStoC)) self.assertFalse(g.have_rule(RuleNewStoEPS)) class RuleOldBtoEps(Rule): rule = ([B], [EPS]) self.assertTrue(g.have_rule(RuleOldBtoEps))
def test_simpleTestShouldChange(self): g = Grammar(terminals=[1], nonterminals=[S, A, B, C], rules=[Rules], start_symbol=S) ContextFree.remove_rules_with_epsilon(g, transform_grammar=True) self.assertEqual(g.rules_count(), 6) class RuleNewS(Rule): rule=([S], [1]) class RuleNewA(Rule): rule=([A], [1]) self.assertTrue(g.have_rule(RuleNewS)) self.assertTrue(g.have_rule(RuleNewA)) fromS = g.get_rule(RuleNewS) self.assertEqual(fromS.fromSymbol, S) self.assertEqual(fromS.toSymbol, 1) self.assertTrue(isclass(fromS)) self.assertTrue(issubclass(fromS, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromS.from_rule.rule, ([S], [1, B])) self.assertEqual(fromS.replace_index, 1) fromA = g.get_rule(RuleNewA) self.assertEqual(fromA.fromSymbol, A) self.assertEqual(fromA.toSymbol, 1) self.assertTrue(isclass(fromA)) self.assertTrue(issubclass(fromA, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromA.from_rule.rule, ([A], [1, B])) self.assertEqual(fromA.replace_index, 1) class OldA(Rule): rule=([A], [EPS]) class OldB(Rule): rule=([B], [EPS]) self.assertFalse(g.have_rule(OldA)) self.assertFalse(g.have_rule(OldB))
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_simpleTestShouldNotChange(self): g = Grammar(terminals=[1], nonterminals=[S, A, B, C], rules=[Rules]) ContextFree.remove_rules_with_epsilon(g) self.assertEqual(g.rules_count(), 6) class RuleNewS(Rule): rule=([S], [1]) class RuleNewA(Rule): rule=([A], [1]) self.assertFalse(g.have_rule(RuleNewS)) self.assertFalse(g.have_rule(RuleNewA)) class OldA(Rule): rule=([A], [EPS]) class OldB(Rule): rule=([B], [EPS]) self.assertTrue(g.have_rule(OldA)) self.assertTrue(g.have_rule(OldB))
def test_simpleChainingTestShouldChange(self): g = Grammar(terminals=[0, 1], nonterminals=[S, A, B, C], rules=[Rules], start_symbol=S) ContextFree.remove_rules_with_epsilon(g, transform_grammar=True) self.assertEqual(len(g.rules()), 11) class RuleNewSto0(Rule): rule = ([S], [0]) class RuleNewSto1(Rule): rule = ([S], [1]) class RuleNewStoC(Rule): rule = ([S], [C]) class RuleNewStoEPS(Rule): rule = ([S], [EPS]) self.assertTrue( g.have_rule([RuleNewSto0, RuleNewSto1, RuleNewStoC, RuleNewStoEPS])) fromSto0 = g.get_rule(RuleNewSto0) self.assertTrue(isclass(fromSto0)) self.assertTrue(issubclass(fromSto0, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromSto0.from_rule.rule, ([S], [0, A])) self.assertEqual(fromSto0.replace_index, 1) fromSto1 = g.get_rule(RuleNewSto1) self.assertTrue(isclass(fromSto1)) self.assertTrue(issubclass(fromSto1, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromSto1.from_rule.rule, ([S], [1, C])) self.assertEqual(fromSto1.replace_index, 1) fromStoC = g.get_rule(RuleNewStoC) self.assertTrue(isclass(fromStoC)) self.assertTrue(issubclass(fromStoC, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromStoC.from_rule.rule, ([S], [C, C])) self.assertEqual(fromStoC.replace_index, 0) fromStoEPS = g.get_rule(RuleNewStoEPS) self.assertTrue(isclass(fromStoEPS)) self.assertTrue(issubclass(fromStoEPS, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromStoEPS.from_rule.rule, ([S], [C])) self.assertEqual(fromStoEPS.replace_index, 0) class RuleOldBtoEps(Rule): rule = ([B], [EPS]) self.assertFalse(g.have_rule(RuleOldBtoEps))
def test_simpleTest(self): g = Grammar(terminals=[1], nonterminals=[S, A, B, C], rules=[Rules]) com = ContextFree.remove_rules_with_epsilon(g) self.assertEqual(com.rules_count(), 6) class RuleNewS(Rule): rule=([S], [1]) class RuleNewA(Rule): rule=([A], [1]) self.assertTrue(com.have_rule(RuleNewS)) self.assertTrue(com.have_rule(RuleNewA)) fromS = com.get_rule(RuleNewS) self.assertEqual(fromS.fromSymbol, S) self.assertEqual(fromS.toSymbol, 1) self.assertTrue(isclass(fromS)) self.assertTrue(issubclass(fromS, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromS.from_rule.rule, ([S], [1, B])) self.assertEqual(fromS.replace_index, 1) fromA = com.get_rule(RuleNewA) self.assertEqual(fromA.fromSymbol, A) self.assertEqual(fromA.toSymbol, 1) self.assertTrue(isclass(fromA)) self.assertTrue(issubclass(fromA, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromA.from_rule.rule, ([A], [1, B])) self.assertEqual(fromA.replace_index, 1) class OldA(Rule): rule=([A], [EPS]) class OldB(Rule): rule=([B], [EPS]) self.assertFalse(com.have_rule(OldA)) self.assertFalse(com.have_rule(OldB))
def test_multipleUsageShouldNotChange(self): g = Grammar(terminals=[0, 1, 2, 3], nonterminals=[S, A, B, C, D], rules=[Rules], start_symbol=S) ContextFree.remove_rules_with_epsilon(g) self.assertEqual(len(g.rules()), 12) self.assertEqual(g.rules_count(), 12) class RuleNewAto0A(Rule): rule = ([A], [0, A]) class RuleNewAtoA0(Rule): rule = ([A], [A, 0]) class RuleNewAtoB(Rule): rule = ([A], [B]) class RuleNewAtoC(Rule): rule = ([A], [C]) class RuleNewAtoCC(Rule): rule = ([A], [C, C]) class RuleNewAto1(Rule): rule = ([A], [1]) class RuleNewBto3(Rule): rule = ([B], [3]) class RuleNewCtoA3(Rule): rule = ([C], [A, 3]) class RuleNewDtoAB(Rule): rule = ([D], [A, B]) class RuleNewDtoAA(Rule): rule = ([D], [A, A]) class RuleNewDtoA3(Rule): rule = ([D], [A, 3]) class RuleNewCto3(Rule): rule = ([C], [3]) class RuleNewDtoA(Rule): rule = ([D], [A]) class RuleNewDtoB(Rule): rule = ([D], [B]) class RuleNewDto3(Rule): rule = ([D], [3]) self.assertFalse(g.have_rule(RuleNewAto0A)) self.assertFalse(g.have_rule(RuleNewAtoA0)) self.assertFalse(g.have_rule(RuleNewAtoB)) self.assertFalse(g.have_rule(RuleNewAtoC)) self.assertFalse(g.have_rule(RuleNewAtoCC)) self.assertFalse(g.have_rule(RuleNewAto1)) self.assertFalse(g.have_rule(RuleNewBto3)) self.assertFalse(g.have_rule(RuleNewCtoA3)) self.assertFalse(g.have_rule(RuleNewDtoAB)) self.assertFalse(g.have_rule(RuleNewDtoAA)) self.assertFalse(g.have_rule(RuleNewDtoA3)) self.assertFalse(g.have_rule(RuleNewCto3)) self.assertFalse(g.have_rule(RuleNewDtoA)) self.assertFalse(g.have_rule(RuleNewDtoB)) self.assertFalse(g.have_rule(RuleNewDto3)) class BtoEps(Rule): rule=([B], [EPS]) class CtoEps(Rule): rule=([B], [EPS]) self.assertTrue(g.have_rule(BtoEps)) self.assertTrue(g.have_rule(CtoEps))
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)
def test_multipleUsage(self): g = Grammar(terminals=[0, 1, 2, 3], nonterminals=[S, A, B, C, D], rules=[Rules], start_symbol=S) com = ContextFree.remove_rules_with_epsilon(g) self.assertEqual(len(com.rules()), 25) self.assertEqual(com.rules_count(), 25) class RuleNewSto0A(Rule): rule = ([S], [0, A]) self.assertTrue(com.have_rule(RuleNewSto0A)) fromSto0A = com.get_rule(RuleNewSto0A) self.assertTrue(isclass(fromSto0A)) self.assertTrue(issubclass(fromSto0A, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromSto0A.from_rule.rule, ([S], [A, 0, A])) self.assertEqual(fromSto0A.replace_index, 0) class RuleNewStoA0(Rule): rule = ([S], [A, 0]) self.assertTrue(com.have_rule(RuleNewStoA0)) fromStoA0 = com.get_rule(RuleNewStoA0) self.assertTrue(isclass(fromStoA0)) self.assertTrue(issubclass(fromStoA0, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromStoA0.from_rule.rule, ([S], [A, 0, A])) self.assertEqual(fromStoA0.replace_index, 2) class RuleNewAtoB(Rule): rule = ([A], [B]) self.assertTrue(com.have_rule(RuleNewAtoB)) fromAtoB = com.get_rule(RuleNewAtoB) self.assertTrue(isclass(fromAtoB)) self.assertTrue(issubclass(fromAtoB, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromAtoB.from_rule.rule, ([A], [B, C])) self.assertEqual(fromAtoB.replace_index, 1) class RuleNewAtoC(Rule): rule = ([A], [C]) self.assertTrue(com.have_rule(RuleNewAtoC)) fromAtoC = com.get_rule(RuleNewAtoC) self.assertTrue(isclass(fromAtoC)) self.assertTrue(issubclass(fromAtoC, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromAtoC.from_rule.rule, ([A], [B, C])) self.assertEqual(fromAtoC.replace_index, 0) class RuleNewAtoCC(Rule): rule = ([A], [C, C]) self.assertTrue(com.have_rule(RuleNewAtoCC)) fromAtoCC = com.get_rule(RuleNewAtoCC) self.assertTrue(isclass(fromAtoCC)) self.assertTrue(issubclass(fromAtoCC, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromAtoCC.from_rule.rule, ([A], [C, C, C])) self.assertEqual(fromAtoCC.replace_index, 0) class RuleNewBto1(Rule): rule = ([B], [1]) self.assertTrue(com.have_rule(RuleNewBto1)) fromBto1 = com.get_rule(RuleNewBto1) self.assertTrue(isclass(fromBto1)) self.assertTrue(issubclass(fromBto1, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromBto1.from_rule.rule, ([B], [1, C])) self.assertEqual(fromBto1.replace_index, 1) class RuleNewBto3(Rule): rule = ([B], [3]) self.assertTrue(com.have_rule(RuleNewBto3)) fromBto3 = com.get_rule(RuleNewBto3) self.assertTrue(isclass(fromBto3)) self.assertTrue(issubclass(fromBto3, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromBto3.from_rule.rule, ([B], [3, D])) self.assertEqual(fromBto3.replace_index, 1) class RuleNewCtoA3(Rule): rule = ([C], [A, 3]) self.assertTrue(com.have_rule(RuleNewCtoA3)) fromCtoA3 = com.get_rule(RuleNewCtoA3) self.assertTrue(isclass(fromCtoA3)) self.assertTrue(issubclass(fromCtoA3, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromCtoA3.from_rule.rule, ([C], [A, A, 3])) self.assertEqual(fromCtoA3.replace_index, 0) class RuleNewDtoAB(Rule): rule = ([D], [A, B]) self.assertTrue(com.have_rule(RuleNewDtoAB)) fromDtoAB = com.get_rule(RuleNewDtoAB) self.assertTrue(isclass(fromDtoAB)) self.assertTrue(issubclass(fromDtoAB, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromDtoAB.from_rule.rule, ([D], [A, A, B])) self.assertEqual(fromDtoAB.replace_index, 0) class RuleNewDtoAA(Rule): rule = ([D], [A, A]) self.assertTrue(com.have_rule(RuleNewDtoAA)) fromDtoAA = com.get_rule(RuleNewDtoAA) self.assertTrue(isclass(fromDtoAA)) self.assertTrue(issubclass(fromDtoAA, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromDtoAA.from_rule.rule, ([D], [A, A, B])) self.assertEqual(fromDtoAA.replace_index, 2) class RuleNewDtoA3(Rule): rule = ([D], [A, 3]) self.assertTrue(com.have_rule(RuleNewDtoA3)) fromDtoA3 = com.get_rule(RuleNewDtoA3) self.assertTrue(isclass(fromDtoA3)) self.assertTrue(issubclass(fromDtoA3, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromDtoA3.from_rule.rule, ([D], [A, A, 3])) self.assertEqual(fromDtoA3.replace_index, 0) class RuleNewCto3(Rule): rule = ([C], [3]) self.assertTrue(com.have_rule(RuleNewCto3)) fromCto3 = com.get_rule(RuleNewCto3) self.assertTrue(isclass(fromCto3)) self.assertTrue(issubclass(fromCto3, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromCto3.from_rule.rule, ([C], [A, 3])) self.assertEqual(fromCto3.replace_index, 0) class RuleNewDtoA(Rule): rule = ([D], [A]) self.assertTrue(com.have_rule(RuleNewDtoA)) fromDtoA = com.get_rule(RuleNewDtoA) self.assertTrue(isclass(fromDtoA)) self.assertTrue(issubclass(fromDtoA, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromDtoA.from_rule.rule, ([D], [A, B])) self.assertEqual(fromDtoA.replace_index, 1) class RuleNewDtoB(Rule): rule = ([D], [B]) self.assertTrue(com.have_rule(RuleNewDtoB)) fromDtoB = com.get_rule(RuleNewDtoB) self.assertTrue(isclass(fromDtoB)) self.assertTrue(issubclass(fromDtoB, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromDtoB.from_rule.rule, ([D], [A, B])) self.assertEqual(fromDtoB.replace_index, 0) class RuleNewDto3(Rule): rule = ([D], [3]) self.assertTrue(com.have_rule(RuleNewDto3)) fromDto3 = com.get_rule(RuleNewDto3) self.assertTrue(isclass(fromDto3)) self.assertTrue(issubclass(fromDto3, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromDto3.from_rule.rule, ([D], [A, 3])) self.assertEqual(fromDto3.replace_index, 0) class BtoEps(Rule): rule=([B], [EPS]) class CtoEps(Rule): rule=([B], [EPS]) self.assertFalse(com.have_rule(BtoEps)) self.assertFalse(com.have_rule(CtoEps))
def test_simpleChainingTest(self): g = Grammar(terminals=[0, 1], nonterminals=[S, A, B, C], rules=[Rules], start_symbol=S) com = ContextFree.remove_rules_with_epsilon(g) self.assertEqual(len(com.rules()), 12) class RuleNewStoBC(Rule): rule = ([S], [B, C]) class RuleNewStoAC(Rule): rule = ([S], [A, C]) class RuleNewStoAB(Rule): rule = ([S], [A, B]) class RuleNewAto0(Rule): rule = ([A], [0]) class RuleNewStoB(Rule): rule = ([S], [B]) class RuleNewStoC(Rule): rule = ([S], [C]) class RuleNewStoA(Rule): rule = ([S], [A]) class RuleNewStoEPS(Rule): rule = ([S], [EPS]) self.assertTrue( com.have_rule([ RuleNewStoBC, RuleNewStoAC, RuleNewStoAB, RuleNewAto0, RuleNewStoB, RuleNewStoC, RuleNewStoA, RuleNewStoEPS ])) fromStoBC = com.get_rule(RuleNewStoBC) self.assertTrue(isclass(fromStoBC)) self.assertTrue(issubclass(fromStoBC, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromStoBC.from_rule.rule, ([S], [A, B, C])) self.assertEqual(fromStoBC.replace_index, 0) fromStoAC = com.get_rule(RuleNewStoAC) self.assertTrue(isclass(fromStoAC)) self.assertTrue(issubclass(fromStoAC, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromStoAC.from_rule.rule, ([S], [A, B, C])) self.assertEqual(fromStoAC.replace_index, 1) fromStoAB = com.get_rule(RuleNewStoAB) self.assertTrue(isclass(fromStoAB)) self.assertTrue(issubclass(fromStoAB, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromStoAB.from_rule.rule, ([S], [A, B, C])) self.assertEqual(fromStoAB.replace_index, 2) fromAto0 = com.get_rule(RuleNewAto0) self.assertTrue(isclass(fromAto0)) self.assertTrue(issubclass(fromAto0, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromAto0.from_rule.rule, ([A], [0, A])) self.assertEqual(fromAto0.replace_index, 1) fromStoA = com.get_rule(RuleNewStoA) self.assertTrue(isclass(fromStoA)) self.assertTrue(issubclass(fromStoA, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromStoA.from_rule.rule, ([S], [A, C])) self.assertEqual(fromStoA.replace_index, 1) fromStoB = com.get_rule(RuleNewStoB) self.assertTrue(isclass(fromStoB)) self.assertTrue(issubclass(fromStoB, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromStoB.from_rule.rule, ([S], [B, C])) self.assertEqual(fromStoB.replace_index, 1) fromStoC = com.get_rule(RuleNewStoC) self.assertTrue(isclass(fromStoC)) self.assertTrue(issubclass(fromStoC, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromStoC.from_rule.rule, ([S], [B, C])) self.assertEqual(fromStoC.replace_index, 0) fromStoEPS = com.get_rule(RuleNewStoEPS) self.assertTrue(isclass(fromStoEPS)) self.assertTrue(issubclass(fromStoEPS, ContextFree.EpsilonRemovedRule)) self.assertEqual(fromStoEPS.from_rule.rule, ([S], [C])) self.assertEqual(fromStoEPS.replace_index, 0) class RuleOldAtoEps(Rule): rule = ([A], [EPS]) class RuleOldBtoEps(Rule): rule = ([B], [EPS]) class RuleOldCtoEps(Rule): rule = ([C], [EPS]) self.assertFalse(com.have_rule(RuleOldAtoEps)) self.assertFalse(com.have_rule(RuleOldBtoEps)) self.assertFalse(com.have_rule(RuleOldCtoEps))
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)