コード例 #1
0
    def test_find_best_matches(self):
        # Match "the cat" onto "the dog chases the cat" (exact fit)
        matches = general_matching.find_best_matches(self.cat_dmrs, self.small_dmrs)

        self.assertEqual(len(matches), 1)
        self.assertCountEqual(matches[0].nodeid_pairs, [(2, 5), (1, 4)])
        self.assertCountEqual(matches[0].link_pairs,
                             [(Link(1, 2, 'RSTR', 'H'), Link(4, 5, 'RSTR', 'H'))])

        # Match "the dog chases the cat" onto "the cat chases the dog" (inexact fit)
        matches = general_matching.find_best_matches(self.small_dmrs, self.reverse_dmrs)
        self.assertEqual(len(matches), 1)
        self.assertCountEqual(matches[0].nodeid_pairs, [(5, 2), (4, 1), (3, 3), (2, 5), (1, 4)])
        self.assertCountEqual(matches[0].link_pairs,
                             [(Link(4, 5, 'RSTR', 'H'), Link(1, 2, 'RSTR', 'H')),
                              (Link(1, 2, 'RSTR', 'H'), Link(4, 5, 'RSTR', 'H'))])

        # No match found
        matches = general_matching.find_best_matches(examples_dmrs.the_mouse(), self.reverse_dmrs)
        self.assertIsNone(matches)

        # More than one match found.
        matches = general_matching.find_best_matches(self.cat_dmrs, self.large_dmrs)
        self.assertEqual(len(matches), 2)
        self.assertCountEqual(matches[0].nodeid_pairs, [(2, 5), (1, 4)])
        self.assertCountEqual(matches[0].link_pairs,
                             [(Link(1, 2, 'RSTR', 'H'), Link(4, 5, 'RSTR', 'H'))])
        self.assertCountEqual(matches[1].nodeid_pairs, [(2, 8), (1, 7)])
        self.assertCountEqual(matches[1].link_pairs,
                             [(Link(1, 2, 'RSTR', 'H'), Link(7, 8, 'RSTR', 'H'))])
コード例 #2
0
 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))
コード例 #3
0
    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)
コード例 #4
0
    def test_find_best_matches(self):
        # Match "the cat" onto "the dog chases the cat" (exact fit)
        matches = general_matching.find_best_matches(self.cat_dmrs,
                                                     self.small_dmrs)

        self.assertEqual(len(matches), 1)
        self.assertCountEqual(matches[0].nodeid_pairs, [(2, 5), (1, 4)])
        self.assertCountEqual(
            matches[0].link_pairs,
            [(Link(1, 2, 'RSTR', 'H'), Link(4, 5, 'RSTR', 'H'))])

        # Match "the dog chases the cat" onto "the cat chases the dog" (inexact fit)
        matches = general_matching.find_best_matches(self.small_dmrs,
                                                     self.reverse_dmrs)
        self.assertEqual(len(matches), 1)
        self.assertCountEqual(matches[0].nodeid_pairs, [(5, 2), (4, 1), (3, 3),
                                                        (2, 5), (1, 4)])
        self.assertCountEqual(
            matches[0].link_pairs,
            [(Link(4, 5, 'RSTR', 'H'), Link(1, 2, 'RSTR', 'H')),
             (Link(1, 2, 'RSTR', 'H'), Link(4, 5, 'RSTR', 'H'))])

        # No match found
        matches = general_matching.find_best_matches(examples_dmrs.the_mouse(),
                                                     self.reverse_dmrs)
        self.assertIsNone(matches)

        # More than one match found.
        matches = general_matching.find_best_matches(self.cat_dmrs,
                                                     self.large_dmrs)
        self.assertEqual(len(matches), 2)
        self.assertCountEqual(matches[0].nodeid_pairs, [(2, 5), (1, 4)])
        self.assertCountEqual(
            matches[0].link_pairs,
            [(Link(1, 2, 'RSTR', 'H'), Link(4, 5, 'RSTR', 'H'))])
        self.assertCountEqual(matches[1].nodeid_pairs, [(2, 8), (1, 7)])
        self.assertCountEqual(
            matches[1].link_pairs,
            [(Link(1, 2, 'RSTR', 'H'), Link(7, 8, 'RSTR', 'H'))])