예제 #1
0
 def test_NonterminalPreterminalIdentity(self):
     """
 Using the Identity back-off, the state of the parent rule
 is applied to the paths of the variables in the RHS.
 However, the states of the paths of the variables in the RHS
 should be more specific: "copy" and "hypernym".
 """
     intree = tree_or_string('(NP (DT the) (NN dog))')
     rule0 = XTRule('q', tree_or_string('(NP ?x0|DT ?x1|NN)'),
                    tree_or_string('(NPP ?x0|DTT ?x1|NNN)'), {
                        (0, ): 'q',
                        (1, ): 'q'
                    }, 1.0)
     rule1 = XTRule('q', tree_or_string('(DT ?x0|)'),
                    tree_or_string('(DTT ?x0|)'), {(0, ): 'copy'}, 1.0)
     rule2 = XTRule('copy', tree_or_string('the'), tree_or_string('the'),
                    {}, 1.0)
     rule3 = XTRule('hypernym', tree_or_string('dog'),
                    tree_or_string('canine'), {}, 1.0)
     rules = [rule0, rule1, rule2, rule3]
     rule_backoffs = [Identity()]
     initial_state = 'q'
     transducer = xT(initial_state, rules, rule_backoffs)
     wrtg = transducer.Transduce(intree, None)
     outtrees = [tree for tree, _ in wrtg.GenerateNBestTrees()]
     expected_outtree = immutable(
         tree_or_string('(NPP (DTT the) (NN canine))'))
     self.assertIn(expected_outtree, outtrees)
예제 #2
0
 def test_UnseenTerminalDifferent(self):
     intree = tree_or_string('dog')
     rule0 = XTRule('t', tree_or_string('italian'), tree_or_string('house'),
                    {}, 1.0)
     rules = [rule0]
     initial_state = 't'
     transducer = xT(initial_state, rules)
     wrtg = transducer.Transduce(intree)
     self.assertEqual(wrtg.P, [])
예제 #3
0
 def test_PreterminalUnseenTerminalDifferent(self):
     intree = tree_or_string('(NN dog)')
     rule0 = XTRule('q', tree_or_string('(NN ?x0|)'),
                    tree_or_string('(JJ ?x0|)'), {(0, ): 't'}, 1.0)
     rule1 = XTRule('t', tree_or_string('italian'), tree_or_string('house'),
                    {}, 1.0)
     rules = [rule0, rule1]
     initial_state = 'q'
     transducer = xT(initial_state, rules)
     wrtg = transducer.Transduce(intree, None)
     self.assertEqual(wrtg.P, [])
예제 #4
0
 def test_UnseenTerminalEqual(self):
     intree = tree_or_string('dog')
     rule0 = XTRule('copy', tree_or_string('italian'),
                    tree_or_string('italian'), {}, 1.0)
     rules = [rule0]
     rule_backoffs = [LexicalSimilarity()]
     initial_state = 'copy'
     transducer = xT(initial_state, rules, rule_backoffs)
     wrtg = transducer.Transduce(intree)
     outtrees = [tree for tree, _ in wrtg.GenerateNBestTrees()]
     expected_outtree = tree_or_string('dog')
     self.assertIn(expected_outtree, outtrees)
예제 #5
0
 def test_PreterminalIdentity(self):
     intree = tree_or_string('(NN dog)')
     rule1 = XTRule('q', tree_or_string('dog'), tree_or_string('perro'), {},
                    1.0)
     rules = [rule1]
     rule_backoffs = [Identity(), LexicalSimilarity()]
     initial_state = 'q'
     transducer = xT(initial_state, rules, rule_backoffs)
     wrtg = transducer.Transduce(intree, None)
     outtrees = [tree for tree, _ in wrtg.GenerateNBestTrees()]
     expected_outtree = immutable(tree_or_string('(NN perro)'))
     self.assertIn(expected_outtree, outtrees)
예제 #6
0
 def test_PreterminalUnseenTerminalEqual(self):
     intree = tree_or_string('(NN dog)')
     rule0 = XTRule('q', tree_or_string('(NN ?x0|)'),
                    tree_or_string('(JJ ?x0|)'), {(0, ): 'copy'}, 1.0)
     rule1 = XTRule('copy', tree_or_string('italian'),
                    tree_or_string('italian'), {}, 1.0)
     rules = [rule0, rule1]
     rule_backoffs = [LexicalSimilarity()]
     initial_state = 'q'
     transducer = xT(initial_state, rules, rule_backoffs)
     wrtg = transducer.Transduce(intree, None)
     outtrees = [tree for tree, _ in wrtg.GenerateNBestTrees()]
     expected_outtree = immutable(tree_or_string('(JJ dog)'))
     self.assertIn(expected_outtree, outtrees)
예제 #7
0
 def test_NonConsumingLHSAvoidsInfiniteRTG(self):
     intree = tree_or_string('(NN dog)')
     rule0 = XTRule('q', tree_or_string('?x0|NN'),
                    tree_or_string('(NN ?x0|)'), {(0, ): 'q'}, 0.9)
     rule1 = XTRule('q', tree_or_string('?x0|NN'),
                    tree_or_string('(JJ ?x0|)'), {(0, ): 't'}, 0.9)
     rule2 = XTRule('t', tree_or_string('(NN dog)'),
                    tree_or_string('canine'), {}, 1.0)
     rules = [rule0, rule1, rule2]
     initial_state = 'q'
     transducer = xT(initial_state, rules)
     wrtg = transducer.Transduce(intree, None)
     outtrees = [tree for tree, _ in wrtg.GenerateNBestTrees()]
     expected_outtree = immutable(tree_or_string('(JJ canine)'))
     self.assertIn(expected_outtree, outtrees)
예제 #8
0
    def test_OnlySourceDifferentVarTypes(self):
        rule0 = XTRule('q', tree_or_string('(A ?x0|AA)'),
                       tree_or_string('(a ?x0|aa)'), {(0, ): 't'}, 1.0)
        rule1 = XTRule('t', tree_or_string('(AA AAA)'),
                       tree_or_string('(aa aaa)'), {}, 1.0)
        rule2 = XTRule('t', tree_or_string('(AA AAA)'),
                       tree_or_string('(bb bbb)'), {}, 1.0)
        rules = [rule0, rule1, rule2]
        self.transducer = xT('q', rules)
        input_tree = immutable(tree_or_string('(A (AA AAA))'))
        output_tree = None
        productions, _ = self.transducer.Produce(input_tree, output_tree, 'q',
                                                 (), ())

        self.assertEqual(2, len(productions))
        self.assertIn(rule0, [p.rhs.rule for p in productions])
        self.assertIn(rule1, [p.rhs.rule for p in productions])
        self.assertNotIn(rule2, [p.rhs.rule for p in productions])
예제 #9
0
 def test_PreterminalIdentityUnseenTerminalSimilar(self):
     """
 Using the Identity back-off, the state of the parent rule
 is applied to the path of the variable in the RHS.
 However, the states of the path of the variable in the RHS
 should be more specific: "hypernym".
 """
     intree = tree_or_string('(NN dog)')
     rule1 = XTRule('hypernym', tree_or_string('italian'),
                    tree_or_string('european'), {}, 1.0)
     rules = [rule1]
     rule_backoffs = [Identity(), LexicalSimilarity()]
     initial_state = 'q'
     transducer = xT(initial_state, rules, rule_backoffs)
     wrtg = transducer.Transduce(intree, None)
     outtrees = [tree for tree, _ in wrtg.GenerateNBestTrees()]
     expected_outtree = immutable(tree_or_string('(NN canine)'))
     self.assertIn(expected_outtree, outtrees)
예제 #10
0
 def test_NonterminalIdentityNoBackoff(self):
     intree = tree_or_string('(NP (DT the) (NN dog))')
     rule0 = XTRule('q', tree_or_string('(DT ?x0|)'),
                    tree_or_string('(DTT ?x0|)'), {(0, ): 'copy'}, 1.0)
     rule1 = XTRule('copy', tree_or_string('the'), tree_or_string('the'),
                    {}, 1.0)
     rule2 = XTRule('q', tree_or_string('(NN ?x0|)'),
                    tree_or_string('(NNN ?x0|)'), {(0, ): 'hypernym'}, 1.0)
     rule3 = XTRule('hypernym', tree_or_string('dog'),
                    tree_or_string('canine'), {}, 1.0)
     rules = [rule0, rule1, rule2, rule3]
     rule_backoffs = []
     initial_state = 'q'
     transducer = xT(initial_state, rules, rule_backoffs)
     wrtg = transducer.Transduce(intree, None)
     outtrees = [tree for tree, _ in wrtg.GenerateNBestTrees()]
     expected_outtree = immutable(
         tree_or_string('(NP (DTT the) (NNN canine))'))
     self.assertNotIn(expected_outtree, outtrees)
예제 #11
0
 def test_NonterminalUnseenTerminalEqualAndSimilar(self):
     intree = tree_or_string('(NP (DT the) (NN dog))')
     rule0 = XTRule('q', tree_or_string('(NP (DT ?x0|) (NN ?x1|))'),
                    tree_or_string('(NP (DT ?x0|) (NN ?x1|))'), {
                        (0, 0): 'copy',
                        (1, 0): 'hypernym'
                    }, 1.0)
     rule1 = XTRule('copy', tree_or_string('the'), tree_or_string('the'),
                    {}, 1.0)
     rule2 = XTRule('hypernym', tree_or_string('italian'),
                    tree_or_string('european'), {}, 1.0)
     rules = [rule0, rule1, rule2]
     rule_backoffs = [LexicalSimilarity()]
     initial_state = 'q'
     transducer = xT(initial_state, rules, rule_backoffs)
     wrtg = transducer.Transduce(intree, None)
     outtrees = [tree for tree, _ in wrtg.GenerateNBestTrees()]
     expected_outtree = immutable(
         tree_or_string('(NP (DT the) (NN canine))'))
     self.assertIn(expected_outtree, outtrees)
예제 #12
0
 def setUp(self):
     rule1 = XTRule('q', tree_or_string('(A ?x0| ?x1|)'),
                    tree_or_string('(A (R ?x1| ?x0|) (S X))'), {
                        (0, 0): 'q',
                        (0, 1): 'q'
                    }, 1.0)
     rule2 = XTRule('q', tree_or_string('(B ?x0| ?x1|)'),
                    tree_or_string('U'), {}, 1.0)
     rule3 = XTRule('q', tree_or_string('(C ?x0| ?x1|)'),
                    tree_or_string('(T ?x0| ?x1|)'), {
                        (0, ): 'q',
                        (1, ): 'q'
                    }, 0.6)
     rule4 = XTRule('q', tree_or_string('(C ?x0| ?x1|)'),
                    tree_or_string('(T ?x1| ?x0|)'), {
                        (0, ): 'q',
                        (1, ): 'q'
                    }, 0.4)
     rule5 = XTRule('q', tree_or_string('F'), tree_or_string('V'), {}, 0.9)
     rule6 = XTRule('q', tree_or_string('F'), tree_or_string('W'), {}, 0.1)
     rule7 = XTRule('q', tree_or_string('G'), tree_or_string('V'), {}, 0.5)
     rule8 = XTRule('q', tree_or_string('G'), tree_or_string('W'), {}, 0.5)
     self.rules = [rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8]
     self.transducer = xT('q', self.rules)