0% found this document useful (0 votes)
56 views5 pages

NLP Assignment-3 (Print)

The document contains code to find the minimum edit distance between two strings using dynamic programming. It first defines a recursive function that takes in two strings, calculates the length of each, and initializes a 2D table to store results. It then fills the table in a bottom-up manner by considering insert, remove, and replace operations on corresponding characters of the two strings. The minimum value in the table is returned as the edit distance. A second program uses this function for spelling checking and correction by finding the closest match within a dictionary.

Uploaded by

amey bhirange
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views5 pages

NLP Assignment-3 (Print)

The document contains code to find the minimum edit distance between two strings using dynamic programming. It first defines a recursive function that takes in two strings, calculates the length of each, and initializes a 2D table to store results. It then fills the table in a bottom-up manner by considering insert, remove, and replace operations on corresponding characters of the two strings. The minimum value in the table is returned as the edit distance. A second program uses this function for spelling checking and correction by finding the closest match within a dictionary.

Uploaded by

amey bhirange
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

NLP Assignment-3

Program (To find minimum edit distance):


def min_edit_distance(str1, str2):

m = len(str1)

n = len(str2)

# Create a table to store results of subproblems

dp = [[0] * (n + 1) for _ in range(m + 1)]

# Fill dp[][] in bottom-up manner

for i in range(m + 1):

for j in range(n + 1):

# If first string is empty, only option is to

# insert all characters of second string

if i == 0:

dp[i][j] = j # Min. operations = j

# If second string is empty, only option is to

# remove all characters of second string

elif j == 0:

dp[i][j] = i # Min. operations = i

# If last characters are same, ignore last char

# and recur for remaining string

elif str1[i - 1] == str2[j - 1]:

dp[i][j] = dp[i - 1][j - 1]

# If last character are different, consider all

# possibilities and find minimum

else:
dp[i][j] = 1 + min(dp[i][j - 1], # Insert

dp[i - 1][j], # Remove

dp[i - 1][j - 1]) # Replace

return dp[m][n]

# Take input from user for str1 and str2

str1 = input("Enter the first string: ")

str2 = input("Enter the second string: ")

print("Minimum Edit Distance between '{}' and '{}' is: {}".format(str1, str2, min_edit_distance(str1,
str2)))

Output:

Program (Spelling checker and corrector):


import numpy as np

def min_edit_distance(source, target):

m = len(source)

n = len(target)

dp = np.zeros((m + 1, n + 1))

for i in range(m + 1):

dp[i][0] = i

for j in range(n + 1):

dp[0][j] = j
for i in range(1, m + 1):

for j in range(1, n + 1):

if source[i - 1] == target[j - 1]:

dp[i][j] = dp[i - 1][j - 1]

else:

dp[i][j] = 1 + min(dp[i - 1][j], # Deletion

dp[i][j - 1], # Insertion

dp[i - 1][j - 1]) # Substitution

return dp[m][n]

def spelling_checker(word, dictionary):

min_distance = float('inf')

suggested_word = None

for dict_word in dictionary:

distance = min_edit_distance(word, dict_word)

if distance < min_distance:

min_distance = distance

suggested_word = dict_word

if min_distance <= 3: # Threshold for suggesting a word

print(f"Entered word: {word}")

print(f"Suggested correction: {suggested_word}")

else:

print(f"Entered word: {word}")

print("No suggestion found in the dictionary.")

def main():

dictionary = ['hello', 'world', 'python', 'programming', 'language']

word = input("Enter a word to check its spelling: ")


spelling_checker(word, dictionary)

if __name__ == "__main__":

main()

Output:
3) Levenshtein Distance Code:

def min_edit_distance(str1, str2):

m = len(str1)

n = len(str2)

# Create a table to store results of subproblems

dp = [[0] * (n + 1) for _ in range(m + 1)]

# Fill dp[][] in bottom up manner

for i in range(m + 1):

for j in range(n + 1):

# If first string is empty, only option is to

# insert all characters of second string

if i == 0:

dp[i][j] = j

# If second string is empty, only option is to

# remove all characters of first string

elif j == 0:

dp[i][j] = i

# If last characters are different, consider all

# possibilities and find minimum

elif str1[i - 1] != str2[j - 1]:

dp[i][j] = min(dp[i - 1][j] + 1, # Remove

dp[i][j - 1] + 1, # Insert

dp[i - 1][j - 1] + 2) # Replace

# If last characters are same, no operation needed

else:

dp[i][j] = dp[i - 1][j - 1]

return dp[m][n]

str1 = input("Enter string 1: ")

str2 = input("Enter string 2: ")

print("Minimum Edit distance between",str1,"and",str2,"is",min_edit_distance(str1,str2))

You might also like