def test_cfpq_matrix_triple(): cfg = GrammarUtils.from_file("src/data/test_cfpq_grammar_triple.txt") grammar = GrammarUtils.to_cnf(cfg) graph = Graph.from_file("src/data/test_cfpq_graph_triple.txt") result = GrammarUtils.cfpq_matrix(graph, grammar) assert set(result) == set(expected_triple)
def test_cyk_parentheses(): cfg = GrammarUtils.from_file("src/data/test_cyk_grammar_parentheses.txt") grammar = GrammarUtils.to_cnf(cfg) assert GrammarUtils.cyk(grammar, 'aabbab') assert GrammarUtils.cyk(grammar, 'ab') assert GrammarUtils.cyk(grammar, 'abababababab') assert not GrammarUtils.cyk(grammar, 'aabba') assert not GrammarUtils.cyk(grammar, 'abbab')
def test_cyk_palindrome(): cfg = GrammarUtils.from_file("src/data/test_cyk_grammar_palindrome.txt") grammar = GrammarUtils.to_cnf(cfg) assert GrammarUtils.cyk(grammar, 'aabbaa') assert GrammarUtils.cyk(grammar, 'aaaa') assert not GrammarUtils.cyk(grammar, 'aba') assert not GrammarUtils.cyk(grammar, 'a')
def test_cyk_b(): cfg = GrammarUtils.from_file("src/data/test_cyk_grammar_b.txt") grammar = GrammarUtils.to_cnf(cfg) # number of 'b's must be even assert GrammarUtils.cyk(grammar, 'ababbb') assert GrammarUtils.cyk(grammar, 'bbbb') assert not GrammarUtils.cyk(grammar, 'ab') assert not GrammarUtils.cyk(grammar, 'abbab')
graphs = [ f for f in listdir('/mnt/x/DataForFLCourse/graphs') if isfile(join('/mnt/x/DataForFLCourse/graphs', f)) ] grammars = [ f for f in listdir('/mnt/x/DataForFLCourse/grammars') if isfile(join('/mnt/x/DataForFLCourse/grammars', f)) ] for grammar_name in grammars: for g in graphs: (graph_name, separator, extension) = g.partition('.') print(grammar_name) print(graph_name) benchmark_data = [[] for i in range(1, 5)] cfg = GrammarUtils.from_file( f'/mnt/x/DataForFLCourse/grammars/{grammar_name}') cnf = GrammarUtils.to_cnf(cfg) graph = Graph.from_file(f'/mnt/x/DataForFLCourse/graphs/{g}') for tests_count in range(5): time_start = perf_counter() GrammarUtils.cfpq_matrix(graph, cnf) time_end = perf_counter() benchmark_data[0].append(time_end - time_start) print(time_end - time_start) time_start = perf_counter() GrammarUtils.cfpq_tensor(graph, cfg) time_end = perf_counter() benchmark_data[1].append(time_end - time_start) print(time_end - time_start)
def test_cfpq_tensor_empty(): cfg = GrammarUtils.from_file("src/data/test_cfpq_grammar_brackets.txt") result = GrammarUtils.cfpq_tensor(Graph(), cfg) assert set() == set(get_reachable(result))
def test_cfpq_tensor_triple(): cfg = GrammarUtils.from_file("src/data/test_cfpq_grammar_triple.txt") graph = Graph.from_file("src/data/test_cfpq_graph_triple.txt") result = GrammarUtils.cfpq_tensor(graph, cfg) assert set(result) == set(expected_triple)
def test_cfpq_hellings_empty(): cfg = GrammarUtils.from_file("src/data/test_cfpq_grammar_brackets.txt") result = GrammarUtils.cfpq_hellings(cfg, Graph()) assert set() == set(get_reachable(result))
def test_cfpq_hellings_brackets(): cfg = GrammarUtils.from_file("src/data/test_cfpq_grammar_brackets.txt") graph = Graph.from_file("src/data/test_cfpq_graph.txt") result = GrammarUtils.cfpq_hellings(cfg, graph) assert set(result) == set(expected_brackets)
def test_grammar(): cfg = GrammarUtils.from_file( 'src/query_language/query_language_grammar.txt') grammar = GrammarUtils.to_cnf(cfg) for (script, expected) in test_input: assert check_script_with_cyk(grammar, script) == expected