def score(): """Handle requests for /score via POST""" # Read files s1 = request.form.get("string1") s2 = request.form.get("string2") if not s1 or not s2: abort(400, "missing strings") # Score files matrix = distances(s1, s2) # Extract operations from table operations = [] i, j = len(s1), len(s2) while True: _, operation = matrix[i][j] if not operation: break if operation == Operation.INSERTED: j -= 1 elif operation == Operation.DELETED: i -= 1 else: i -= 1 j -= 1 operations.append(operation) operations.reverse() # Maintain list of intermediate strings, operation, and descriptions transitions = [(s1, None, None)] i = 0 # Apply each operation prev = s1 for operation in operations: # Update string and description of operation if operation == Operation.INSERTED: s = (prev[:i], s2[i], prev[i:]) description = f"inserted '{s2[i]}'" prev = prev[:i] + s2[i] + prev[i:] i += 1 elif operation == Operation.DELETED: s = (prev[:i], prev[i], prev[i + 1:]) description = f"deleted '{prev[i]}'" prev = prev[:i] + prev[i + 1:] elif prev[i] != s2[i]: s = (prev[:i], s2[i], prev[i + 1:]) description = f"substituted '{prev[i]}' with '{s2[i]}'" prev = prev[:i] + s2[i] + prev[i + 1:] i += 1 else: i += 1 continue transitions.append((s, str(operation), description)) transitions.append((s2, None, None)) # Output comparison return render_template("score.html", matrix=matrix, s1=s1, s2=s2, operations=transitions)
def test_matrix_of_harward_and_yale(self): expected = [[(0, None), (1, Operation.INSERTED), (2, Operation.INSERTED), (3, Operation.INSERTED), (4, Operation.INSERTED), (5, Operation.INSERTED), (6, Operation.INSERTED), (7, Operation.INSERTED)], [(1, Operation.DELETED), (1, Operation.SUBSTITUTED), (2, Operation.SUBSTITUTED), (3, Operation.SUBSTITUTED), (4, ), (5, Operation.INSERTED), (6, Operation.INSERTED), (7, Operation.INSERTED)], [(2, Operation.DELETED), (2, Operation.SUBSTITUTED), (1, Operation.SUBSTITUTED), (2, Operation.SUBSTITUTED), (3, Operation.INSERTED), (4, Operation.INSERTED), (5, Operation.INSERTED), (6, Operation.INSERTED)], [(3, Operation.DELETED), (3, Operation.SUBSTITUTED), (2, Operation.DELETED), (2, ), (3, ), (4, ), (5, ), (6, Operation.INSERTED)], [(4, Operation.DELETED), (4, Operation.SUBSTITUTED), (3, Operation.DELETED), (3, ), (3, ), (4, ), (5, ), (6, Operation.INSERTED)]] calculated = distances("Yale", "Harvard") self.assertStepsAreEqual(calculated, expected)
from helpers import distances matrix = distances("aaa", "bbb") for i in range(len("aaa") + 2): for j in range(len("bbb") + 2): print(matrix[i][j], end=" | ") print("|")
def test_matrix_edit_distance_between_yale_harward_should_be_6(self): matrix = distances("Yale", "Harward") distance = matrix[4][7][0] self.assertEqual(distance, 6)
def test_matrix_size_should_be_lenAplus1XlenBplus1(self): s1 = "sdfsdf" s2 = "asdrwe" matrix = distances(s1, s2) self.assertEqual(len(matrix), len(s1) + 1) self.assertEqual(len(matrix[len(s1)]), len(s2) + 1)
from helpers import distances from sys import argv s1 = argv[1] s2 = argv[2] cost = distances(s1, s2) print(cost[len(s1)][len(s2)])