示例#1
0
    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)
示例#2
0
    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)
示例#3
0
    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)
示例#4
0
    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)
示例#5
0
    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)
示例#6
0
    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)
示例#7
0
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)
示例#8
0
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)
示例#9
0
 def test_no_transduction(self):
     tree = Tree("(foo bar)")
     results = transduce.transduce(tree, self.rules, "q")
     self.assertListEqual([], results)
示例#10
0
 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))
示例#11
0
 def test_no_transduction(self):
     tree = Tree("(foo bar)")
     results = transduce.transduce(tree, self.rules, "q")
     self.assertListEqual([], results)
示例#12
0
 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))