-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spell_Correction.py
59 lines (51 loc) · 1.56 KB
/
Spell_Correction.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
__author__ = 'Januka Samaranayake'
import enchant
import operator
from collections import Counter
from math import sqrt
from Normalize import toLowerCase
from Normalize import removePunctuations
from Normalize import removeRedundantWhiteSpaces
def spellCorrect(str_input):
str_input = removeRedundantWhiteSpaces(str_input)
str_input = toLowerCase(str_input)
str_input = removePunctuations(str_input).split(" ")
out_array = []
for word in str_input:
d = enchant.request_pwl_dict("words.txt")
isword = d.check(word)
if (isword):
out_array.append(word)
else:
word_list = d.suggest(word)
out = suggestions(word_list, word)
out_array.append(out)
output = ' '.join(out_array)
return output
def suggestions(word, user_input):
dict = {}
for i in word:
key_in_dict = i
Va = word2vec(i)
Vb = word2vec(user_input)
cos = cosdis(Va, Vb)
if (cos > 0.5):
dict[key_in_dict] = cos
else:
pass
if (len(dict) == 0):
out = user_input
elif (len(dict) == 1):
str = user_input
out = str.replace(user_input, dict.keys()[0]).lower()
else:
out = max(dict.iteritems(), key=operator.itemgetter(1))[0].lower()
return out
def word2vec(word):
cw = Counter(word)
sw = set(cw)
lw = sqrt(sum(c * c for c in cw.values()))
return cw, sw, lw
def cosdis(v1, v2):
common = v1[1].intersection(v2[1])
return sum(v1[0][ch] * v2[0][ch] for ch in common) / v1[2] / v2[2]