Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from nltk.metrics import edit_distance
- class SpellingReplacer(object):
- def __init__(self, dict_name = 'en_GB', max_dist = 2):
- self.spell_dict = enchant.Dict(dict_name)
- self.max_dist = 2
- def replace(self, word):
- if self.spell_dict.check(word):
- return word
- suggestions = self.spell_dict.suggest(word)
- if suggestions and edit_distance(word, suggestions[0]) <= self.max_dist:
- return suggestions[0]
- else:
- return word
- def spell_check(word_list):
- checked_list = []
- for item in word_list:
- replacer = SpellingReplacer()
- r = replacer.replace(item)
- checked_list.append(r)
- return checked_list
- >>> word_list = ['car', 'colour']
- >>> spell_check(words)
- ['car', 'color']
- def edits1(word):
- splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
- deletes = [a + b[1:] for a, b in splits if b]
- transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
- replaces = [a + c + b[1:] for a, b in splits for c in alphabet if b]
- inserts = [a + c + b for a, b in splits for c in alphabet]
- return set(deletes + transposes + replaces + inserts)
- def min_edit_dist(word1,word2):
- len_1=len(word1)
- len_2=len(word2)
- x = [[0]*(len_2+1) for _ in range(len_1+1)]#the matrix whose last element ->edit distance
- for i in range(0,len_1+1):
- #initialization of base case values
- x[i][0]=i
- for j in range(0,len_2+1):
- x[0][j]=j
- for i in range (1,len_1+1):
- for j in range(1,len_2+1):
- if word1[i-1]==word2[j-1]:
- x[i][j] = x[i-1][j-1]
- else :
- x[i][j]= min(x[i][j-1],x[i-1][j],x[i-1][j-1])+1
- return x[i][j]
- from Tkinter import *
- def retrieve_text():
- global word1
- word1=(app_entry.get())
- path="C:Documents and SettingsOwnerDesktopDictionary.txt"
- ffile=open(path,'r')
- lines=ffile.readlines()
- distance_list=[]
- print "Suggestions coming right up count till 10"
- for i in range(0,58109):
- dist=min_edit_dist(word1,lines[i])
- distance_list.append(dist)
- for j in range(0,58109):
- if distance_list[j]<=2:
- print lines[j]
- print" "
- ffile.close()
- if __name__ == "__main__":
- app_win = Tk()
- app_win.title("spell")
- app_label = Label(app_win, text="Enter the incorrect word")
- app_label.pack()
- app_entry = Entry(app_win)
- app_entry.pack()
- app_button = Button(app_win, text="Get Suggestions", command=retrieve_text)
- app_button.pack()
- # Initialize GUI loop
- app_win.mainloop()
- from autocorrect import spell
- print spell('caaaar')
- print spell(u'mussage')
- print spell(u'survice')
- print spell(u'hte')
- caesar
- message
- service
- the
Add Comment
Please, Sign In to add comment