예제 #1
0
def tonal_space_local_alignment(sem1, sem2):
    """
    Like L{tonal_space_alignment}, but uses local alignment of seq2 within seq1.
    
    Also returns the distance metric (like L{tonal_space_local_distance}.
    
    """
    from jazzparser.utils.distance import local_levenshtein_distance
    # Get a list of (coord,fun) pairs for the logical forms
    seq1 = _lf_to_coord_funs(sem1)
    seq2 = _lf_to_coord_funs(sem2)
    # Produce a version of the paths made up of steps and functions, 
    #  rather than points and functions
    steps1 = _steps_list(seq1)
    steps2 = _steps_list(seq2)
    
    dists,pointers = local_levenshtein_distance(
                                steps1, 
                                steps2,
                                delins_cost=2,
                                subst_cost_fun=_subst_cost)
    
    # We now have the matrix of costs and the pointers that generated 
    #  those costs.
    # Trace back to find out what produces the optimal alignment
    # Start at the top right corner
    i,j = (len(dists)-1), (len(dists[0])-1)
    oplist = []
    while not i == j == 0:
        if pointers[i][j] == "I":
            oplist.append("I")
            j -= 1
        elif pointers[i][j] == "D":
            oplist.append("D")
            i -= 1
        elif pointers[i][j] == ".":
            oplist.append(".")
            i -= 1
        else:
            # Substitution: find out what kind
            step1 = steps1[i-1]
            step2 = steps2[j-1]
            subst_type = _subst_type(step1, step2)
            if subst_type == "both":
                oplist.append("S")
            elif subst_type == "fun":
                oplist.append("Sf")
            elif subst_type == "root":
                oplist.append("Sr")
            else:
                oplist.append("A")
            j -= 1
            i -= 1
    
    oplist = list(reversed(oplist))
    distance = float(dists[-1][-1]) / 2.0
    return oplist, steps1, steps2, distance
예제 #2
0
def tonal_space_local_alignment(sem1, sem2):
    """
    Like L{tonal_space_alignment}, but uses local alignment of seq2 within seq1.
    
    Also returns the distance metric (like L{tonal_space_local_distance}.
    
    """
    from jazzparser.utils.distance import local_levenshtein_distance
    # Get a list of (coord,fun) pairs for the logical forms
    seq1 = _lf_to_coord_funs(sem1)
    seq2 = _lf_to_coord_funs(sem2)
    # Produce a version of the paths made up of steps and functions,
    #  rather than points and functions
    steps1 = _steps_list(seq1)
    steps2 = _steps_list(seq2)

    dists, pointers = local_levenshtein_distance(steps1,
                                                 steps2,
                                                 delins_cost=2,
                                                 subst_cost_fun=_subst_cost)

    # We now have the matrix of costs and the pointers that generated
    #  those costs.
    # Trace back to find out what produces the optimal alignment
    # Start at the top right corner
    i, j = (len(dists) - 1), (len(dists[0]) - 1)
    oplist = []
    while not i == j == 0:
        if pointers[i][j] == "I":
            oplist.append("I")
            j -= 1
        elif pointers[i][j] == "D":
            oplist.append("D")
            i -= 1
        elif pointers[i][j] == ".":
            oplist.append(".")
            i -= 1
        else:
            # Substitution: find out what kind
            step1 = steps1[i - 1]
            step2 = steps2[j - 1]
            subst_type = _subst_type(step1, step2)
            if subst_type == "both":
                oplist.append("S")
            elif subst_type == "fun":
                oplist.append("Sf")
            elif subst_type == "root":
                oplist.append("Sr")
            else:
                oplist.append("A")
            j -= 1
            i -= 1

    oplist = list(reversed(oplist))
    distance = float(dists[-1][-1]) / 2.0
    return oplist, steps1, steps2, distance
예제 #3
0
def tonal_space_local_distance(sem1, sem2):
    """
    Like L{tonal_space_distance}, but uses local alignment of seq2 within 
    seq1.
    
    """
    from jazzparser.utils.distance import local_levenshtein_distance
    # Get a list of (coord,fun) pairs for the logical forms
    seq1 = _lf_to_coord_funs(sem1)
    seq2 = _lf_to_coord_funs(sem2)
    # Produce a version of the paths made up of steps and functions,
    #  rather than points and functions
    steps1 = _steps_list(seq1)
    steps2 = _steps_list(seq2)

    dists, pointers = local_levenshtein_distance(steps1,
                                                 steps2,
                                                 delins_cost=2,
                                                 subst_cost_fun=_subst_cost)
    return float(dists[-1][-1]) / 2.0
예제 #4
0
def tonal_space_local_distance(sem1, sem2):
    """
    Like L{tonal_space_distance}, but uses local alignment of seq2 within 
    seq1.
    
    """
    from jazzparser.utils.distance import local_levenshtein_distance
    # Get a list of (coord,fun) pairs for the logical forms
    seq1 = _lf_to_coord_funs(sem1)
    seq2 = _lf_to_coord_funs(sem2)
    # Produce a version of the paths made up of steps and functions, 
    #  rather than points and functions
    steps1 = _steps_list(seq1)
    steps2 = _steps_list(seq2)
    
    dists,pointers = local_levenshtein_distance(
                                steps1, 
                                steps2,
                                delins_cost=2,
                                subst_cost_fun=_subst_cost)
    return float(dists[-1][-1]) / 2.0