def test_one_result(self): tree = Tree("(A foo bar)") results = transduce.transduce(tree, self.rules, "q") trees = [ss.tree for ss in results] self.assertEqual(results[0].weight, 1.0) self.assertListEqual([Tree("(A (R bar foo) (S X))")], trees)
def test_nested(self): tree = Tree("(S Abe (VP kicked the-ball))") results = transduce.transduce(tree, self.rules, "q") trees = [ss.tree for ss in results] ss1 = transduce.SearchState(Tree("(S kicked Abe the-ball)"), {}, 1) ss2 = transduce.SearchState(Tree("(kicked kicked kicked kicked)"), {}, 0.5) # print(ss1) # print(ss2) self.assertIn(ss1, results) self.assertNotIn(ss2, results)
def test_figure_five(self): tree = Tree("(A (B D E) (C F G))") rules = loadrules.loadrules("figure5.yaml") results = transduce.transduce(tree, rules, "q") trees = [ss.tree for ss in results] ss1 = transduce.SearchState(Tree("(A (R (T V W) U) (S X))"), {}, 0.27) ss2 = transduce.SearchState(Tree("(A (R (T V W) U) (S X))"), {}, (1.0 * 0.4 * 1.0 * 0.1 * 0.5)) self.assertIn(ss1, results) self.assertIn(ss2, results)
def translate(tr, rules): intext = " ".join(tr.leaves()) print("** Translating:", intext) theresults = transduce(tr, rules, "q") output_triples = set() if not theresults: print(" FAILED TO TRANSLATE O NOES.") for result in theresults: treestr = result.tree.pprint(margin=1000) outtext = " ".join(result.tree.leaves()) w = result.weight output_triples.add((treestr,outtext,w)) inorder = sorted(list(output_triples), key=itemgetter(2)) for treestr,outtext,w in inorder: print(" text:", outtext) print(" tree:", treestr) print(" weight:", w)
def translate(tr, rules): intext = " ".join(tr.leaves()) print("** Translating:", intext) theresults = transduce(tr, rules, "q") output_triples = set() if not theresults: print(" FAILED TO TRANSLATE O NOES.") for result in theresults: treestr = result.tree.pprint(margin=1000) outtext = " ".join(result.tree.leaves()) w = result.weight output_triples.add((treestr, outtext, w)) inorder = sorted(list(output_triples), key=itemgetter(2)) for treestr, outtext, w in inorder: print(" text:", outtext) print(" tree:", treestr) print(" weight:", w)
def test_no_transduction(self): tree = Tree("(foo bar)") results = transduce.transduce(tree, self.rules, "q") self.assertListEqual([], results)
def test_onesymbol(self): results = transduce.transduce("F", self.rules, "q") trees = [ss.tree for ss in results] self.assertIn("V", trees) self.assertIn("W", trees) self.assertEqual(2, len(results))