Пример #1
0
    def test_arc_sort(self):
        s = r'''
        0 1 2 1
        0 4 0 2
        0 2 0 3
        1 2 1 4
        1 3 0 5
        2 1 0 6
        4
        '''

        fsa = k2host.str_to_fsa(s)
        sorter = k2host.ArcSorter(fsa)
        array_size = k2host.IntArray2Size()
        sorter.get_sizes(array_size)
        fsa_out = k2host.Fsa.create_fsa_with_size(array_size)
        arc_map = k2host.IntArray1.create_array_with_size(array_size.size2)
        sorter.get_output(fsa_out, arc_map)
        expected_arc_indexes = torch.IntTensor([0, 3, 5, 6, 6, 6])
        expected_arcs = torch.IntTensor([[0, 2, 0, float_to_int(3)],
                                         [0, 4, 0, float_to_int(2)],
                                         [0, 1, 2, float_to_int(1)],
                                         [1, 3, 0, float_to_int(5)],
                                         [1, 2, 1, float_to_int(4)],
                                         [2, 1, 0, float_to_int(6)]])
        expected_arc_map = torch.IntTensor([2, 1, 0, 4, 3, 5])
        self.assertTrue(torch.equal(fsa_out.indexes, expected_arc_indexes))
        self.assertTrue(torch.equal(fsa_out.data, expected_arcs))
        self.assertTrue(torch.equal(arc_map.data, expected_arc_map))
Пример #2
0
 def test_bad_cases1(self):
     s = r'''
     0 2 0 0
     2
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertFalse(k2host.is_connected(fsa))
Пример #3
0
 def test_good_case_2(self):
     s_a = r'''
     0 1 1 0
     1 2 3 0
     2 3 4 0
     3 4 -1 0
     4
     '''
     fsa = k2host.str_to_fsa(s_a)
     rand_path = k2host.RandPath(fsa, False)
     array_size = k2host.IntArray2Size()
     rand_path.get_sizes(array_size)
     path = k2host.Fsa.create_fsa_with_size(array_size)
     arc_map = k2host.IntArray1.create_array_with_size(array_size.size2)
     status = rand_path.get_output(path, arc_map)
     self.assertTrue(status)
     self.assertFalse(k2host.is_empty(path))
     self.assertFalse(arc_map.empty())
     expected_arc_indexes = torch.IntTensor([0, 1, 2, 3, 4, 4])
     expected_arcs = torch.IntTensor([[0, 1, 1, 0], [1, 2, 3, 0],
                                      [2, 3, 4, 0], [3, 4, -1, 0]])
     expected_arc_map = torch.IntTensor([0, 1, 2, 3])
     self.assertTrue(torch.equal(path.indexes, expected_arc_indexes))
     self.assertTrue(torch.equal(path.data, expected_arcs))
     self.assertTrue(torch.equal(arc_map.data, expected_arc_map))
Пример #4
0
 def test_bad_case1(self):
     s = r'''
     0 1 2 0
     1
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertFalse(k2host.is_empty(fsa))
Пример #5
0
    def test_fsa(self):
        s = r'''
        0 1 1 1.25
        0 2 2 1.5
        1 3 3 1.75
        2 3 3 2.25
        3 4 -1 2.5
        4
        '''

        fsa = k2host.str_to_fsa(s)
        self.assertEqual(fsa.num_states(), 5)
        self.assertEqual(fsa.final_state(), 4)
        self.assertFalse(fsa.empty())
        self.assertIsInstance(fsa, k2host.Fsa)
        # test get_data
        self.assertEqual(fsa.get_data(0).src_state, 0)
        self.assertEqual(fsa.get_data(0).dest_state, 1)
        self.assertEqual(fsa.get_data(0).label, 1)
        self.assertEqual(fsa.get_data(0).weight, 1.25)
        self.assertEqual(fsa.get_data(1).weight, 1.5)
        self.assertEqual(fsa.get_data(2).weight, 1.75)
        self.assertEqual(fsa.get_data(3).weight, 2.25)
        self.assertEqual(fsa.get_data(4).weight, 2.5)
        # fsa.data and the corresponding k2host::Fsa object are sharing memory
        fsa.data[0] = torch.IntTensor([5, 1, 6, 1])
        self.assertEqual(fsa.get_data(0).src_state, 5)
Пример #6
0
 def test_case_4(self):
     # connected fsa
     s = r'''
     0 4 40 0
     0 2 20 0
     1 6 -1 0
     2 3 30 0
     3 6 -1 0
     3 1 10 0
     4 5 50 0
     5 2 8 0
     6
     '''
     fsa = k2host.str_to_fsa(s)
     sorter = k2host.TopSorter(fsa)
     array_size = k2host.IntArray2Size()
     sorter.get_sizes(array_size)
     fsa_out = k2host.Fsa.create_fsa_with_size(array_size)
     arc_map = k2host.IntArray1.create_array_with_size(array_size.size2)
     status = sorter.get_output(fsa_out, arc_map)
     self.assertTrue(status)
     expected_arc_indexes = torch.IntTensor([0, 2, 3, 4, 5, 7, 8, 8])
     expected_arcs = torch.IntTensor([[0, 1, 40, 0], [0, 3, 20, 0],
                                      [1, 2, 50, 0], [2, 3, 8, 0],
                                      [3, 4, 30, 0], [4, 6, -1, 0],
                                      [4, 5, 10, 0], [5, 6, -1, 0]])
     expected_arc_map = torch.IntTensor([0, 1, 6, 7, 3, 4, 5, 2])
     self.assertTrue(torch.equal(fsa_out.indexes, expected_arc_indexes))
     self.assertTrue(torch.equal(fsa_out.data, expected_arcs))
     self.assertTrue(torch.equal(arc_map.data, expected_arc_map))
Пример #7
0
 def test_case_2(self):
     # a cyclic input fsa
     # after trimming, the cycle is removed;
     # so the output fsa should be topsorted.
     s = r'''
     0 1 1 0
     0 2 2 1
     1 3 3 -2
     1 6 6 -3
     2 4 2 4
     2 6 3 5
     2 6 -1 6
     5 0 1 7
     5 7 -1 8
     7
     '''
     fsa = k2host.str_to_fsa(s)
     connection = k2host.Connection(fsa)
     array_size = k2host.IntArray2Size()
     connection.get_sizes(array_size)
     fsa_out = k2host.Fsa.create_fsa_with_size(array_size)
     arc_map = k2host.IntArray1.create_array_with_size(array_size.size2)
     status = connection.get_output(fsa_out, arc_map)
     self.assertTrue(status)
     self.assertTrue(k2host.is_empty(fsa_out))
     self.assertTrue(arc_map.empty())
Пример #8
0
 def test_case_1(self):
     # a non-connected, non-topsorted, acyclic input fsa;
     # the output fsa is topsorted.
     s = r'''
     0 1 1 0
     0 2 2 0
     1 3 3 0
     1 6 -1 0
     2 4 2 0
     2 6 -1 0
     2 1 1 0
     5 0 1 0
     6
     '''
     fsa = k2host.str_to_fsa(s)
     connection = k2host.Connection(fsa)
     array_size = k2host.IntArray2Size()
     connection.get_sizes(array_size)
     fsa_out = k2host.Fsa.create_fsa_with_size(array_size)
     arc_map = k2host.IntArray1.create_array_with_size(array_size.size2)
     status = connection.get_output(fsa_out, arc_map)
     self.assertTrue(status)
     expected_arc_indexes = torch.IntTensor([0, 2, 4, 5, 5])
     expected_arcs = torch.IntTensor([[0, 2, 1, 0], [0, 1, 2, 0],
                                      [1, 3, -1, 0], [1, 2, 1, 0],
                                      [2, 3, -1, 0]])
     expected_arc_map = torch.IntTensor([0, 1, 5, 6, 3])
     self.assertTrue(torch.equal(fsa_out.indexes, expected_arc_indexes))
     self.assertTrue(torch.equal(fsa_out.data, expected_arcs))
     self.assertTrue(torch.equal(arc_map.data, expected_arc_map))
Пример #9
0
 def test_case_4(self):
     # a cyclic input fsa
     # after trimming, the cycle remains (it is not a self-loop);
     # so the output fsa is NOT topsorted.
     s = r'''
     0 3 3 1
     0 2 2 2
     1 0 1 3
     2 6 -1 4
     3 5 5 5
     3 2 2 6
     3 5 5 7
     4 4 4 8
     5 3 3 9
     5 4 4 10
     6
     '''
     fsa = k2host.str_to_fsa(s)
     connection = k2host.Connection(fsa)
     array_size = k2host.IntArray2Size()
     connection.get_sizes(array_size)
     fsa_out = k2host.Fsa.create_fsa_with_size(array_size)
     status = connection.get_output(fsa_out)
     self.assertFalse(status)
     self.assertFalse(k2host.is_top_sorted(fsa_out))
Пример #10
0
 def test_good_case_1(self):
     # both fsas will be empty after trimming
     s_a = r'''
     0 1 1 0
     0 2 2 0
     1 2 3 0
     3
     '''
     fsa_a = k2host.str_to_fsa(s_a)
     s_b = r'''
     0 1 1 0
     0 2 2 0
     3
     '''
     fsa_b = k2host.str_to_fsa(s_b)
     self.assertTrue(k2host.is_rand_equivalent(fsa_a, fsa_b))
Пример #11
0
    def test_arc_sort(self):
        s = r'''
        0 1 2 1
        0 4 0 2
        0 2 0 3
        1 2 1 4
        1 3 0 5
        2 1 0 6
        4
        '''

        fsa = k2host.str_to_fsa(s)
        arc_map = k2host.IntArray1.create_array_with_size(fsa.size2)
        k2host.arc_sort(fsa, arc_map)
        expected_arc_indexes = torch.IntTensor([0, 3, 5, 6, 6, 6])
        expected_arcs = torch.IntTensor([[0, 2, 0, float_to_int(3)],
                                         [0, 4, 0, float_to_int(2)],
                                         [0, 1, 2, float_to_int(1)],
                                         [1, 3, 0, float_to_int(5)],
                                         [1, 2, 1, float_to_int(4)],
                                         [2, 1, 0, float_to_int(6)]])
        expected_arc_map = torch.IntTensor([2, 1, 0, 4, 3, 5])
        self.assertTrue(torch.equal(fsa.indexes, expected_arc_indexes))
        self.assertTrue(torch.equal(fsa.data, expected_arcs))
        self.assertTrue(torch.equal(arc_map.data, expected_arc_map))
Пример #12
0
    def test_case_2(self):
        s_a = r'''
        0 1 1 0
        1 2 0 0
        1 3 1 0
        1 4 2 0
        2 2 1 0
        2 3 1 0
        2 3 2 0
        3 3 0 0
        3 4 1 0
        4
        '''

        fsa_a = k2host.str_to_fsa(s_a)

        s_b = r'''
        0 1 1 0
        1 3 1 0
        1 2 2 0
        2 3 1 0
        3
        '''

        fsa_b = k2host.str_to_fsa(s_b)
        intersection = k2host.Intersection(fsa_a, fsa_b)
        array_size = k2host.IntArray2Size()
        intersection.get_sizes(array_size)
        fsa_out = k2host.Fsa.create_fsa_with_size(array_size)
        arc_map_a = k2host.IntArray1.create_array_with_size(array_size.size2)
        arc_map_b = k2host.IntArray1.create_array_with_size(array_size.size2)
        status = intersection.get_output(fsa_out, arc_map_a, arc_map_b)
        self.assertTrue(status)
        expected_arc_indexes = torch.IntTensor([0, 1, 4, 7, 8, 8, 8, 10, 10])
        expected_arcs = torch.IntTensor([[0, 1, 1, 0], [1, 2, 0, 0],
                                         [1, 3, 1, 0], [1, 4, 2, 0],
                                         [2, 5, 1, 0], [2, 3, 1, 0],
                                         [2, 6, 2, 0], [3, 3, 0, 0],
                                         [6, 6, 0, 0], [6, 7, 1, 0]])
        expected_arc_map_a = torch.IntTensor([0, 1, 2, 3, 4, 5, 6, 7, 7, 8])
        expected_arc_map_b = torch.IntTensor([0, -1, 1, 2, 1, 1, 2, -1, -1, 3])
        self.assertTrue(torch.equal(fsa_out.indexes, expected_arc_indexes))
        self.assertTrue(torch.equal(fsa_out.data, expected_arcs))
        self.assertTrue(torch.equal(arc_map_a.data, expected_arc_map_a))
        self.assertTrue(torch.equal(arc_map_b.data, expected_arc_map_b))
Пример #13
0
 def test_good_case2(self):
     s = r'''
     0 1 0 0
     0 2 0 0
     1 2 0 0
     3
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertTrue(k2host.is_top_sorted(fsa))
Пример #14
0
 def test_good_case3(self):
     s = r'''
     0 1 0 0
     0 2 -1 0
     1 2 -1 0
     2
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertTrue(k2host.is_valid(fsa))
Пример #15
0
 def test_good_case2(self):
     s = r'''
     0 1 2 0
     0 2 1 0
     1 2 1 0
     2
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertTrue(k2host.is_epsilon_free(fsa))
Пример #16
0
 def test_bad_cases2(self):
     # same label on two arcs
     s = r'''
     0 2 0 0
     0 1 0 0
     2
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertFalse(k2host.is_arc_sorted(fsa))
Пример #17
0
 def test_good_case2(self):
     s = r'''
     0 1 2 0
     1 2 0 0
     1 3 2 0
     3
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertTrue(k2host.is_deterministic(fsa))
Пример #18
0
 def test_bad_cases1(self):
     s = r'''
     0 1 2 0
     0 2 0 0
     1 2 1 0
     2
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertFalse(k2host.is_epsilon_free(fsa))
Пример #19
0
 def test_bad_cases1(self):
     s = r'''
     0 1 2 0
     1 2 0 0
     1 3 0 0
     3
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertFalse(k2host.is_deterministic(fsa))
Пример #20
0
 def test_good_case2(self):
     s = r'''
     0 1 0 0
     1 2 0 0
     1 1 0 0
     2
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertTrue(k2host.has_self_loops(fsa))
Пример #21
0
 def test_bad_cases1(self):
     s = r'''
     0 1 0 0
     0 2 0 0
     2 1 0 0
     2
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertFalse(k2host.is_top_sorted(fsa))
Пример #22
0
 def test_good_case2(self):
     s = r'''
     0 1 0 0
     0 2 0 0
     2 3 -1 0
     3
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertTrue(k2host.is_valid(fsa))
Пример #23
0
 def test_bad_cases1(self):
     s = r'''
     0 1 0 0
     0 2 0 0
     1 2 0 0
     2
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertFalse(k2host.has_self_loops(fsa))
Пример #24
0
 def test_bad_case_1(self):
     # just set arc.weight as 0 since we won't use it here
     s_a = r'''
     0 1 1 0
     0 2 2 0
     1 2 3 0
     1 3 4 0
     2 3 5 0
     3
     '''
     fsa_a = k2host.str_to_fsa(s_a)
     s_b = r'''
     0 1 1 0
     0 2 2 0
     1 2 3 0
     3
     '''
     fsa_b = k2host.str_to_fsa(s_b)
     self.assertFalse(k2host.is_rand_equivalent(fsa_a, fsa_b))
Пример #25
0
 def test_bad_cases1(self):
     s = r'''
     0 1 1 0
     0 2 2 0
     1 2 2 0
     1 3 1 0
     3
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertFalse(k2host.is_arc_sorted(fsa))
Пример #26
0
 def test_bad_case2(self):
     # only kFinalSymbol arcs enter the final state
     s = r'''
     0 1 0 0
     0 2 1 0
     1 2 0 0
     2
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertFalse(k2host.is_valid(fsa))
Пример #27
0
 def test_bad_case_2(self):
     s_a = r'''
     0 1 1 0
     0 2 2 0
     1 2 3 0
     1 3 4 0
     2 3 5 0
     3
     '''
     fsa_a = k2host.str_to_fsa(s_a)
     s_b = r'''
     0 1 1 0
     0 2 2 0
     1 2 3 0
     1 3 4 0
     2 3 6 0
     3
     '''
     fsa_b = k2host.str_to_fsa(s_b)
     self.assertFalse(k2host.is_rand_equivalent(fsa_a, fsa_b, 100))
Пример #28
0
 def test_good_case2(self):
     s = r'''
     0 1 2 0
     0 2 1 0
     1 2 0 0
     1 3 5 0
     2 3 6 0
     3
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertTrue(k2host.is_acyclic(fsa))
Пример #29
0
 def test_bad_case_2(self):
     s_a = r'''
     0 1 1 0
     0 2 2 0
     0 3 8 0
     1 4 4 0
     2 4 5 0
     4
     '''
     fsa_a = k2host.str_to_fsa(s_a)
     s_b = r'''
     0 2 1 0
     0 1 2 0
     0 3 9 0
     1 4 5 0
     2 4 4 0
     4
     '''
     fsa_b = k2host.str_to_fsa(s_b)
     self.assertTrue(k2host.is_rand_equivalent(fsa_a, fsa_b))
Пример #30
0
 def test_bad_cases1(self):
     s = r'''
     0 1 2 0
     0 4 0 0
     0 2 0 0
     1 2 1 0
     1 3 0 0
     2 1 0 0
     3
     '''
     fsa = k2host.str_to_fsa(s)
     self.assertFalse(k2host.is_acyclic(fsa))