pset6相似性-索引范围和编辑距离

时间:2018-12-19 19:26:30

标签: python

我收到有关Pset6相似性的以下错误(更多):

:) helpers.py存在

:) helpers.py编译

:)进行0操作将“”转换为“”

:((需要3个操作,才能将“ dog”转换为“”     列出索引超出范围

:((需执行4次运算才能将“”转换为“ dog”     列表分配索引超出范围

:)只需执行一次操作即可将“ a”转换为“ b”

:((需执行1次操作才能将“猫”转换为“外套”     列出索引超出范围

:((需执行1次操作才能将“青蛙”转换为“雾”     列出索引超出范围

:((进行1次运算即可将“年”转换为“梨”     预期的编辑距离为1,而不是0

:)需要进行0次运算才能将“今天”转换为“今天”

:((需要5个操作才能将“今天”转换为“昨天”     列出索引超出范围

:((需要6次操作才能将“明天”转换为“今天”     列出索引超出范围

:((需要3个操作才能将“今天”转换为“ ToDaY”     预期的编辑距离为3,而不是1

这是我的helpers.py代码:

def distances(a, b):
    """Calculate edit distance from a to b"""

    # TODO

    # Set up 2D list
    i = len(a)
    j = len(b)

    x = i + 1
    y = j + 1

    # Add values for base cases (first row and first column) |top left = [0][0] bottom right = [len(a)][len(b)] = [i][j]
    matrix = [[(0, None) for i in range(x)] for j in range(y)]

    for i in range(1, x):
        if i != 0 and j == 0:
            matrix[i][0] = (i, Operation.DELETED)

    for j in range(1, y):
        if i == 0 and j != 0:
            matrix[0][j] = (j, Operation.INSERTED)

    for i in range(1, x):
        for j in range(1, y):
            if a[i - 1] == b[j - 1]:
                matrix[0][0] = (0, None)

    # Fill in the other entries in the table
    for i in range(1, x):
        for j in range(1, y):
            if a[i - 1] == b[j - 1]:
                none, _ = matrix[i][j]
            else:
                deletion, _ = matrix[i - 1][j]
                insertion, _ = matrix[i][j - 1]
                substitution, _ = matrix[i - 1][j - 1]

                if a[i - 1] == b[j - 1]:
                        matrix[0][0] = (0, None)
                else:
                    if deletion <= insertion and deletion <= substitution:
                        matrix[i][j] = (deletion + 1, Operation.DELETED)
                    if insertion <= deletion and insertion <= substitution:
                        matrix[i][j] = (insertion + 1, Operation.INSERTED)
                    if substitution <= deletion and substitution <= insertion:
                        matrix[i][j] = (substitution + 1, Operation.SUBSTITUTED)

    # return[[]]
    return matrix

0 个答案:

没有答案