示例#1
0
def test_small_fst():
    fst = VectorFst()

    # States
    s1 = fst.add_state()
    s2 = fst.add_state()

    assert s1 == 0
    assert s2 == 1

    fst.set_start(s1)
    fst.set_final(s2)
    assert fst.start() == s1
    assert fst.is_final(s2)
    assert pytest.approx(fst.final(s2)) == pytest.approx(0.0)

    # Trs
    tr_1 = Tr(3, 5, 10.0, s2)
    fst.add_tr(s1, tr_1)

    assert fst.num_trs(s1) == 1

    tr_2 = Tr(5, 7, 18.0, s2)
    fst.add_tr(s1, tr_2)
    assert fst.num_trs(s1) == 2
示例#2
0
def test_fst_symt():
    fst = VectorFst()
    s1 = fst.add_state()
    s2 = fst.add_state()
    fst.set_start(s1)
    fst.set_final(s2, 1.0)

    tr_1 = Tr(1, 0, 10.0, s2)
    tr_2 = Tr(2, 0, 1.0, s1)
    tr_3 = Tr(3, 0, 1.0, s2)
    fst.add_tr(s1, tr_1)
    fst.add_tr(s2, tr_2)
    fst.add_tr(s2, tr_3)

    input_symt = SymbolTable()
    input_symt.add_symbol("a")
    input_symt.add_symbol("b")
    input_symt.add_symbol("c")

    fst.set_input_symbols(input_symt)
    fst_in_symbols = fst.input_symbols()

    assert input_symt == fst_in_symbols
    assert fst_in_symbols.num_symbols() == 4
    assert fst_in_symbols.find("a") == 1
    assert fst_in_symbols.find("b") == 2
    assert fst_in_symbols.find("c") == 3

    output_symt = SymbolTable()
    fst.set_output_symbols(output_symt)
    fst_out_symbols = fst.output_symbols()

    assert output_symt == fst_out_symbols
    assert fst_out_symbols.num_symbols() == 1
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()
示例#4
0
def test_isomorphic_2():
    # FST 1
    fst1 = VectorFst()

    s1 = fst1.add_state()
    s2 = fst1.add_state()

    fst1.set_start(s1)
    fst1.set_final(s2)

    tr1_1 = Tr(12, 25, None, s2)
    fst1.add_tr(s1, tr1_1)

    # FST 2
    fst2 = VectorFst()
    s1 = fst2.add_state()
    s2 = fst2.add_state()

    fst2.set_start(s2)
    fst2.set_final(s1)

    tr1_1 = Tr(12, 25, None, s1)
    fst2.add_tr(s2, tr1_1)

    assert fst1.isomorphic(fst2)
示例#5
0
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
示例#6
0
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
示例#7
0
def test_fst_relabel_tables():
    fst = VectorFst()
    s1 = fst.add_state()
    s2 = fst.add_state()
    fst.add_tr(s1, Tr(1, 2, weight_one(), s2))
    fst.set_start(s1)
    fst.set_final(s2)

    old_isymt = SymbolTable.from_symbols(["a", "b"])
    new_isymt = SymbolTable.from_symbols(["b", "a"])

    old_osymt = SymbolTable.from_symbols(["aa", "bb"])
    new_osymt = SymbolTable.from_symbols(["bb", "aa"])

    fst_ref = VectorFst()
    s1 = fst_ref.add_state()
    s2 = fst_ref.add_state()
    fst_ref.add_tr(s1, Tr(2, 1, weight_one(), s2))
    fst_ref.set_start(s1)
    fst_ref.set_final(s2)

    fst_1 = fst.copy()
    fst_1.relabel_tables(
        old_isymbols=old_isymt,
        new_isymbols=new_isymt,
        attach_new_isymbols=True,
        old_osymbols=old_osymt,
        new_osymbols=new_osymt,
        attach_new_osymbols=True,
    )
    assert fst_1 == fst_ref
    assert fst_1.input_symbols() == new_isymt
    assert fst_1.output_symbols() == new_osymt

    fst_2 = fst.copy()
    fst_2.relabel_tables(
        old_isymbols=old_isymt,
        new_isymbols=new_isymt,
        attach_new_isymbols=False,
        old_osymbols=old_osymt,
        new_osymbols=new_osymt,
        attach_new_osymbols=False,
    )
    assert fst_2 == fst_ref
    assert fst_2.input_symbols() is None
    assert fst_2.output_symbols() is None

    fst_3 = fst.copy()
    fst_3.set_input_symbols(old_isymt)
    fst_3.set_output_symbols(old_osymt)
    fst_3.relabel_tables(
        new_isymbols=new_isymt,
        new_osymbols=new_osymt,
    )
    assert fst_3 == fst_ref
    assert fst_3.input_symbols() == new_isymt
    assert fst_3.output_symbols() == new_osymt
示例#8
0
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
示例#9
0
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
示例#10
0
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
示例#11
0
def test_trs():
    a = Tr(1, 1, 1.0, 2)
    b = Tr(2, 2, 2.0, 3)
    c = Tr(3, 3, 3.0, 4)

    trs = Trs()
    trs.push(a)
    trs.push(b)
    trs.push(c)

    assert trs.len() == 3

    assert trs.remove(2) == c
    assert trs.remove(1) == b

    assert trs.len() == 1
示例#12
0
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
示例#13
0
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}
示例#14
0
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
示例#15
0
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
示例#16
0
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
示例#17
0
def test_fst_read_write_with_symt():
    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)

    input_symt = SymbolTable()
    input_symt.add_symbol("a")
    input_symt.add_symbol("b")
    input_symt.add_symbol("c")
    fst.set_input_symbols(input_symt)

    output_symt = SymbolTable()
    fst.set_output_symbols(output_symt)

    fst.write("/tmp/test.fst")

    read_fst = VectorFst.read("/tmp/test.fst")

    assert read_fst.input_symbols().num_symbols() == 4
    assert read_fst.input_symbols().find("a") == 1
    assert read_fst.input_symbols().find("b") == 2
    assert read_fst.input_symbols().find("c") == 3

    assert read_fst.output_symbols().num_symbols() == 1

    assert fst == read_fst
示例#18
0
def test_fst_trs_iterator():
    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)

    trs = [tr_1, tr_2]

    num_trs = fst.num_trs(s1)
    idx = 0
    for i, tr in enumerate(fst.trs(s1)):
        idx += 1
        assert tr == trs[i]

    assert num_trs == idx
示例#19
0
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
示例#20
0
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
示例#21
0
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
示例#22
0
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()
示例#23
0
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
示例#24
0
def test_tr():
    a = Tr(1, 1, 1.0, 2)

    assert a.ilabel == 1
    assert a.olabel == 1
    assert pytest.approx(a.weight) == pytest.approx(1.0)
    assert a.next_state == 2

    a.ilabel = 2
    a.olabel = 3
    a.weight = 4.0
    a.next_state = 5

    assert a.ilabel == 2
    assert a.olabel == 3
    assert pytest.approx(a.weight) == pytest.approx(4.0)
    assert a.next_state == 5
示例#25
0
def test_project_output():
    # 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)

    # 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_1 = Tr(2, 2, 1.0, s2)
    expected_fst.add_tr(s1, tr1_1)

    tr1_2 = Tr(4, 4, 2.0, s2)
    expected_fst.add_tr(s1, tr1_2)

    tr1_3 = Tr(5, 5, 3.0, s3)
    expected_fst.add_tr(s2, tr1_3)

    project_type = ProjectType.PROJECT_OUTPUT
    fst2 = fst1.project(project_type)

    assert expected_fst == fst2
示例#26
0
def test_project_input():
    # 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)

    # 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_1 = Tr(1, 1, 1.0, s2)
    expected_fst.add_tr(s1, tr1_1)

    tr1_2 = Tr(3, 3, 2.0, s2)
    expected_fst.add_tr(s1, tr1_2)

    tr1_3 = Tr(4, 4, 3.0, s3)
    expected_fst.add_tr(s2, tr1_3)

    fst2 = fst1.project()

    assert expected_fst == fst2
示例#27
0
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
示例#28
0
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
示例#29
0
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
示例#30
0
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