From 57d107732a70519f0f89d018260f816317f0a7e9 Mon Sep 17 00:00:00 2001 From: Hudson Gouge Date: Sat, 9 Mar 2024 22:47:08 -0500 Subject: [PATCH] fixes --- Aardvark Interpreter/Exec.py | 2 +- Aardvark Interpreter/nlp.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 Aardvark Interpreter/nlp.py diff --git a/Aardvark Interpreter/Exec.py b/Aardvark Interpreter/Exec.py index 8989be1d..a075d966 100644 --- a/Aardvark Interpreter/Exec.py +++ b/Aardvark Interpreter/Exec.py @@ -6,7 +6,7 @@ from Operators import Operators import random import math -from nltk import edit_distance +from nlp import edit_distance import Types from Types import ( Null, diff --git a/Aardvark Interpreter/nlp.py b/Aardvark Interpreter/nlp.py new file mode 100644 index 00000000..c067c06a --- /dev/null +++ b/Aardvark Interpreter/nlp.py @@ -0,0 +1,29 @@ +def edit_distance(word1, word2): + # Get the lengths of the two words. + m = len(word1) + n = len(word2) + + # Create a 2D array to store the dynamic programming table. + dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)] + + # Initialize the first row and column of the DP table. + for i in range(m + 1): + dp[i][0] = i + for j in range(n + 1): + dp[0][j] = j + + # Fill in the DP table. + for i in range(1, m + 1): + for j in range(1, n + 1): + # Calculate the cost of substitution (0 if characters are the same, 1 otherwise). + cost = 0 if word1[i - 1] == word2[j - 1] else 1 + + # Use the minimum of three possible operations: deletion, insertion, or substitution. + dp[i][j] = min( + dp[i - 1][j] + 1, # Deletion + dp[i][j - 1] + 1, # Insertion + dp[i - 1][j - 1] + cost, # Substitution + ) + + # The final value in the DP table represents the edit distance between the two words. + return dp[m][n]