def test_mut_trs_iterator(): fst = VectorFst() s1 = fst.add_state() s2 = fst.add_state() fst.set_start(s1) fst.set_final(s2, 0.54) tr1 = Tr(1, 18, 2.33, s2) fst.add_tr(s1, tr1) trs_it = MutableTrsIterator(fst, s1) tr = trs_it.value() assert tr.ilabel == 1 assert tr.olabel == 18 assert pytest.approx(tr.weight) == pytest.approx(2.33) assert tr.next_state == s2 next(trs_it) assert trs_it.done() trs_it.reset() assert not trs_it.done() tr1 = Tr(10, 18, 2.33, s2) trs_it.set_value(tr1) trs_it = fst.trs(s1) assert next(trs_it).ilabel == 10
def test_fst_unset_final(): fst = VectorFst() s = fst.add_state() assert not fst.is_final(s) fst.set_final(s) assert fst.is_final(s) fst.unset_final(s) assert not fst.is_final(s)
def test_trs_iterator(): fst = VectorFst() s1 = fst.add_state() s2 = fst.add_state() fst.set_start(s1) fst.set_final(s2, 0.54) tr1 = Tr(1, 18, 2.33, s2) fst.add_tr(s1, tr1) trs_it = TrsIterator(fst, s1) tr = next(trs_it) assert tr.ilabel == 1 assert tr.olabel == 18 assert pytest.approx(tr.weight) == pytest.approx(2.33) assert tr.next_state == s2 assert trs_it.done() trs_it.reset() assert not trs_it.done() for tr in trs_it: assert tr == tr1
def test_fst_from_bytes(): fst = VectorFst() # States s1 = fst.add_state() s2 = fst.add_state() fst.set_start(s1) fst.set_final(s2) with NamedTemporaryFile() as f: fst.write(f.name) bytes = Path(f.name).read_bytes() fst_loaded = VectorFst.from_bytes(bytes) assert fst == fst_loaded
def test_fst_io_bytes(): fst = VectorFst() # States s1 = fst.add_state() s2 = fst.add_state() fst.set_start(s1) fst.set_final(s2) fst_loaded = VectorFst.from_bytes(fst.to_bytes()) assert fst_loaded == fst
def test_state_iterator(): fst = VectorFst() s1 = fst.add_state() s2 = fst.add_state() fst.set_start(s1) fst.set_final(s2, 0.54) tr1 = Tr(1, 18, 2.33, s2) fst.add_tr(s1, tr1) states_it = StateIterator(fst) num_visited_states = 0 for idx, state in enumerate(states_it): num_visited_states += 1 assert idx == state assert num_visited_states == fst.num_states()
def test_symt_copy_add(): fst = VectorFst() symt = SymbolTable.from_symbols(["a", "b"]) fst.set_input_symbols(symt) fst.set_output_symbols(symt) symt2 = fst.input_symbols().copy() symt2.add_symbol("c") assert symt2.num_symbols() == symt.num_symbols() + 1
def test_fst_states_iterator(): fst = VectorFst() # States s1 = fst.add_state() s2 = fst.add_state() fst.set_start(s1) fst.set_final(s2) for idx, state in enumerate(fst.states()): assert state == idx
def test_fst_to_bytes(): fst = VectorFst() # States s1 = fst.add_state() s2 = fst.add_state() fst.set_start(s1) fst.set_final(s2) bytes = fst.to_bytes() with NamedTemporaryFile() as f: Path(f.name).write_bytes(bytes) fst_read = VectorFst.read(f.name) assert fst == fst_read
def test_fst_del_states(): fst = VectorFst() # States fst.add_state() fst.add_state() fst.delete_states() assert fst.num_states() == 0
def test_fst_print(): fst = VectorFst() # States s1 = fst.add_state() s2 = fst.add_state() fst.set_start(s1) fst.set_final(s2) # Check print is not crashing print(fst)
def test_concat_list(): concat_list([VectorFst(), VectorFst(), VectorFst()])
def test_optimize_fst(): # FST 1 fst1 = VectorFst() s1 = fst1.add_state() s2 = fst1.add_state() s3 = fst1.add_state() s4 = fst1.add_state() fst1.set_start(s1) fst1.set_final(s4, 0.0) tr1_1 = Tr(1, 2, 1.0, s2) fst1.add_tr(s1, tr1_1) tr1_2 = Tr(1, 3, 2.0, s3) fst1.add_tr(s1, tr1_2) tr1_3 = Tr(0, 0, 3.0, s4) fst1.add_tr(s2, tr1_3) tr1_4 = Tr(4, 6, 4.0, s4) fst1.add_tr(s2, tr1_4) tr1_5 = Tr(7, 8, 5.0, s4) fst1.add_tr(s3, tr1_5) # Expected FST expected_fst = VectorFst() s1 = expected_fst.add_state() s2 = expected_fst.add_state() s3 = expected_fst.add_state() s4 = expected_fst.add_state() expected_fst.set_start(s1) expected_fst.set_final(s2, 0.0) expected_fst.set_final(s4, 0.0) tr_1 = Tr(1, 2, 4.0, s2) expected_fst.add_tr(s1, tr_1) tr_2 = Tr(1, 3, 7.0, s3) expected_fst.add_tr(s1, tr_2) tr_4 = Tr(4, 6, 1.0, s4) expected_fst.add_tr(s2, tr_4) tr_5 = Tr(7, 8, None, s4) expected_fst.add_tr(s3, tr_5) fst1.optimize() assert fst1 == expected_fst
def test_randgen(): fst = VectorFst() s0 = fst.add_state() s1 = fst.add_state() fst.set_start(s0) fst.set_final(s1) fst.add_tr(s0, Tr(2, 2, weight_one(), s1)) fst.add_tr(s0, Tr(3, 3, weight_one(), s1)) res = randgen(ifst=fst, seed=33) assert res.num_states() == 2 for tr in fst.trs(fst.start()): assert tr.ilabel in {2, 3} assert tr.olabel in {2, 3}
def test_concat_fst(): # FST 1 fst1 = VectorFst() s1 = fst1.add_state() s2 = fst1.add_state() fst1.set_start(s1) fst1.set_final(s2, 0.2) tr1_1 = Tr(1, 2, 1.0, s2) fst1.add_tr(s1, tr1_1) tr1_2 = Tr(3, 4, 2.5, s2) fst1.add_tr(s2, tr1_2) # FST 2 fst2 = VectorFst() s1 = fst2.add_state() s2 = fst2.add_state() fst2.set_start(s1) fst2.set_final(s2, 1.5) tr2_1 = Tr(1, 2, 3.0, s1) fst2.add_tr(s1, tr2_1) tr2_2 = Tr(4, 5, 2.0, s2) fst2.add_tr(s1, tr2_2) # Expected FST expected_fst = VectorFst() s1 = expected_fst.add_state() s2 = expected_fst.add_state() s3 = expected_fst.add_state() s4 = expected_fst.add_state() expected_fst.set_start(s1) expected_fst.set_final(s4, 1.5) tr3_1 = Tr(1, 2, 1.0, s2) expected_fst.add_tr(s1, tr3_1) tr3_2 = Tr(3, 4, 2.5, s2) expected_fst.add_tr(s2, tr3_2) tr3_3 = Tr(0, 0, 0.2, s3) expected_fst.add_tr(s2, tr3_3) tr3_4 = Tr(1, 2, 3.0, s3) expected_fst.add_tr(s3, tr3_4) tr3_5 = Tr(4, 5, 2.0, s4) expected_fst.add_tr(s3, tr3_5) fst3 = fst1.concat(fst2) assert fst3 == expected_fst
def test_reverse(): # FST 1 fst1 = VectorFst() s1 = fst1.add_state() s2 = fst1.add_state() s3 = fst1.add_state() fst1.set_start(s1) fst1.set_final(s3, 1.0) tr1_1 = Tr(1, 2, 1.0, s2) fst1.add_tr(s1, tr1_1) tr1_2 = Tr(3, 4, 2.0, s2) fst1.add_tr(s1, tr1_2) tr1_3 = Tr(5, 6, 1.5, s2) fst1.add_tr(s2, tr1_3) tr1_4 = Tr(3, 5, 1.0, s3) fst1.add_tr(s2, tr1_4) # Expected FST expected_fst = VectorFst() s1 = expected_fst.add_state() s2 = expected_fst.add_state() s3 = expected_fst.add_state() s4 = expected_fst.add_state() expected_fst.set_start(s1) expected_fst.set_final(s2) tr1_1 = Tr(0, 0, 1.0, s4) expected_fst.add_tr(s1, tr1_1) tr1_2 = Tr(3, 5, 1.0, s3) expected_fst.add_tr(s4, tr1_2) tr1_3 = Tr(1, 2, 1.0, s2) expected_fst.add_tr(s3, tr1_3) tr1_4 = Tr(3, 4, 2.0, s2) expected_fst.add_tr(s3, tr1_4) tr1_5 = Tr(5, 6, 1.5, s3) expected_fst.add_tr(s3, tr1_5) reversed_fst = fst1.reverse() assert expected_fst == reversed_fst
def test_fst_read_write(): fst = VectorFst() # States s1 = fst.add_state() s2 = fst.add_state() fst.set_start(s1) fst.set_final(s2) tr_1 = Tr(3, 5, 10.0, s2) tr_2 = Tr(5, 7, 18.0, s2) fst.add_tr(s1, tr_1) fst.add_tr(s1, tr_2) fst.write("/tmp/test.fst") read_fst = VectorFst.read("/tmp/test.fst") assert fst == read_fst
def test_acceptor(): symt = SymbolTable() symt.add_symbol("hello") symt.add_symbol("world") f = acceptor("hello world", symt) # Expected FST expected_fst = VectorFst() s1 = expected_fst.add_state() s2 = expected_fst.add_state() s3 = expected_fst.add_state() expected_fst.set_start(s1) expected_fst.set_final(s3) tr1 = Tr(1, 1, None, s2) expected_fst.add_tr(s1, tr1) tr2 = Tr(2, 2, None, s3) expected_fst.add_tr(s2, tr2) assert f == expected_fst
def test_tr_unique_1(): # FST 1 fst1 = VectorFst() s1 = fst1.add_state() s2 = fst1.add_state() fst1.set_start(s1) fst1.set_final(s2, 0.0) tr1_1 = Tr(1, 2, 1.0, s2) fst1.add_tr(s1, tr1_1) tr1_2 = Tr(1, 2, 1.0, s2) fst1.add_tr(s1, tr1_2) tr1_3 = Tr(1, 2, 2.0, s2) fst1.add_tr(s1, tr1_3) tr1_4 = Tr(2, 2, 1.0, s2) fst1.add_tr(s1, tr1_4) # Expected FST expected_fst = VectorFst() s1 = expected_fst.add_state() s2 = expected_fst.add_state() expected_fst.set_start(s1) expected_fst.set_final(s2, 0.0) tr_1 = Tr(1, 2, 1.0, s2) expected_fst.add_tr(s1, tr_1) tr_2 = Tr(1, 2, 2.0, s2) expected_fst.add_tr(s1, tr_2) tr_4 = Tr(2, 2, 1.0, s2) expected_fst.add_tr(s1, tr_4) fst1.tr_unique() assert fst1 == expected_fst
def test_compose_config(): compose_filter = ComposeFilter.TRIVIALFILTER compose_config = ComposeConfig(compose_filter, True) # Checked on debug print # FST 1 fst1 = VectorFst() s1 = fst1.add_state() s2 = fst1.add_state() s3 = fst1.add_state() fst1.set_start(s1) fst1.set_final(s2) fst1.set_final(s3) tr1_1 = Tr(1, 2, 1.0, s2) fst1.add_tr(s1, tr1_1) tr1_2 = Tr(1, 4, 2.0, s3) fst1.add_tr(s1, tr1_2) tr1_3 = Tr(3, 5, 2.0, s2) fst1.add_tr(s2, tr1_3) # FST 2 fst2 = VectorFst() s1 = fst2.add_state() s2 = fst2.add_state() s3 = fst2.add_state() fst2.set_start(s1) fst2.set_final(s3) tr2_1 = Tr(2, 6, 1.0, s2) fst2.add_tr(s1, tr2_1) tr2_2 = Tr(5, 7, 2.5, s3) fst2.add_tr(s2, tr2_2) tr2_3 = Tr(5, 8, 1.5, s3) fst2.add_tr(s3, tr2_3) tr2_4 = Tr(4, 9, 3.0, s3) fst2.add_tr(s1, tr2_4) # Expected FST expected_fst = VectorFst() s1 = expected_fst.add_state() s2 = expected_fst.add_state() s3 = expected_fst.add_state() s4 = expected_fst.add_state() expected_fst.set_start(s1) expected_fst.set_final(s3) expected_fst.set_final(s4) tr3_1 = Tr(1, 6, 2.0, s2) expected_fst.add_tr(s1, tr3_1) tr3_2 = Tr(1, 9, 5.0, s3) expected_fst.add_tr(s1, tr3_2) tr3_3 = Tr(3, 7, 4.5, s4) expected_fst.add_tr(s2, tr3_3) tr3_4 = Tr(3, 8, 3.5, s4) expected_fst.add_tr(s4, tr3_4) fst3 = fst1.compose(fst2, compose_config) assert fst3 == expected_fst
def test_connect(): # FST 1 fst1 = VectorFst() s1 = fst1.add_state() s2 = fst1.add_state() s3 = fst1.add_state() s4 = fst1.add_state() s5 = fst1.add_state() fst1.set_start(s1) fst1.set_final(s2, 0.0) tr1_1 = Tr(1, 2, 1.0, s1) fst1.add_tr(s5, tr1_1) tr1_2 = Tr(3, 4, 2.0, s2) fst1.add_tr(s1, tr1_2) tr1_3 = Tr(4, 5, 3.0, s3) fst1.add_tr(s2, tr1_3) tr1_4 = Tr(4, 6, 4.0, s4) fst1.add_tr(s3, tr1_4) tr1_5 = Tr(7, 8, 5.0, s1) fst1.add_tr(s3, tr1_5) # Expected FST expected_fst = VectorFst() s1 = expected_fst.add_state() s2 = expected_fst.add_state() s3 = expected_fst.add_state() expected_fst.set_start(s1) expected_fst.set_final(s2, 0.0) tr_1 = Tr(3, 4, 2.0, s2) expected_fst.add_tr(s1, tr_1) tr_2 = Tr(4, 5, 3.0, s3) expected_fst.add_tr(s2, tr_2) tr_3 = Tr(7, 8, 5.0, s1) expected_fst.add_tr(s3, tr_3) fst1.connect() assert expected_fst == fst1
def test_shortest_path(): # FST 1 fst1 = VectorFst() s1 = fst1.add_state() s2 = fst1.add_state() s3 = fst1.add_state() s4 = fst1.add_state() fst1.set_start(s1) fst1.set_final(s4, 2.0) tr1_1 = Tr(1, 1, 3.0, s2) fst1.add_tr(s1, tr1_1) tr1_2 = Tr(2, 2, 2.0, s2) fst1.add_tr(s2, tr1_2) tr1_3 = Tr(3, 3, 4.0, s4) fst1.add_tr(s2, tr1_3) tr1_4 = Tr(4, 4, 5.0, s3) fst1.add_tr(s1, tr1_4) tr1_5 = Tr(5, 5, 4.0, s4) fst1.add_tr(s3, tr1_5) # Expected FST expected_fst = VectorFst() s1 = expected_fst.add_state() s2 = expected_fst.add_state() s3 = expected_fst.add_state() expected_fst.set_start(s3) expected_fst.set_final(s1, 2.0) tr1_1 = Tr(1, 1, 3.0, s2) expected_fst.add_tr(s3, tr1_1) tr1_2 = Tr(3, 3, 4.0, s1) expected_fst.add_tr(s2, tr1_2) config = ShortestPathConfig(1, True) shortes_path = fst1.shortest_path(config) assert shortes_path == expected_fst
def test_union_list(): union_list([VectorFst(), VectorFst(), VectorFst()])
def test_rm_epsilon(): # FST 1 fst1 = VectorFst() s1 = fst1.add_state() s2 = fst1.add_state() s3 = fst1.add_state() s4 = fst1.add_state() fst1.set_start(s1) fst1.set_final(s4, 1.0) tr1_1 = Tr(0, 0, 1.0, s2) fst1.add_tr(s1, tr1_1) tr1_2 = Tr(1, 0, 2.0, s3) fst1.add_tr(s2, tr1_2) tr1_3 = Tr(0, 2, 3.0, s3) fst1.add_tr(s2, tr1_3) tr1_4 = Tr(0, 0, 4.0, s3) fst1.add_tr(s2, tr1_4) tr1_5 = Tr(0, 0, 5.0, s3) fst1.add_tr(s3, tr1_5) tr1_6 = Tr(0, 0, 5.0, s4) fst1.add_tr(s3, tr1_6) # Expected FST expected_fst = VectorFst() s1 = expected_fst.add_state() s2 = expected_fst.add_state() expected_fst.set_start(s1) expected_fst.set_final(s1, 11.0) expected_fst.set_final(s2, 6.0) tr1_1 = Tr(0, 2, 4.0, s2) expected_fst.add_tr(s1, tr1_1) tr1_2 = Tr(1, 0, 3.0, s2) expected_fst.add_tr(s1, tr1_2) fst1.rm_epsilon() assert expected_fst == fst1
def test_replace(): # FST 1 fst1 = VectorFst() s1 = fst1.add_state() s2 = fst1.add_state() s3 = fst1.add_state() s4 = fst1.add_state() fst1.set_start(s1) fst1.set_final(s4) # call:call tr1_1 = Tr(1, 1, None, s2) fst1.add_tr(s1, tr1_1) # NAME:NAME tr1_2 = Tr(10, 10, None, s3) fst1.add_tr(s2, tr1_2) # now:now tr1_3 = Tr(2, 2, None, s4) fst1.add_tr(s3, tr1_3) # FST 2 fst2 = VectorFst() s1 = fst2.add_state() s2 = fst2.add_state() s3 = fst2.add_state() fst2.set_start(s1) fst2.set_final(s3) # FIRST_NAME:FIRST_NAME tr2_1 = Tr(11, 11, None, s2) fst2.add_tr(s1, tr2_1) # LAST_NAME:LAST_NAME tr2_2 = Tr(12, 12, None, s3) fst2.add_tr(s2, tr2_2) # FST 3 fst3 = VectorFst() s1 = fst3.add_state() s2 = fst3.add_state() fst3.set_start(s1) fst3.set_final(s2) # david:david tr3_1 = Tr(3, 3, None, s2) fst3.add_tr(s1, tr3_1) # john:john tr3_2 = Tr(4, 4, None, s2) fst3.add_tr(s1, tr3_2) # FST 4 fst4 = VectorFst() s1 = fst4.add_state() s2 = fst4.add_state() fst4.set_start(s1) fst4.set_final(s2) # bowie:bowie tr4_1 = Tr(5, 5, None, s2) fst4.add_tr(s1, tr4_1) # williams:williaw tr4_2 = Tr(6, 6, None, s2) fst4.add_tr(s1, tr4_2) # Expected FST expected_fst = VectorFst() s1 = expected_fst.add_state() s2 = expected_fst.add_state() s3 = expected_fst.add_state() s4 = expected_fst.add_state() s5 = expected_fst.add_state() s6 = expected_fst.add_state() s7 = expected_fst.add_state() s8 = expected_fst.add_state() s9 = expected_fst.add_state() s10 = expected_fst.add_state() s11 = expected_fst.add_state() expected_fst.set_start(s1) expected_fst.set_final(s11) # call:call tr_1 = Tr(1, 1, None, s2) expected_fst.add_tr(s1, tr_1) # NAME:<eps> tr_2 = Tr(10, 0, None, s3) expected_fst.add_tr(s2, tr_2) # FIRST_NAME:<eps> tr_3 = Tr(11, 0, None, s4) expected_fst.add_tr(s3, tr_3) # david:david tr_4 = Tr(3, 3, None, s5) expected_fst.add_tr(s4, tr_4) # john:john tr_5 = Tr(4, 4, None, s5) expected_fst.add_tr(s4, tr_5) # <eps>:<eps> tr_eps = Tr(0, 0, None, s6) expected_fst.add_tr(s5, tr_eps) # LAST_NAME:<eps> tr_6 = Tr(12, 0, None, s7) expected_fst.add_tr(s6, tr_6) # bowie:bowie tr_7 = Tr(5, 5, None, s8) expected_fst.add_tr(s7, tr_7) # williams:williaw tr_8 = Tr(6, 6, None, s8) expected_fst.add_tr(s7, tr_8) # <eps>:<eps> tr_9 = Tr(0, 0, None, s9) expected_fst.add_tr(s8, tr_9) # <eps>:<eps> tr_10 = Tr(0, 0, None, s10) expected_fst.add_tr(s9, tr_10) # now:now tr_11 = Tr(2, 2, None, s11) expected_fst.add_tr(s10, tr_11) replaced_fst = fst1.replace(100, [(10, fst2), (11, fst3), (12, fst4)], False) assert replaced_fst == expected_fst
def test_determinize_fst(): # FST 1 fst1 = VectorFst() s1 = fst1.add_state() s2 = fst1.add_state() s3 = fst1.add_state() s4 = fst1.add_state() fst1.set_start(s1) fst1.set_final(s4, 0.0) tr1_1 = Tr(1, 2, 1.0, s2) fst1.add_tr(s1, tr1_1) tr1_2 = Tr(1, 3, 2.0, s3) fst1.add_tr(s1, tr1_2) tr1_3 = Tr(4, 5, 3.0, s4) fst1.add_tr(s2, tr1_3) tr1_4 = Tr(4, 6, 4.0, s4) fst1.add_tr(s2, tr1_4) tr1_5 = Tr(7, 8, 5.0, s4) fst1.add_tr(s3, tr1_5) # Expected FST expected_fst = VectorFst() s1 = expected_fst.add_state() s2 = expected_fst.add_state() s3 = expected_fst.add_state() s4 = expected_fst.add_state() s5 = expected_fst.add_state() expected_fst.set_start(s1) expected_fst.set_final(s5) tr1_1 = Tr(1, 0, 1.0, s2) expected_fst.add_tr(s1, tr1_1) tr1_2 = Tr(4, 2, 3.0, s3) expected_fst.add_tr(s2, tr1_2) tr1_3 = Tr(7, 3, 6.0, s4) expected_fst.add_tr(s2, tr1_3) tr1_4 = Tr(0, 5, None, s5) expected_fst.add_tr(s3, tr1_4) tr1_5 = Tr(0, 8, None, s5) expected_fst.add_tr(s4, tr1_5) config = DeterminizeConfig(DeterminizeType.DETERMINIZE_DISAMBIGUATE) det_fst = fst1.determinize(config) assert det_fst == expected_fst
def test_union(): # FST 1 fst1 = VectorFst() s1 = fst1.add_state() s2 = fst1.add_state() s3 = fst1.add_state() fst1.set_start(s1) fst1.set_final(s3) tr1_1 = Tr(1, 2, 1.0, s2) fst1.add_tr(s1, tr1_1) tr1_2 = Tr(3, 4, 2.0, s2) fst1.add_tr(s1, tr1_2) tr1_3 = Tr(4, 5, 3.0, s3) fst1.add_tr(s2, tr1_3) # FST 2 fst2 = VectorFst() s1 = fst2.add_state() s2 = fst2.add_state() fst2.set_start(s1) fst2.set_final(s2, 0.2) tr2_1 = Tr(1, 2, 1.0, s2) fst2.add_tr(s1, tr2_1) tr2_2 = Tr(3, 4, 2.5, s2) fst2.add_tr(s2, tr2_2) # Expected FST expected_fst = VectorFst() s1 = expected_fst.add_state() s2 = expected_fst.add_state() s3 = expected_fst.add_state() s4 = expected_fst.add_state() s5 = expected_fst.add_state() expected_fst.set_start(s1) expected_fst.set_final(s3) expected_fst.set_final(s5, 0.2) tr_1 = Tr(1, 2, 1.0, s2) expected_fst.add_tr(s1, tr_1) tr_2 = Tr(3, 4, 2.0, s2) expected_fst.add_tr(s1, tr_2) tr_3 = Tr(4, 5, 3.0, s3) expected_fst.add_tr(s2, tr_3) tr_4 = Tr(0, 0, None, s4) expected_fst.add_tr(s1, tr_4) tr_5 = Tr(1, 2, 1.0, s5) expected_fst.add_tr(s4, tr_5) tr_6 = Tr(3, 4, 2.5, s5) expected_fst.add_tr(s5, tr_6) union_fst = fst1.union(fst2) assert union_fst == expected_fst
def test_string_paths_iterator(): fst = VectorFst() s1 = fst.add_state() s2 = fst.add_state() fst.set_start(s1) fst.set_final(s2, 2.0) fst.add_tr(s1, Tr(1, 2, 2.0, s2)) fst.add_tr(s1, Tr(2, 3, 3.0, s2)) symt = SymbolTable() symt.add_symbol("a") symt.add_symbol("b") symt.add_symbol("c") fst.set_input_symbols(symt) fst.set_output_symbols(symt) string_paths_it = fst.string_paths() assert not string_paths_it.done() v1 = next(string_paths_it) assert v1.weight() == 4.0 assert v1.istring() == "a" assert v1.ostring() == "b" assert not string_paths_it.done() v2 = next(string_paths_it) assert v2.weight() == 5.0 assert v2.istring() == "b" assert v2.ostring() == "c" assert string_paths_it.done()
def test_tr_sort_olabel(): # FST 1 fst1 = VectorFst() s1 = fst1.add_state() s2 = fst1.add_state() fst1.set_start(s1) fst1.set_final(s2, 0.0) tr1_1 = Tr(1, 2, 1.0, s2) fst1.add_tr(s1, tr1_1) tr1_2 = Tr(3, 3, 2.0, s2) fst1.add_tr(s1, tr1_2) tr1_3 = Tr(1, 5, 3.0, s2) fst1.add_tr(s1, tr1_3) tr1_4 = Tr(2, 6, 4.0, s2) fst1.add_tr(s1, tr1_4) # Expected FST expected_fst = VectorFst() s1 = expected_fst.add_state() s2 = expected_fst.add_state() expected_fst.set_start(s1) expected_fst.set_final(s2, 0.0) tr_1 = Tr(1, 2, 1.0, s2) expected_fst.add_tr(s1, tr_1) tr_2 = Tr(3, 3, 2.0, s2) expected_fst.add_tr(s1, tr_2) tr_4 = Tr(1, 5, 3.0, s2) expected_fst.add_tr(s1, tr_4) tr_5 = Tr(2, 6, 4.0, s2) expected_fst.add_tr(s1, tr_5) fst1.tr_sort(ilabel_cmp=False) assert fst1 == expected_fst
def test_compose_fst(): # FST 1 fst1 = VectorFst() s1 = fst1.add_state() s2 = fst1.add_state() s3 = fst1.add_state() fst1.set_start(s1) fst1.set_final(s2) fst1.set_final(s3) tr1_1 = Tr(1, 2, 1.0, s2) fst1.add_tr(s1, tr1_1) tr1_2 = Tr(1, 4, 2.0, s3) fst1.add_tr(s1, tr1_2) tr1_3 = Tr(3, 5, 2.0, s2) fst1.add_tr(s2, tr1_3) # FST 2 fst2 = VectorFst() s1 = fst2.add_state() s2 = fst2.add_state() s3 = fst2.add_state() fst2.set_start(s1) fst2.set_final(s3) tr2_1 = Tr(2, 6, 1.0, s2) fst2.add_tr(s1, tr2_1) tr2_2 = Tr(5, 7, 2.5, s3) fst2.add_tr(s2, tr2_2) tr2_3 = Tr(5, 8, 1.5, s3) fst2.add_tr(s3, tr2_3) tr2_4 = Tr(4, 9, 3.0, s3) fst2.add_tr(s1, tr2_4) # Expected FST expected_fst = VectorFst() s1 = expected_fst.add_state() s2 = expected_fst.add_state() s3 = expected_fst.add_state() s4 = expected_fst.add_state() expected_fst.set_start(s1) expected_fst.set_final(s3) expected_fst.set_final(s4) tr3_1 = Tr(1, 6, 2.0, s2) expected_fst.add_tr(s1, tr3_1) tr3_2 = Tr(1, 9, 5.0, s3) expected_fst.add_tr(s1, tr3_2) tr3_3 = Tr(3, 7, 4.5, s4) expected_fst.add_tr(s2, tr3_3) tr3_4 = Tr(3, 8, 3.5, s4) expected_fst.add_tr(s4, tr3_4) fst3 = fst1.compose(fst2) assert fst3 == expected_fst