def run_cyk_from_file(gram_file_name, w_file_name): gram = custom_CFG.read_cfg(open(gram_file_name, 'r').read()) gram = gram.to_normal_form() res = [] with open(w_file_name, 'r') as w_f: for w in w_f.readlines(): res.append(CYK(gram, w.strip())) return res
def run_script(raw_script, gram=None): if gram is None: with open(GRAMMAR_PATH, 'r') as f: gram = custom_CFG.read_cfg( f.read(), start_symbol=Variable("SCRIPT"), contains_regexes=True, track_variables=True, ) gram = gram.to_normal_form() script = preprocess_script(raw_script) return CYK(gram, script)
def run_hellings_from_file(edges_file_name, gram_file_name): gram = custom_CFG.read_cfg(open(gram_file_name, 'r').read()) gram = gram.to_normal_form() edges = read_edges(edges_file_name) raw_res = hellings(edges, gram) res = set() for (nterm, vs, ve) in raw_res: if nterm == gram.start_symbol: res.add((vs, ve)) return res
def run_cfpq_from_file(edges_file_name, gram_file_name): gram = custom_CFG.read_cfg(open(gram_file_name, 'r').read()) print(gram.to_text()) edges = utils.read_edges(edges_file_name) raw_res = cfpq(edges, gram) print(raw_res.to_string()) res = set() for (vs, ve, _) in zip(*raw_res.to_lists()): res.add((vs, ve)) print(res) return res
def test_all(self): with open(self.GRAMMAR_PATH, 'r') as f: gram = custom_CFG.read_cfg( f.read(), start_symbol=Variable("SCRIPT"), contains_regexes=True, track_variables=True, ) gram = gram.to_normal_form() script = """ connect "ABC" """ assert (run_script(script, gram=gram)) script = """ select count edges from "g" intersect grammar """ assert (run_script(script, gram=gram)) script = """ select count edges from setStartAndFinal {3, 2, 6} 1:6 "g" """ assert (run_script(script, gram=gram)) script = """ select count edges from [term(a)?.term(b)*.(term(c)|term(d))+] """ assert (run_script(script, gram=gram)) script = """ select count edges from "g" intersect [term(a).term(b)*.(term(c)|term(d))+] """ assert (run_script(script, gram=gram)) script = """ select count edges from setStartAndFinal _ _ "g" """ assert (run_script(script, gram=gram)) script = """ select count edges from setStartAndFinal _ _ ("g" intersect grammar) """ assert (run_script(script, gram=gram)) script = """ select filter (v, e, u) -> (e hasLbl abc) : edges from "g" """ assert (run_script(script, gram=gram)) script = """ select filter (v, e, u) -> (e hasLbl abc & isStart v | !(isFinal u)) : edges from "g" """ assert (run_script(script, gram=gram)) script = """ select count edges from "g1" intersect ("g2" intersect "g3") """ assert (run_script(script, gram=gram)) script = """ s : nonterm(S).term(a).nonterm(S).term(b) s : term(eps) """ assert (run_script(script, gram=gram)) script = """ select edges from [term(a)?.term(b)*.(term(c)|term(d))+] """ assert (run_script(script, gram=gram))