Example #1
0
def transition(n, profil, pos, withTransitions):
    # This is the transition function as described
    # by table 4.1
    #
    # Note that the current profil is the state's
    # characteristic vector (CV), not the current
    # position's CV. It means that position's CV
    # is in fact:
    #
    #  profil[pos.i:min(n - e + 1, w - i)]
    #
    # (See Definition 4.0.22)
    i = pos.i
    e = pos.e
    w = len(profil)

    positions = []
    if pos.isTransposition:
        # if we are a transposition position, the only
        # accepting position if profil is [0, 1]
        if profil[i] == 1:
            return [fsc.StandardPosition(i + 2, e)]
        else:
            return positions

    if withTransitions and (w - i) >= 2 and profil[i:i + 2] == [0, 1]:
        positions += [fsc.TPosition(i, e + 1)]

    # Return directly if this is a match.
    if i < w and profil[i] is 1:
        return [fsc.StandardPosition(i + 1, e)]

    # Basic positions: deletion, subsitution
    positions += [
        fsc.StandardPosition(i, e + 1),
        fsc.StandardPosition(i + 1, e + 1)
    ]

    # Addition operation:
    if i < w:
        k = fsc.positiveK(profil[i:i + min(n - e + 1, len(profil) - i)])
        if k is not None:
            positions.append(fsc.StandardPosition(i + k, e + k - 1))

    # remove positions that goes beyong profil.
    positions = [s for s in positions if s.i <= w]
    # remove positions that goes beyong allowed edit mistakes
    positions = [s for s in positions if s.e <= n]
    return positions
Example #2
0
 def testNotSubsumming(self):
     """
     Test if the subsumming test is okay.
     """
     areSubsummed = [[(1, 0), (1, 0)], [(1, 0), (0, 0)], [(1, 0), (2, 0)],
                     [(1, 0), (3, 0)], [(1, 0), (3, 1)], [(1, 0), (4, 0)],
                     [(1, 0), (4, 1)], [(1, 0), (4, 2)], [(1, 0), (5, 0)],
                     [(1, 0), (5, 1)], [(1, 0), (5, 2)], [(1, 0), (5, 3)],
                     [(1, 0), (6, 0)], [(1, 0), (6, 1)], [(1, 0), (6, 2)],
                     [(1, 0), (6, 3)], [(1, 0), (6, 4)]]
     n = 1
     for entry in areSubsummed:
         errorMsg = "The entry " + str(entry[0]) + " is not supposed to subsume " +\
                    "the entry " + str(entry[1]) + " but it is"
         self.assertTrue(not fsc.isSubsumming(fsc.StandardPosition(entry[0][0], entry[0][1]), \
                                           fsc.StandardPosition(entry[1][0], entry[1][1]), n), msg = errorMsg)
Example #3
0
 def testSubsumming(self):
     """
     Test if the subsumming test is okay.
     """
     areSubsummed = [[(1, 0), (0, 1)], [(1, 0), (1, 1)], [(1, 0), (2, 1)],
                     [(1, 0), (0, 2)], [(1, 0), (1, 2)], [(1, 0), (2, 2)],
                     [(1, 0), (3, 2)], [(1, 0), (0, 3)], [(1, 0), (1, 3)],
                     [(1, 0), (2, 3)], [(1, 0), (3, 3)], [(1, 0), (4, 3)],
                     [(2, 0), (1, 1)], [(2, 0), (2, 1)], [(2, 0), (3, 1)],
                     [(2, 0), (0, 2)], [(2, 0), (1, 2)], [(2, 0), (2, 2)],
                     [(2, 0), (3, 2)], [(2, 0), (4, 2)], [(2, 0), (0, 3)],
                     [(2, 0), (1, 3)], [(2, 0), (2, 3)], [(2, 0), (3, 3)],
                     [(2, 0), (4, 3)], [(2, 0), (5, 3)]]
     n = 1
     for entry in areSubsummed:
         errorMsg = "The entry " + str(entry[0]) + " is supposed to subsume " +\
                    "the entry " + str(entry[1]) + " but it isn't"
         self.assert_(fsc.isSubsumming(fsc.StandardPosition(entry[0][0], entry[0][1]), \
                                       fsc.StandardPosition(entry[1][0], entry[1][1]), n), msg = errorMsg)
Example #4
0
def genTransitions(n, withTransitions = True):
    allProfils = genAllProfilPowerSet(n)
    transitions = [{} for i in range(2 * n + 2)]
    
    # Add the initial state
    processedStates = []
    unprocessedStates = [[fsc.StandardPosition(0,0)]]
    while len(unprocessedStates) > 0:
        state = unprocessedStates.pop()
        processedStates.append(state)

        profilLenMin, profilLenMax = determineRelevantSubwordLenghts(n, state)
        for profilLen in range(profilLenMin, profilLenMax + 1):
            for profil in allProfils[profilLen]:
                # for the current characteristic vector check what
                # is the next state. 
                nextState, difference = getNextState(n, profil, state, withTransitions)
                transitions[profilLen].setdefault(str(profil), {}).setdefault(str(state), (nextState, difference))
                if nextState != [] and not nextState in processedStates and not nextState in unprocessedStates:
                    unprocessedStates.append(nextState)
                
    return transitions