def setUp(self):
     self.the_cat = examples_dmrs.the_cat().convert_to(
         abstractSortDictDmrs(node_key=span_pred_key))
     # Checks if the matching code converts to SortDictDmrs with span_pred_key
     self.the_cat_chases_the_dog = examples_dmrs.the_cat_chases_the_dog(
     ).convert_to(abstractSortDictDmrs(node_key=span_pred_key))
     self.the_dog_chases_the_cat = examples_dmrs.the_dog_chases_the_cat(
     ).convert_to(abstractSortDictDmrs(node_key=span_pred_key))
     self.the_mouse = examples_dmrs.the_mouse() \
         .convert_to(abstractSortDictDmrs(node_key=span_pred_key))
     self.dog_cat = examples_dmrs.dog_cat() \
         .convert_to(abstractSortDictDmrs(node_key=span_pred_key))
    def test_get_matching_nodeids(self):
        # Match "the cat" onto "the dog chases the cat" (exact fit)
        matches1 = aligned_matching.get_matching_nodeids(self.the_cat, self.the_dog_chases_the_cat)
        self.assertEqual(len(matches1), 2)
        self.assertCountEqual(matches1[0], [(2, 5), (1, 1)])
        self.assertCountEqual(matches1[1], [(2, 5), (1, 4)])

        # all_surface = True
        all_matches1 = aligned_matching.get_matching_nodeids(self.the_cat,
                                                             self.the_dog_chases_the_cat,
                                                             all_surface=True)
        self.assertListEqual(matches1[1], all_matches1[1])
        # Extra surface nodes
        self.assertCountEqual(all_matches1[0], [(2, 5), (1, 1), (None, 2), (None, 3), (None, 4)])

        # Match "the dog chases the cat" onto "the cat chases the dog" (inexact fit)
        matches2 = aligned_matching.get_matching_nodeids(self.the_dog_chases_the_cat,
                                                         self.the_cat_chases_the_dog)
        self.assertEqual(len(matches2), 1)
        self.assertCountEqual(matches2[0], [(4, 4), (3, 3), (1, 1)])
        all_matches2 = aligned_matching.get_matching_nodeids(self.the_dog_chases_the_cat,
                                                             self.the_cat_chases_the_dog,
                                                             all_surface=True)
        self.assertEqual(len(all_matches2), 1)
        self.assertCountEqual(all_matches2[0], [(4, 4), (3, 3), (1, 1), (None, 2)])

        # No match found
        the_mouse = examples_dmrs.the_mouse() \
            .convert_to(abstractSortDictDmrs(node_key=span_pred_key))
        dog_cat = examples_dmrs.dog_cat() \
            .convert_to(abstractSortDictDmrs(node_key=span_pred_key))
        matches = aligned_matching.get_matching_nodeids(the_mouse, dog_cat)
        self.assertListEqual(matches, [])

        # Should be the same as 'the cat'.
        mixed_cat = ListDmrs(surface='the cat')
        mixed_cat.add_node(Node(nodeid=2, pred=RealPred('cat', 'n', '1'), cfrom=4, cto=7,
                                sortinfo=InstanceSortinfo(pers='3', num='sg', ind='+')))
        mixed_cat.add_node(Node(nodeid=1, pred=RealPred('the', 'q'), cfrom=0, cto=3))
        mixed_cat.add_link(Link(start=1, end=2, rargname='RSTR', post='H'))
        mixed = aligned_matching.get_matching_nodeids(mixed_cat, self.the_dog_chases_the_cat)
        self.assertListEqual(mixed, matches1)
from pydmrs.matching.exact_matching import dmrs_exact_matching
import examples.examples_dmrs as examples


if __name__ == '__main__':

    # "the" - "the dog chases the cat"
    assert len(list(dmrs_exact_matching(examples.the(), examples.the_dog_chases_the_cat()))) == 2

    # "the cat" - "the dog chases the cat"
    assert len(list(dmrs_exact_matching(examples.the_cat(), examples.the_dog_chases_the_cat()))) == 1

    # "dog cat" - "the dog chases the cat"
    assert len(list(dmrs_exact_matching(examples.dog_cat(), examples.the_dog_chases_the_cat()))) == 1

    # "the dog chases the cat" - "the dog chases the cat"
    assert len(list(dmrs_exact_matching(examples.the_dog_chases_the_cat(), examples.the_dog_chases_the_cat()))) == 1

    # "the cat chases the dog" - "the dog chases the cat"
    assert not len(list(dmrs_exact_matching(examples.the_cat_chases_the_dog(), examples.the_dog_chases_the_cat())))

    # "the dog chases the cat" - "the dog chases the cat and the mouse"
    assert not len(list(dmrs_exact_matching(examples.the_dog_chases_the_cat(), examples.the_dog_chases_the_cat_and_the_mouse())))

    # "the dog chases the cat" - "the dog chases the cat and the cat chases the mouse"
    assert len(list(dmrs_exact_matching(examples.the_dog_chases_the_cat(), examples.the_dog_chases_the_cat_and_the_cat_chases_the_mouse()))) == 1

    # "the cat" - "the dog chases the cat and the cat chases the mouse"
    assert len(list(dmrs_exact_matching(examples.the_cat(), examples.the_dog_chases_the_cat_and_the_cat_chases_the_mouse()))) == 2

    # "dog cat" - "the dog chases the cat and the cat chases the mouse"
Exemple #4
0
    # "the" - "the dog chases the cat"
    assert len(
        list(
            dmrs_exact_matching(examples.the(),
                                examples.the_dog_chases_the_cat()))) == 2

    # "the cat" - "the dog chases the cat"
    assert len(
        list(
            dmrs_exact_matching(examples.the_cat(),
                                examples.the_dog_chases_the_cat()))) == 1

    # "dog cat" - "the dog chases the cat"
    assert len(
        list(
            dmrs_exact_matching(examples.dog_cat(),
                                examples.the_dog_chases_the_cat()))) == 1

    # "the dog chases the cat" - "the dog chases the cat"
    assert len(
        list(
            dmrs_exact_matching(examples.the_dog_chases_the_cat(),
                                examples.the_dog_chases_the_cat()))) == 1

    # "the cat chases the dog" - "the dog chases the cat"
    assert not len(
        list(
            dmrs_exact_matching(examples.the_cat_chases_the_dog(),
                                examples.the_dog_chases_the_cat())))

    # "the dog chases the cat" - "the dog chases the cat and the mouse"