def test_empty_fsa(self): array_size = k2.IntArray2Size(0, 0) fsa = k2.Fsa.create_fsa_with_size(array_size) arc_map = k2.IntArray1.create_array_with_size(fsa.size2) k2.arc_sort(fsa, arc_map) self.assertTrue(k2.is_empty(fsa)) self.assertTrue(arc_map.empty()) # test without arc_map k2.arc_sort(fsa) self.assertTrue(k2.is_empty(fsa))
def test_empty_fsa(self): array_size = k2.IntArray2Size(0, 0) fsa = k2.Fsa.create_fsa_with_size(array_size) sorter = k2.ArcSorter(fsa) array_size = k2.IntArray2Size() sorter.get_sizes(array_size) fsa_out = k2.Fsa.create_fsa_with_size(array_size) arc_map = k2.IntArray1.create_array_with_size(array_size.size2) sorter.get_output(fsa_out, arc_map) self.assertTrue(k2.is_empty(fsa)) # test without arc_map sorter.get_output(fsa_out) self.assertTrue(k2.is_empty(fsa_out))
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 2 2 1 3 3 1 6 6 2 4 2 2 6 3 2 6 -1 5 0 1 5 7 -1 7 ''' fsa = k2.str_to_fsa(s) connection = k2.Connection(fsa) array_size = k2.IntArray2Size() connection.get_sizes(array_size) fsa_out = k2.Fsa.create_fsa_with_size(array_size) arc_map = k2.IntArray1.create_array_with_size(array_size.size2) status = connection.get_output(fsa_out, arc_map) self.assertTrue(status) self.assertTrue(k2.is_empty(fsa_out)) self.assertTrue(arc_map.empty())
def test_bad_case1(self): s = r''' 0 1 2 1 ''' fsa = k2.str_to_fsa(s) self.assertFalse(k2.is_empty(fsa))
def test_case_1(self): # empty fsa array_size = k2.IntArray2Size(0, 0) fsa = k2.Fsa.create_fsa_with_size(array_size) sorter = k2.TopSorter(fsa) array_size = k2.IntArray2Size() sorter.get_sizes(array_size) fsa_out = k2.Fsa.create_fsa_with_size(array_size) state_map = k2.IntArray1.create_array_with_size(array_size.size1) status = sorter.get_output(fsa_out, state_map) self.assertTrue(status) self.assertTrue(k2.is_empty(fsa_out)) self.assertTrue(state_map.empty()) # test without arc_map sorter.get_output(fsa_out) self.assertTrue(k2.is_empty(fsa_out))
def test_case_1(self): # empty fsa array_size = k2.IntArray2Size(0, 0) fsa_a = k2.Fsa.create_fsa_with_size(array_size) fsa_b = k2.Fsa.create_fsa_with_size(array_size) intersection = k2.Intersection(fsa_a, fsa_b) array_size = k2.IntArray2Size() intersection.get_sizes(array_size) fsa_out = k2.Fsa.create_fsa_with_size(array_size) arc_map_a = k2.IntArray1.create_array_with_size(array_size.size2) arc_map_b = k2.IntArray1.create_array_with_size(array_size.size2) status = intersection.get_output(fsa_out, arc_map_a, arc_map_b) self.assertTrue(status) self.assertTrue(k2.is_empty(fsa_out)) self.assertTrue(arc_map_a.empty()) self.assertTrue(arc_map_b.empty()) # test without arc_map status = intersection.get_output(fsa_out) self.assertTrue(status) self.assertTrue(k2.is_empty(fsa_out))
def test_bad_case_1(self): # empty fsa array_size = k2.IntArray2Size(0, 0) fsa = k2.Fsa.create_fsa_with_size(array_size) rand_path = k2.RandPath(fsa, False) array_size = k2.IntArray2Size() rand_path.get_sizes(array_size) path = k2.Fsa.create_fsa_with_size(array_size) arc_map = k2.IntArray1.create_array_with_size(array_size.size2) status = rand_path.get_output(path, arc_map) self.assertFalse(status) self.assertTrue(k2.is_empty(path)) self.assertTrue(arc_map.empty())
def test_case_1(self): # empty fsa array_size = k2.IntArray2Size(0, 0) fsa_in = k2.Fsa.create_fsa_with_size(array_size) indexes = torch.IntTensor([0, 1, 3, 6, 7]) data = torch.IntTensor([1, 2, 3, 4, 5, 6, 7]) labels_in = k2.AuxLabels(indexes, data) inverter = k2.FstInverter(fsa_in, labels_in) fsa_size = k2.IntArray2Size() aux_size = k2.IntArray2Size() inverter.get_sizes(fsa_size, aux_size) self.assertEqual(aux_size.size1, 0) self.assertEqual(aux_size.size2, 0) fsa_out = k2.Fsa.create_fsa_with_size(fsa_size) labels_out = k2.AuxLabels.create_array_with_size(aux_size) inverter.get_output(fsa_out, labels_out) self.assertTrue(k2.is_empty(fsa_out)) self.assertTrue(labels_out.empty())
def test_bad_case_2(self): # non-connected fsa s_a = r''' 0 1 1 0 2 2 1 3 4 3 ''' fsa = k2.str_to_fsa(s_a) rand_path = k2.RandPath(fsa, False) array_size = k2.IntArray2Size() rand_path.get_sizes(array_size) path = k2.Fsa.create_fsa_with_size(array_size) arc_map = k2.IntArray1.create_array_with_size(array_size.size2) status = rand_path.get_output(path, arc_map) self.assertFalse(status) self.assertTrue(k2.is_empty(path)) self.assertTrue(arc_map.empty())
def test_case_3(self): # non-connected fsa (not accessible) s = r''' 0 2 -1 1 0 1 1 2 0 2 ''' fsa = k2.str_to_fsa(s) sorter = k2.TopSorter(fsa) array_size = k2.IntArray2Size() sorter.get_sizes(array_size) fsa_out = k2.Fsa.create_fsa_with_size(array_size) state_map = k2.IntArray1.create_array_with_size(array_size.size1) status = sorter.get_output(fsa_out, state_map) self.assertFalse(status) self.assertTrue(k2.is_empty(fsa_out)) self.assertTrue(state_map.empty())
def test_good_case_1(self): s_a = r''' 0 1 1 0 2 2 1 2 3 2 3 4 2 4 5 3 4 7 4 5 9 5 ''' fsa = k2.str_to_fsa(s_a) rand_path = k2.RandPath(fsa, False) array_size = k2.IntArray2Size() rand_path.get_sizes(array_size) path = k2.Fsa.create_fsa_with_size(array_size) status = rand_path.get_output(path) self.assertTrue(status) self.assertFalse(k2.is_empty(path))
def test_eps_arc_1(self): s_a = r''' 0 1 1 0 2 0 1 2 3 2 3 0 2 4 5 3 4 7 4 5 9 5 ''' fsa = k2.str_to_fsa(s_a) rand_path = k2.RandPath(fsa, True) array_size = k2.IntArray2Size() rand_path.get_sizes(array_size) path = k2.Fsa.create_fsa_with_size(array_size) arc_map = k2.IntArray1.create_array_with_size(array_size.size2) status = rand_path.get_output(path, arc_map) self.assertTrue(status) self.assertFalse(k2.is_empty(path)) self.assertFalse(arc_map.empty())
def test_good_case_2(self): s_a = r''' 0 1 1 1 2 3 2 3 4 3 ''' fsa = k2.str_to_fsa(s_a) rand_path = k2.RandPath(fsa, False) array_size = k2.IntArray2Size() rand_path.get_sizes(array_size) path = k2.Fsa.create_fsa_with_size(array_size) arc_map = k2.IntArray1.create_array_with_size(array_size.size2) status = rand_path.get_output(path, arc_map) self.assertTrue(status) self.assertFalse(k2.is_empty(path)) self.assertFalse(arc_map.empty()) expected_arc_indexes = torch.IntTensor([0, 1, 2, 3, 3]) expected_arcs = torch.IntTensor([[0, 1, 1], [1, 2, 3], [2, 3, 4]]) expected_arc_map = torch.IntTensor([0, 1, 2]) 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))
def test_good_cases1(self): array_size = k2.IntArray2Size(0, 0) fsa = k2.Fsa.create_fsa_with_size(array_size) self.assertTrue(k2.is_empty(fsa))