PyCharm声明结束

时间:2018-09-24 19:31:10

标签: python python-3.x pycharm edit-distance

我正在运行PyCharm版本3.6.6(已通过sys.version命令检查),解释器为3.6。问题出在所有打印功能上。这是老师给我们提供的用于编辑距离算法的代码。我们假设只是运行此代码,这样它就可以工作...请查看附件图片以查看错误。

def edit_distance(s1, s2):
m = len(s1) + 1
n = len(s2) + 1

deletes = 1
inserts = 1
substitute = 1

# Initialize the matrix
mTable = {}
for i in range(0, m):
    for j in range(0, n):
        mTable[i, j] = 0

for i in range(0, m):
    # Need to add one line here
    mTable[i,0] = i * deletes

for j in range(0, n):
    # Need to add one line here
    mTable[0, j] = j * inserts

cost = 0

for i in range(1, m):
    for j in range(1, n):
       if(s1[i-1] == s2[j-1]):
           cost = 0
       else:
            mTable[i,j]=min(mTable[i-1,j] + deletes,
            mTable[i, j - 1] + inserts,  # (i,j-1) with insert cost
            mTable[i - 1, j - 1] + cost  # (i-1,j-1) with if match cost=0 else cost=substitude

# Print the edit distance matrix
print("Edit Distance Matrix\n")
print("      ", end= ' ')
for j in range(n-1):
    print("| " + s2[j] + " ", end='')
print("\n")
for i in range(0, m):
    if i == 0:
        print("   ", end='')
    if i > 0:
        print(" " + s1[i - 1] + " ", end='')
    for j in range(0, n):
        print("| " + str(mTable[i, j]) + " ", end='')
    print("\n")

return mTable, mTable[m - 1, n - 1]

def get_edits(s1, s2, mTable, nEditDist):
m = len(s1) + 1
n = len(s2) + 1

i_old = m - 1
j_old = n - 1
i_new = m - 1
j_new = n - 1
sOperation = ""
nIndexOfOperation = nEditDist - 1
sOperationList = {}
for i in range(0, nEditDist - 1):
    sOperationList[i] = ""
while 1:
    nLeft = mTable[i_old, j_old-1]
    nUp   = mTable[i_old-1, j_old]
    nUpLeft = mTable[i_old-1, j_old-1]
    if nUpLeft <= nLeft and nUpLeft <= nUp:
        i_new = i_old - 1
        j_new = j_old - 1
        if mTable[i_old, j_old] > nUpLeft:
            sOperation = "Substitute"                     
            sOperationList[nIndexOfOperation] = sOperation
            nIndexOfOperation -= 1
    elif nLeft <= nUpLeft and nLeft <= nUp:
        i_new = i_old
        j_new = j_old - 1
        if mTable[i_old, j_old] > nLeft:
            sOperation = "Insert"                     
            sOperationList[nIndexOfOperation] = sOperation
            nIndexOfOperation -= 1
    elif nUp <= nUpLeft and nUp <= nLeft:
        i_new = i_old - 1
        j_new = j_old
        if mTable[i_old, j_old] > nUp:
            sOperation = "Delete"                  
            sOperationList[nIndexOfOperation] = sOperation
            nIndexOfOperation -= 1
    i_old = i_new
    j_old = j_new
    if i_old == 0 and j_old == 0:
        break

print("the sequence of the edits:")
for i in range(0, nEditDist):
    print("Step " + str(i + 1) + " : " + sOperationList[i])


if __name__ == "__main__":
# Example 1
sString1 = "kitten"
sString2 = "sitting"
# Example 2
# sString1 = "GAMBOL"
# sString2 = "GUMBO"
mTable, nEditDist = edit_distance(sString1, sString2)
print("Edit distance is " + str(nEditDist))
get_edits(sString1, sString2, mTable, nEditDist)

下面是错误的图片:

enter image description here

我尝试在顶部添加“来自未来 import print_function”,但该错误不会消失。

1 个答案:

答案 0 :(得分:1)

您似乎在代码开头缺少结尾括号:

mTable[i, j] = min(...
    ...
    ... ) <-- on the third line here