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))
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))
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)
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))
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)
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))
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)
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
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))
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))
def setUp(self): self.tree = Tree.fromstring( '(S (NP (DT the) (NN house)) (VP (VBZ is) (JJ beautiful)))')
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))
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))
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))
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))
def test_PositiveMatchNonterminalVariable(self): tree = Tree.fromstring('(NP (DT the) (NN house))') subtree = tree_or_string('?x0|NP') self.assertTrue(TreeContains(tree, subtree))