def test_good_case_1(self): # both fsas will be empty after triming s_a = r''' 0 1 1 0 2 2 1 2 3 3 ''' fsa_a = k2.str_to_fsa(s_a) s_b = r''' 0 1 1 0 2 2 3 ''' fsa_b = k2.str_to_fsa(s_b) self.assertTrue(k2.is_rand_equivalent(fsa_a, fsa_b))
def test_arc_sort(self): s = r''' 0 1 2 0 4 0 0 2 0 1 2 1 1 3 0 2 1 0 4 ''' fsa = k2.str_to_fsa(s) 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) expected_arc_indexes = torch.IntTensor([0, 3, 5, 6, 6, 6]) expected_arcs = torch.IntTensor([[0, 2, 0], [0, 4, 0], [0, 1, 2], [1, 3, 0], [1, 2, 1], [2, 1, 0]]) 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))
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 0 2 2 1 0 1 2 6 -1 3 5 5 3 2 2 3 5 5 4 4 4 5 3 3 5 4 4 6 ''' 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) status = connection.get_output(fsa_out) self.assertFalse(status) self.assertFalse(k2.is_top_sorted(fsa_out))
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_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_case_1(self): # a non-connected, non-topsorted, acyclic input fsa; # the output fsa is topsorted. s = r''' 0 1 1 0 2 2 1 3 3 1 6 -1 2 4 2 2 6 -1 2 1 1 5 0 1 6 ''' 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) expected_arc_indexes = torch.IntTensor([0, 2, 4, 5, 5]) expected_arcs = torch.IntTensor([[0, 2, 1], [0, 1, 2], [1, 3, -1], [1, 2, 1], [2, 3, -1]]) 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))
def test_case_4(self): # connected fsa s = r''' 0 4 40 0 2 20 1 6 -1 2 3 30 3 6 -1 3 1 10 4 5 50 5 2 8 6 ''' 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.assertTrue(status) expected_arc_indexes = torch.IntTensor([0, 2, 3, 4, 5, 7, 8, 8]) expected_arcs = torch.IntTensor([[0, 1, 40], [0, 3, 20], [1, 2, 50], [2, 3, 8], [3, 4, 30], [4, 6, -1], [4, 5, 10], [5, 6, -1]]) expected_state_map = torch.IntTensor([0, 4, 5, 2, 3, 1, 6]) self.assertTrue(torch.equal(fsa_out.indexes, expected_arc_indexes)) self.assertTrue(torch.equal(fsa_out.data, expected_arcs)) self.assertTrue(torch.equal(state_map.data, expected_state_map))
def test_bad_cases1(self): s = r''' 0 2 0 2 ''' fsa = k2.str_to_fsa(s) self.assertFalse(k2.is_connected(fsa))
def setUp(self): s_a = r''' 0 1 1 0 1 2 0 1 3 0 2 4 0 2 5 1 3 5 1 3 6 2 4 5 2 4 6 3 5 -1 4 5 -1 5 ''' self.fsa_a = k2.str_to_fsa(s_a) weights_a = torch.FloatTensor([2, 2, 3, 3, 1, 3, 2, 5, 4, 1, 3]) self.weights_a = k2.FloatArray1(weights_a) s_b = r''' 0 1 1 0 1 2 0 1 3 0 1 4 0 1 5 1 2 5 1 2 6 2 3 -1 3 ''' self.fsa_b = k2.str_to_fsa(s_b) weights_b = torch.FloatTensor([5, 5, 6, 10, 8, 1, 0, 0]) self.weights_b = k2.FloatArray1(weights_b) s_c = r''' 0 1 1 0 1 2 0 1 3 0 1 4 0 1 5 1 2 5 1 2 6 2 3 -1 3 ''' self.fsa_c = k2.str_to_fsa(s_c) weights_c = torch.FloatTensor([5, 5, 6, 10, 9, 1, 0, 0]) self.weights_c = k2.FloatArray1(weights_c)
def test_good_case3(self): s = r''' 0 1 0 0 2 -1 1 2 -1 2 ''' fsa = k2.str_to_fsa(s) self.assertTrue(k2.is_valid(fsa))
def test_bad_cases2(self): # same label on two arcs s = r''' 0 2 0 0 1 0 2 ''' fsa = k2.str_to_fsa(s) self.assertFalse(k2.is_arc_sorted(fsa))
def test_good_case2(self): s = r''' 0 1 0 1 2 0 1 1 0 2 ''' fsa = k2.str_to_fsa(s) self.assertTrue(k2.has_self_loops(fsa))
def test_good_case2(self): s = r''' 0 1 2 0 2 1 1 2 1 2 ''' fsa = k2.str_to_fsa(s) self.assertTrue(k2.is_epsilon_free(fsa))
def test_bad_cases1(self): s = r''' 0 1 2 0 2 0 1 2 1 2 ''' fsa = k2.str_to_fsa(s) self.assertFalse(k2.is_epsilon_free(fsa))
def test_good_case2(self): s = r''' 0 1 2 1 2 0 1 3 2 3 ''' fsa = k2.str_to_fsa(s) self.assertTrue(k2.is_deterministic(fsa))
def test_bad_cases1(self): s = r''' 0 1 2 1 2 0 1 3 0 3 ''' fsa = k2.str_to_fsa(s) self.assertFalse(k2.is_deterministic(fsa))
def test_bad_cases1(self): s = r''' 0 1 0 0 2 0 2 1 0 2 ''' fsa = k2.str_to_fsa(s) self.assertFalse(k2.is_top_sorted(fsa))
def test_good_case2(self): s = r''' 0 1 0 0 2 0 1 2 0 3 ''' fsa = k2.str_to_fsa(s) self.assertTrue(k2.is_top_sorted(fsa))
def test_bad_cases1(self): s = r''' 0 1 0 0 2 0 1 2 0 2 ''' fsa = k2.str_to_fsa(s) self.assertFalse(k2.has_self_loops(fsa))
def test_good_case2(self): s = r''' 0 1 0 0 2 0 2 3 -1 3 ''' fsa = k2.str_to_fsa(s) self.assertTrue(k2.is_valid(fsa))
def test_bad_case_1(self): s_a = r''' 0 1 1 0 2 2 1 2 3 1 3 4 2 3 5 3 ''' fsa_a = k2.str_to_fsa(s_a) s_b = r''' 0 1 1 0 2 2 1 2 3 3 ''' fsa_b = k2.str_to_fsa(s_b) self.assertFalse(k2.is_rand_equivalent(fsa_a, fsa_b))
def test_case_2(self): s_a = r''' 0 1 1 1 2 0 1 3 1 1 4 2 2 2 1 2 3 1 2 3 2 3 3 0 3 4 1 4 ''' fsa_a = k2.str_to_fsa(s_a) s_b = r''' 0 1 1 1 3 1 1 2 2 2 3 1 3 ''' fsa_b = k2.str_to_fsa(s_b) 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) expected_arc_indexes = torch.IntTensor([0, 1, 4, 7, 8, 8, 8, 10, 10]) expected_arcs = torch.IntTensor([[0, 1, 1], [1, 2, 0], [1, 3, 1], [1, 4, 2], [2, 5, 1], [2, 3, 1], [2, 6, 2], [3, 3, 0], [6, 6, 0], [6, 7, 1]]) 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))
def test_good_case2(self): s = r''' 0 1 0 0 3 0 1 2 0 2 3 0 3 ''' fsa = k2.str_to_fsa(s) self.assertTrue(k2.is_connected(fsa))
def test_bad_case2(self): # only kFinalSymbol arcs enter the final state s = r''' 0 1 0 0 2 1 1 2 0 2 ''' fsa = k2.str_to_fsa(s) self.assertFalse(k2.is_valid(fsa))
def test_bad_cases1(self): s = r''' 0 1 1 0 2 2 1 2 2 1 3 1 3 ''' fsa = k2.str_to_fsa(s) self.assertFalse(k2.is_arc_sorted(fsa))
def test_bad_case_2(self): s_a = r''' 0 1 1 0 2 2 0 3 8 1 4 4 2 4 5 4 ''' fsa_a = k2.str_to_fsa(s_a) s_b = r''' 0 2 1 0 1 2 0 3 9 1 4 5 2 4 4 4 ''' fsa_b = k2.str_to_fsa(s_b) self.assertTrue(k2.is_rand_equivalent(fsa_a, fsa_b))
def test_good_case2(self): s = r''' 0 1 2 0 2 1 1 2 0 1 3 5 2 3 6 3 ''' fsa = k2.str_to_fsa(s) self.assertTrue(k2.is_acyclic(fsa))
def test_good_case_2(self): # same fsas s_a = r''' 0 1 1 0 2 2 1 2 3 1 3 4 2 3 5 3 ''' fsa_a = k2.str_to_fsa(s_a) self.assertTrue(k2.is_rand_equivalent(fsa_a, fsa_a))
def test_good_case3(self): s = r''' 0 3 0 1 2 0 2 3 0 2 3 0 2 4 0 3 1 0 4 ''' fsa = k2.str_to_fsa(s) self.assertTrue(k2.is_connected(fsa))
def test_bad_cases1(self): s = r''' 0 1 2 0 4 0 0 2 0 1 2 1 1 3 0 2 1 0 3 ''' fsa = k2.str_to_fsa(s) self.assertFalse(k2.is_acyclic(fsa))