AI Lab1
AI Lab1
Date: 12-03-2021
Time: 10:00-13:00 (A) and 14:00 to 17:00 (B) Instructor: Dr.
Hashir Kiani
Lab Engineer: Ms Shakeela Bibi
Submitted By:
Fatima Seemab
291310
Lab 1
TASK 1
1. Write down a python program which takes two strings as input and calculate the
Levenshtein/Edit distance between the two strings.
CODE
import numpy
no_of_insertion = 0
no_of_deletion = 0
no_of_substitution = 0
print("checkedits index", i, j)
if (arr[i][j] == (arr[i][j-1]+1)):
print("condition 1 hit")
print(inp[j - 1])
print(out[i - 1])
no_of_deletion = no_of_deletion + 1
j -= 1
if (i == 0 or j == 0):
break
else:
continue
OUTPUT
Task #2
Now modify the above written program in such a way that it takes two text files containing
single- line and lowercase English sentences named as reference.txt and hypothesis.txt,
and outputs the file result.txt containing Levenshtein distance of these two files as below. The
distance should be word level and not character level.
CODE
import numpy
no_of_insertion = 0
no_of_deletion = 0
no_of_substitution = 0
def checkedits(i ,j):
global no_of_insertion, no_of_deletion, no_of_substitution, row, col
print("checkedits index", i, j)
if (arr[i][j] == (arr[i][j-1]+1)):
print("condition 1 hit")
print(file1words[j - 1])
print(file2words[i - 1])
no_of_deletion = no_of_deletion + 1
j -= 1
if (i == 0 or j == 0):
break
else:
continue
print(row)
print(col)
OUTPUT
output on console
Task #3
Now modify the above program so that it ignores 10 common words in such a way:-
➢ Insertions and deletions involving these common words are ignored
➢ Substitutions are ignored when both initial and final word are one of 10 common
words
CODE
import numpy
no_of_insertion = 0
no_of_deletion = 0
no_of_substitution = 0
common_words=["the","of","and","a","be","this","there","an","been","some"
]
def checkedits(i ,j):
global no_of_insertion, no_of_deletion, no_of_substitution, row, col
print("checkedits index", i, j)
i = i - 1
j = j - 1
continue
print(i, j)
print(row)
print(col)
OUTPUT
Console output