def test_compute_lattice_expected_counts_one_arc(): # 0 --0-> 1, -1 expected = [0.] actual = compute_lattice_expected_counts(TokenLattice(arcList=[ Arc(src=0, dst=1, token=Token(tokenIndex=0), weight=-1.), ], startState=0, endState=1)) assert allclose(expected, actual), '%s !~= %s' % (expected, actual)
def test_compute_lattice_expected_counts_two_serial_arcs(): # 0 --0-> 1, -1 # 1 --1-> 2, -3 expected = [0., 0.] actual = compute_lattice_expected_counts(TokenLattice(arcList=[ Arc(src=0, dst=1, token=Token(tokenIndex=0), weight=-1.), Arc(src=1, dst=2, token=Token(tokenIndex=1), weight=-3.), ], startState=0, endState=2)) assert allclose(expected, actual), '%s !~= %s' % (expected, actual)
def test_compute_lattice_expected_counts_triangle_arbitrary_states(): # 47 --0-> 9, -1 # 9 --1-> 3, -2 # 47 --1-> 3, -4 A = log(exp(-1 + -2) + exp(-4)) expected = [-1 + -2 - A, 0.] actual = compute_lattice_expected_counts(TokenLattice(arcList=[ Arc(src=47, dst=9, token=Token(tokenIndex=0), weight=-1.), Arc(src=9, dst=3, token=Token(tokenIndex=1), weight=-2.), Arc(src=47, dst=3, token=Token(tokenIndex=1), weight=-4.), ], startState=47, endState=3)) assert allclose(expected, actual), '%s !~= %s' % (expected, actual)
def test_compute_lattice_expected_counts_triangle(): # 0 --0-> 1, -1 # 1 --1-> 2, -2 # 0 --1-> 2, -4 A = log(exp(-1 + -2) + exp(-4)) expected = [-1 + -2 - A, 0.] actual = compute_lattice_expected_counts(TokenLattice(arcList=[ Arc(src=0, dst=1, token=Token(tokenIndex=0), weight=-1.), Arc(src=1, dst=2, token=Token(tokenIndex=1), weight=-2.), Arc(src=0, dst=2, token=Token(tokenIndex=1), weight=-4.), ], startState=0, endState=2)) assert allclose(expected, actual), '%s !~= %s' % (expected, actual)
def test_compute_lattice_expected_counts_rhombus(): # 0 --0-> 1, -1 # 1 --1-> 3, -2 # 0 --0-> 2, -3 # 2 --2-> 3, -4 A = log(exp(-1 + -2) + exp(-3 + -4)) expected = [0., -1 + -2 - A, -3 + -4 - A] actual = compute_lattice_expected_counts(TokenLattice(arcList=[ Arc(src=0, dst=1, token=Token(tokenIndex=0), weight=-1.), Arc(src=1, dst=3, token=Token(tokenIndex=1), weight=-2.), Arc(src=0, dst=2, token=Token(tokenIndex=0), weight=-3.), Arc(src=2, dst=3, token=Token(tokenIndex=2), weight=-4.), ], startState=0, endState=3)) assert allclose(expected, actual), '%s !~= %s' % (expected, actual)
def test_compute_lattice_expected_counts_incomplete_arc(): # 0 --0-> 1, -1 with raises(ValueError): compute_lattice_expected_counts(TokenLattice(arcList=[ Arc(dst=1, token=Token(tokenIndex=0), weight=-1.), ], startState=0, endState=1)) with raises(ValueError): compute_lattice_expected_counts(TokenLattice(arcList=[ Arc(src=0, token=Token(tokenIndex=0), weight=-1.), ], startState=0, endState=1)) with raises(ValueError): compute_lattice_expected_counts(TokenLattice(arcList=[ Arc(src=0, dst=1, weight=-1.), ], startState=0, endState=1)) with raises(ValueError): compute_lattice_expected_counts(TokenLattice(arcList=[ Arc(src=0, dst=1, token=Token(tokenIndex=0)), ], startState=0, endState=1))
def test_compute_lattice_expected_counts_empty(): assert compute_lattice_expected_counts(TokenLattice(arcList=[ ], startState=0, endState=0)) == []