-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab5.py
109 lines (88 loc) · 3.42 KB
/
lab5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'''
Created on Feb 20, 2019
@author: Bharddwaj Vemulapalli
username: bvemulap
I pledge my honor that I have abided by the Stevens Honor System.
CS115 - Lab 5
'''
import time
words = []
HITS = 10
def ED(first, second):
''' Returns the edit distance between the strings first and second.'''
if first == '':
return len(second)
elif second == '':
return len(first)
elif first[0] == second[0]:
return ED(first[1:], second[1:])
else:
substitution = 1 + ED(first[1:], second[1:])
deletion = 1 + ED(first[1:], second)
insertion = 1 + ED(first, second[1:])
return min(substitution, deletion, insertion)
#start_time = time.time()
#print(ED("extraordinary", "originality"))
#end_time = time.time()
#print('Computation time:', end_time - start_time, 'seconds')
def fastED(first, second):
'''Returns the edit distance between the strings first and second. Uses
memoization to speed up the process.'''
def fastED_helper(first,second,memo):
if (first, second) in memo:
return memo[(first, second)]
elif not first:
result = len(second)
elif not second:
result = len(first)
elif first[0] == second[0]:
result = fastED_helper(first[1:], second[1:], memo)
else:
substitution = 1 + fastED_helper(first[1:], second[1:],memo)
deletion = 1 + fastED_helper(first[1:], second,memo)
insertion = 1 + fastED_helper(first, second[1:],memo)
result = min(substitution, deletion, insertion)
memo[(first, second)] = result
return result
return fastED_helper(first, second,{})
#start_time = time.time()
#print(fastED("extraordinary", "originality"))
#end_time = time.time()
#print('Computation time:', end_time - start_time, 'seconds')
print(fastED("antidisestablishment", "antiquities"))
print(fastED("xylophone", "yellow"))
print(fastED("follow", "yellow"))
print(fastED("lower", "hover"))
def getSuggestions(user_input):
'''For each word in the global words list, determine the edit distance of
the user_input and the word. Return a list of tuples containing the
(edit distance, word).
Hint: Use map and lambda, and it's only one line of code!'''
return list(map(lambda x: (fastED(user_input, x),x), words))
def spam():
'''Main loop for the program that prompts the user for words to check.
If the spelling is correct, it tells the user so. Otherwise, it provides up
to HITS suggestions.
To exit the loop, just hit Enter at the prompt.'''
while True:
user_input = input('spell check> ').strip()
if user_input == '':
break
if user_input in words:
print('Correct')
else:
start_time = time.time()
suggestions = getSuggestions(user_input)
suggestions.sort()
endTime = time.time()
print('Suggested alternatives:')
for suggestion in suggestions[:HITS]:
print(' %s' % suggestion[1])
print('Computation time:', endTime - start_time, 'seconds')
print('Bye')
if __name__ == '__main__':
f = open('3esl.txt')
for word in f:
words.append(word.strip())
f.close()
spam()