0

While performing testing of my web application, upon submitting the form and going to /score, none of the actual webpage is displayed, only the text:

Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

From careful inspection of the many exceptions that have been raised by Flask, this one seems to come to my attention the most:

File "/home/ubuntu/workspace/pset6/similarities/more/similarities/application.py", line 45, in score _, operation = matrix[i][j] IndexError: list index out of range

Since this error came from application.py, it's difficult for me to figure out where things went wrong. I ran a few tests on my helpers.py through score to see if that was where the issue was, yet that script raised no issues, meaning that my code indeed works. Even further investigation shows that matrix is of type list, and is made up of tuples, except for the first element, which is supposed to have no operation, but I gave it an Operation.SUBSTITUTED anyway to prevent a TypeError exception.

Here is my code for helpers.py:

from enum import Enum


class Operation(Enum):
    """Operations"""

    DELETED = 1
    INSERTED = 2
    SUBSTITUTED = 3

    def __str__(self):
        return str(self.name.lower())


def distances(a, b):
    """
    Calculate edit distance from a to b
    Modified from Bernd Klein's code on https://www.python-course.eu/levenshtein_distance.php
    """

    # Declaration of matrix dimensions
    rows = len(a) + 1
    cols = len(b) + 1

    # Fill in the edge cells with the index of the substring beside them for initial computations
    dist = [[0 for x in range(cols)] for x in range(rows)]

    # Check which cells need to be deleted
    for i in range(1, rows):
        dist[i][0] = i

    # Check which cells need to be inserted
    for i in range(1, cols):
        dist[0][i] = i

    # Iterate through non-edge cells
    for col in range(1, cols):
        for row in range(1, rows):
            # Check if substitution can be done
            if a[row - 1] == b[col - 1]:
                cost = 0
            else:
                cost = 1

            # Find the most optimal next step
            dist[row][col] = min(dist[row - 1][col] + 1,        # Insertion
                                 dist[row][col - 1] + 1,        # Deletion
                                 dist[row - 1][col - 1] + cost)  # Substitution

    # Assign operations to each cell
    dist[0][0] = (0, Operation.SUBSTITUTED)

    for i in range(1, rows):
        dist[i][0] = (i, Operation.DELETED)
    for i in range(1, cols):
        dist[0][i] = (i, Operation.INSERTED)

    for col in range(1, cols):
        for row in range(1, rows):
            if dist[row][col] == dist[row - 1][col]:
                dist[row][col] = (dist[row][col], Operation.INSERTED)
            elif dist[row][col] == dist[row][col - 1]:
                dist[row][col] = (dist[row][col], Operation.DELETED)
            else:
                dist[row][col] = (dist[row][col], Operation.SUBSTITUTED)

    return dist
2

except for the first element, which is supposed to have no operation, but I gave it an Operation.SUBSTITUTED

And this is probably the problem, since that operation subtracts 1 from i and j, which means when matrix[0][0] is dealt with, i and j both get set to -1, which will be out of range.

Perhaps you should be using None instead of Substituted for the first element.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .