Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hg0428 committed Mar 10, 2024
1 parent 43e7bbf commit 57d1077
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Aardvark Interpreter/Exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions Aardvark Interpreter/nlp.py
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 57d1077

Please sign in to comment.