-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspell.py
60 lines (46 loc) · 1.36 KB
/
spell.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
import distance
from time import time
import corpus
from difflib import SequenceMatcher
global dictionary
dictionary = corpus.load_dictionary('dictionaries')
def similar(a, b):
a = a.lower()
b = b.lower()
a = a.strip()
b = b.strip()
return SequenceMatcher(None, a, b).ratio()
def get_candiate_words(word, limit = 20):
global dictionary
if word[0] not in dictionary:
return []
search_space = dictionary[word[0]]
print('search space', len(search_space))
words = sorted(distance.ifast_comp(word, search_space))[:limit]
return words
def get_correct_word(word):
candidates = get_candiate_words(word)
print(candidates)
if len(candidates) == 0:
return word
max_sim = 0
max_sim_index = 0
for i, c in enumerate(candidates):
_word = c[1]
similarity = similar(_word,word)
if similarity > max_sim:
max_sim = similarity
max_sim_index = i
return candidates[max_sim_index][1]
if __name__ == "__main__":
t1 = time()
sentence = 'Addess: S/O: Manou Ram Palel, House'
tokens = corpus.tokenize(sentence)
print(tokens)
for word in tokens:
word = word.lower()
corrected = get_correct_word(word)
print('original: ', word)
print('corrected: ',corrected)
t2 = time()
print('time taken', t2 - t1)