def FilterMatchingRules(rules, src_subtree, trg_subtree, rhs_child_pos): if trg_subtree is None: relevant_rules = [rule for rule in rules \ if TreeContains(src_subtree, rule.lhs) \ and MatchPos(GetPosAt(rule.rhs, ()), rhs_child_pos)] else: relevant_rules = [rule for rule in rules \ if TreeContains(trg_subtree, rule.rhs) and TreeContains(src_subtree, rule.lhs)] return relevant_rules
def test_LeafVarNoMatch(self): tree = tree_or_string(u'(NP (DT the) (NN house))') subtree = tree_or_string(u'(NP ?x0|DT ?x1|JJ)') self.assertFalse(TreeContains(tree, subtree))
def test_PreterminalJapaneseUntypedVar(self): tree = tree_or_string(u'(学生 hello)') subtree = tree_or_string(u'?x0|') self.assertTrue(TreeContains(tree, subtree))
def test_PreterminalJapaneseTypedVarImmutableTree(self): tree = ImmutableTree.fromstring(u'(学生 hello)') subtree = tree_or_string(u'?x0|学生') 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_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_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_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_PositiveMatchNonterminalVariable(self): tree = Tree.fromstring('(NP (DT the) (NN house))') subtree = tree_or_string('?x0|NP') self.assertTrue(TreeContains(tree, subtree))
def test_NegativeMatchPreterminalToNonterminal(self): tree = tree_or_string('(DT the)') subtree = tree_or_string('(NP (JJ ?x0|))') self.assertFalse(TreeContains(tree, subtree))
def test_PositiveMatchPreterminal(self): tree = tree_or_string('(DT the)') subtree = tree_or_string('(DT ?x0|)') self.assertTrue(TreeContains(tree, subtree))
def test_NegativeMatchTerminal(self): tree = tree_or_string('the') subtree = tree_or_string('da') self.assertFalse(TreeContains(tree, subtree))
def test_PositiveMatchTerminal(self): tree = tree_or_string('the') subtree = tree_or_string('the') self.assertTrue(TreeContains(tree, subtree))