Ejemplo n.º 1
0
 def test_epsilon_context_free_path_querying_matrices(self):
     grammar = CNF.from_file(epsilon_matrices_grammar_path)
     graph = Graph.from_file(graph_path)
     actual_res = context_free_path_querying_matrices(grammar, graph)
     self.assertEqual(actual_res['A'].nonzero().nvals, 3)
     self.assertEqual(actual_res['B'].nonzero().nvals, 2)
     self.assertEqual(actual_res['S'].nonzero().nvals, 5)
Ejemplo n.º 2
0
 def test_cfpq(self):
     grammar = CNF.from_file(grammar_path, is_reduced=True)
     graph = Graph.from_file(graph_path)
     actual_res = context_free_path_querying(grammar, graph)
     self.assertEqual(len(actual_res), len(expected_res))
     for tuple in expected_res:
         self.assertTrue(actual_res.__contains__(tuple))
Ejemplo n.º 3
0
 def test_aNbN_cyk_reduced(self):
     grammar = CNF.from_file(aNbN, is_reduced=True)
     self.assertTrue(cyk([], grammar))
     self.assertTrue(cyk([a, b], grammar))
     self.assertTrue(cyk([a, a, b, b], grammar))
     self.assertTrue(cyk([a, a, a, a, a, b, b, b, b, b], grammar))
     self.assertFalse(cyk([a, b, a, b], grammar))
     self.assertFalse(cyk([a, b, a], grammar))
     self.assertFalse(cyk([a, a, a, a, b], grammar))
     self.assertFalse(cyk([a, b, b, b, b], grammar))
     self.assertFalse(cyk([a, a, a, a, a], grammar))
     self.assertFalse(cyk([b, b, b, b, b], grammar))
Ejemplo n.º 4
0
 def test_aNbN_cyk(self):
     grammar = CNF.from_file(aNbN)
     self.assertTrue(cyk([], grammar))
     self.assertTrue(cyk([a, b], grammar))
     self.assertTrue(cyk([a, a, b, b], grammar))
     self.assertTrue(cyk([a, a, a, a, a, b, b, b, b, b], grammar))
     self.assertFalse(cyk([a, b, a, b], grammar))
     self.assertFalse(cyk([a, b, a], grammar))
     self.assertFalse(cyk([a, a, a, a, b], grammar))
     self.assertFalse(cyk([a, b, b, b, b], grammar))
     self.assertFalse(cyk([a, a, a, a, a], grammar))
     self.assertFalse(cyk([b, b, b, b, b], grammar))
Ejemplo n.º 5
0
 def test_task_4_3_1(self):
     grammar = CNF.from_file(task_1_grammar, Variable('E'))
     self.assertFalse(cyk([], grammar))
     self.assertTrue(cyk([open, n, plus, n, close, mult, n], grammar))
     self.assertTrue(cyk([n, plus, n, mult, n], grammar))
     self.assertTrue(cyk([n, plus, n, plus, n, plus, n], grammar))
     self.assertTrue(
         cyk([n, plus, open, n, mult, n, close, plus, n], grammar))
     self.assertFalse(cyk([n, plus, n, mult, n, close, plus, n], grammar))
     self.assertFalse(cyk([n, plus, open, n, mult, n, close, n], grammar))
     self.assertFalse(cyk([n, plus, open, n, mult, close, plus, n],
                          grammar))
     self.assertFalse(cyk([plus, open, n, mult, n, close, plus, n],
                          grammar))
Ejemplo n.º 6
0
def cyk(word: List[Terminal], cnf: CNF):
    n = len(word)
    if n == 0:
        return cnf.generate_epsilon()
    numbering_dict = dict()
    for index, symbol in enumerate(cnf.variables):
        numbering_dict[symbol] = index
    m = np.zeros((n, n, len(cnf.variables)), bool)
    for s in range(n):
        for production in cnf.productions:
            if len(production.body) == 1 and production.body[0] == word[s]:
                m[0, s, numbering_dict[production.head]] = True
    for l in range(1, n):
        for s in range(n - l):
            for p in range(l):
                for production in cnf.productions:
                    if len(production.body) == 2 and m[
                            p, s, numbering_dict[production.body[0]]] and m[
                                l - p - 1, s + p + 1,
                                numbering_dict[production.body[1]]]:
                        m[l, s, numbering_dict[production.head]] = True
    return m[n - 1, 0, numbering_dict[cnf.start_symbol]]