Ejemplo n.º 1
0
 def testBeamsearchRhoEps(self):
     fst1 = mkfst(3, [(0, 1, 1001, 1.0), (1, 2, L_EPSILON, 2.0),
                      (2, 3, 1003, 3.0)])
     fst2 = mkfst(3, [(0, 1, 1001, 10.0), (1, 2, L_RHO, 20.0),
                      (2, 3, 1003, 30.0)])
     (v1, v2, ins, outs, costs) = ocrolib.beam_search(fst1, fst2, 1000)
     assert EQ(costs, [0.0, Inf]), costs
Ejemplo n.º 2
0
 def testBeamsearchLabels(self):
     fst1 = mkfst(3, [(0, 1, 1001, 1.0, 91), (1, 2, 1002, 2.0, 92),
                      (2, 3, 1003, 3.0, 93)])
     fst2 = mkfst(3, [(0, 1, 101, 10.0, 1001), (1, 2, 102, 20.0, 1002),
                      (2, 3, 103, 30.0, 1003)])
     (v1, v2, ins, outs, costs) = ocrolib.beam_search(fst1, fst2, 1000)
     assert EQ(costs, [0.0, 11.0, 22.0, 33.0, 0.0]), costs
     assert EQ(v1, [0, 1, 2, 3]), v1
     assert EQ(v2, [0, 1, 2, 3]), v2
     assert EQ(ins, [0, 91, 92, 93]), ins
     assert EQ(outs, [0, 101, 102, 103]), outs
Ejemplo n.º 3
0
 def testBeamsearchPhiIsFail(self):
     fst1 = mkfst(1, [(0, 1, 1001, 0.0)])
     # make sure it takes the 0->2 transition only if everything else failed
     fst2 = mkfst([1, 3], [(0, 1, 1001, 100.0), (0, 2, L_PHI, 0.0),
                           (2, 3, 1001, 0.0)])
     (v1, v2, ins, outs, costs) = ocrolib.beam_search(fst1, fst2, 1000)
     assert EQ(costs, [0.0, 100.0, 0.0]), costs
     assert EQ(v1, [0, 1]), v1
     assert EQ(v2, [0, 1]), v2
     assert EQ(ins, [0, 1001]), ins
     assert EQ(outs, [0, 1001]), outs
Ejemplo n.º 4
0
 def testBeamsearchRhoIsRest2(self):
     fst1 = mkfst(3, [(0, 1, 1001, 1.0), (1, 2, 1002, 2.0),
                      (2, 3, 1003, 3.0)])
     fst2 = mkfst(3, [(0, 1, 1001, 10.0), (1, 2, 9999, 20.0),
                      (1, 2, L_RHO, 200.0), (2, 3, 1003, 30.0)])
     (v1, v2, ins, outs, costs) = ocrolib.beam_search(fst1, fst2, 1000)
     assert EQ(costs, [0.0, 11.0, 202.0, 33.0, 0.0]), costs
     assert EQ(v1, [0, 1, 2, 3]), v1
     assert EQ(v2, [0, 1, 2, 3]), v2
     assert EQ(ins, [0, 1001, 1002, 1003]), ins
     assert EQ(outs, [0, 1001, 1002, 1003]), outs
Ejemplo n.º 5
0
 def testBeamsearchEps(self):
     fst1 = mkfst(4, [(0, 1, 1001, 1.0), (1, 2, 1002, 2.0),
                      (2, 3, L_EPSILON, 3.0), (3, 4, 1003, 4.0)])
     fst2 = mkfst(3, [(0, 1, 1001, 10.0), (1, 2, 1002, 20.0),
                      (2, 3, 1003, 30.0)])
     result = ocrolib.beam_search(fst1, fst2, 1000)
     (v1, v2, ins, outs, costs) = result
     assert EQ(costs, [0.0, 11.0, 22.0, 3.0, 34.0, 0.0]), costs
     assert EQ(v1, [0, 1, 2, 3, 4]), v1
     assert EQ(v2, [0, 1, 2, 2, 3]), v2
     assert EQ(ins, [0, 1001, 1002, 0, 1003]), ins
     assert EQ(outs, [0, 1001, 1002, 0, 1003]), outs
Ejemplo n.º 6
0
 def testBeamsearchPhi(self):
     fst1 = mkfst(1, [(0, 1, 1001, 1.0)])
     # L_PHI should behave like L_EPSILON on failure to match, so we test both
     fst2s = [("with epsilon",
               mkfst(3, [(0, 1, 9999, 0.0), (0, 2, L_EPSILON, 0.0),
                         (2, 3, 1001, 0.0)])),
              ("with phi",
               mkfst(3, [(0, 1, 9999, 0.0), (0, 2, L_PHI, 0.0),
                         (2, 3, 1001, 0.0)]))]
     for key, fst2 in fst2s:
         (v1, v2, ins, outs, costs) = ocrolib.beam_search(fst1, fst2, 1000)
         assert EQ(costs, [0.0, 0.0, 1.0, 0.0]), "%s: %s" % (key, costs)
         assert EQ(v1, [0, 0, 1]), v1
         assert EQ(v2, [0, 2, 3]), v2
         assert EQ(ins, [0, 0, 1001]), ins
         assert EQ(outs, [0, 0, 1001]), outs
Ejemplo n.º 7
0
 def testBeamsearchRho(self):
     fst1 = mkfst(3, [(0, 1, 1001, 1.0), (1, 2, 1002, 2.0),
                      (2, 3, 1003, 3.0)])
     # L_RHO should behave like the matching symbol, so we test both
     fst2s = [("with 1002",
               mkfst(3, [(0, 1, 1001, 10.0), (1, 2, 1002, 20.0, 1002),
                         (2, 3, 1003, 30.0)])),
              ("with rho",
               mkfst(3, [(0, 1, 1001, 10.0), (1, 2, 1002, 20.0, L_RHO),
                         (2, 3, 1003, 30.0)]))]
     for key, fst2 in fst2s:
         (v1, v2, ins, outs, costs) = ocrolib.beam_search(fst1, fst2, 1000)
         assert EQ(costs, [0.0, 11.0, 22.0, 33.0, 0.0]), costs
         assert EQ(v1, [0, 1, 2, 3]), v1
         assert EQ(v2, [0, 1, 2, 3]), v2
         assert EQ(ins, [0, 1001, 1002, 1003]), ins
         assert EQ(outs, [0, 1001, 1002, 1003]), outs