コード例 #1
0
 def test_SingleTypedVarRHS(self):
   tree1 = Tree.fromstring('(NP (DT the) (NN house))')
   tree2 = Tree.fromstring('(NP (DT la) (PP casa))')
   rule = XTRule('q', tree_or_string('(NP ?x0|DT ?x1|NN)'),
                      tree_or_string('?x0|NP'),
                      {() : 'q'}, 1.0)
   rule_index = RuleIndexT2T([rule])
   relevant_rules = rule_index.GetRelevantRules(tree1, ((), 'q'), tree2, ())
   self.assertEqual(1, len(relevant_rules))
コード例 #2
0
 def test_SingleVarLHSLevel2(self):
   tree1 = Tree.fromstring('(NP (DT the) (NN house))')
   tree2 = Tree.fromstring('(NP (DT la) (PP casa))')
   rule = XTRule('q', tree_or_string('(DT ?x0|)'),
                      tree_or_string('?x0|'),
                      {() : 'q'}, 1.0)
   rule_index = RuleIndexT2T([rule])
   relevant_rules = rule_index.GetRelevantRules(tree1, ((0,), 'q'), tree2, (0, 0))
   self.assertEqual(1, len(relevant_rules))
コード例 #3
0
 def test_SingleRuleMatchAtRoot(self):
   tree1 = Tree.fromstring('(NP (DT the) (NN house))')
   tree2 = Tree.fromstring('(NP (DT la) (NN casa))')
   relevant_rules = self.rule_index.GetRelevantRules(tree1, ((), 'q'), tree2, ())
   self.assertEqual(1, len(relevant_rules))
   expected_rule = XTRule('q', tree_or_string('(NP ?x0|DT ?x1|NN)'),
                               tree_or_string('(NP ?x0|DT ?x1|NN)'),
                               {(0,) : 'q', (1,) : 'q'}, 1.0)
   self.assertListEqual([expected_rule], relevant_rules)
コード例 #4
0
 def test_UntypedBranchesMatch(self):
   tree1 = Tree.fromstring('(NP (DT the) (NN house))')
   tree2 = Tree.fromstring('(NP (DT la) (PP casa))')
   rule = XTRule('q', tree_or_string('(NP ?x0| ?x1|)'),
                      tree_or_string('(NP ?x0|DT ?x1|PP)'),
                      {(0,) : 'q', (1,) : 'q'}, 1.0)
   rule_index = RuleIndexT2T([rule])
   relevant_rules = rule_index.GetRelevantRules(tree1, ((), 'q'), tree2, ())
   self.assertEqual(1, len(relevant_rules))
コード例 #5
0
 def test_SingleRuleMatchAtLevel2JJ(self):
   tree1 = Tree.fromstring('(NP (DT the) (NN house))')
   tree2 = Tree.fromstring('(NP (DT la) (JJ casa))')
   relevant_rules = self.rule_index.GetRelevantRules(tree1, ((1,), 'q'), tree2, (1,))
   self.assertEqual(1, len(relevant_rules))
   expected_rule = XTRule('q', tree_or_string('(NN ?x0|)'),
                               tree_or_string('(JJ ?x0|)'),
                               {(0,) : 't'}, 0.2)
   self.assertListEqual([expected_rule], relevant_rules)
コード例 #6
0
 def test_RhsVarNoTypeMatchesJpEn(self):
   rule0 = XTRule('q', tree_or_string(u'(NP ?x0|NN)'),
                       tree_or_string(u'?x0|'),
                       {() : 'q'}, 1.0)
   rule1 = XTRule('q', tree_or_string(u'(NN ?x0|)'),
                       tree_or_string(u'(NN ?x0|)'),
                       {(0,) : 't'}, 1.0)
   rule2 = XTRule('t', tree_or_string(u'学生'),
                       tree_or_string(u'student'),
                       {}, 1.0)
   rules = [rule0, rule1, rule2]
   rule_index = RuleIndexT2T(rules)
   tree1 = Tree.fromstring(u'(NP (NN 学生))')
   tree2 = Tree.fromstring(u'(NN student)')
   relevant_rules = rule_index.GetRelevantRules(tree1, ((), 'q'), tree2, ())
   self.assertEqual(1, len(relevant_rules))
コード例 #7
0
 def test_SingleRuleMatchAtLeafJpEn(self):
   rule0 = XTRule('q', tree_or_string(u'(NP ?x0|NN)'),
                       tree_or_string(u'(NP (DT a) ?x0|NN)'),
                       {(1,) : 'q'}, 1.0)
   rule1 = XTRule('q', tree_or_string(u'(NN ?x0|)'),
                       tree_or_string(u'(NN ?x0|)'),
                       {(0,) : 't'}, 1.0)
   rule2 = XTRule('t', tree_or_string(u'学生'),
                       tree_or_string(u'student'),
                       {}, 1.0)
   rules = [rule0, rule1, rule2]
   rule_index = RuleIndexT2T(rules)
   tree1 = Tree.fromstring(u'(NP (NN 学生))')
   tree2 = Tree.fromstring(u'(NP (DT a) (NN student))')
   relevant_rules = rule_index.GetRelevantRules(tree1, ((0,0), 't'), tree2, (1,0))
   self.assertEqual(1, len(relevant_rules))
   expected_rule = XTRule('t', tree_or_string(u'学生'),
                               tree_or_string(u'student'),
                               {}, 1.0)
   self.assertListEqual([expected_rule], relevant_rules)
コード例 #8
0
 def MakeSimilar(self, src_tree_pattern, src_words, src_indices):
     similarities = []
     for i in src_indices:
         target = self.src_trg_cost[i][1].replace(' ',
                                                  self.trg_token_separator)
         prob = self.GetSimilarProb(src_words, i)
         trg_tree_pattern = \
           TreePattern(Tree.fromstring(u'(ID [] {0})'.format(target)), (), [])
         similarities.append(
             Similarity(prob, self.relation, src_tree_pattern,
                        trg_tree_pattern))
     # Remove duplicated elements (since this is not an exact match, they may occur).
     similarities = list(set(similarities))
     return similarities
コード例 #9
0
 def test_SingleRuleNoMatchAtLevel2JJ(self):
   tree1 = Tree.fromstring('(NP (DT the) (NN house))')
   tree2 = Tree.fromstring('(NP (DT la) (PP casa))')
   relevant_rules = self.rule_index.GetRelevantRules(tree1, ((1,), 'q'), tree2, (1,))
   self.assertEqual(0, len(relevant_rules))
コード例 #10
0
 def test_SingleWrongRuleNotMatchAtRoot(self):
   tree1 = Tree.fromstring('(NP (DT the) (JJ house))')
   tree2 = Tree.fromstring('(NP (DT la) (NN casa))')
   relevant_rules = self.rule_index.GetRelevantRules(tree1, ((), 'q'), tree2, ())
   self.assertEqual(0, len(relevant_rules))
コード例 #11
0
 def setUp(self):
     self.tree = Tree.fromstring(
         '(S (NP (DT the) (NN house)) (VP (VBZ is) (JJ beautiful)))')
コード例 #12
0
 def test_PositiveMatchQAVar(self):
     tree = Tree.fromstring('(NP (DT the) (NN house))')
     subtree = tree_or_string('(NP (DT the) (NN []))')
     self.assertTrue(TreeContains(tree, subtree))
コード例 #13
0
 def test_NegativeMatchNonterminal2LevelsFullRight(self):
     tree = Tree.fromstring('(NP (DT the) (NN house))')
     subtree = tree_or_string('(NP (DT the) (NN condominium))')
     self.assertFalse(TreeContains(tree, subtree))
コード例 #14
0
 def test_PositiveMatchNonterminal2LevelsFull(self):
     tree = Tree.fromstring('(NP (DT the) (NN house))')
     subtree = tree_or_string('(NP (DT the) (NN house))')
     self.assertTrue(TreeContains(tree, subtree))
コード例 #15
0
 def test_NegativeMatchNonterminal2Levels(self):
     tree = Tree.fromstring('(NP (DT the) (NN house))')
     subtree = tree_or_string('(NP (DT da) ?x1|NN)')
     self.assertFalse(TreeContains(tree, subtree))
コード例 #16
0
 def test_PositiveMatchNonterminalVariable(self):
     tree = Tree.fromstring('(NP (DT the) (NN house))')
     subtree = tree_or_string('?x0|NP')
     self.assertTrue(TreeContains(tree, subtree))