Guest User

Untitled

a guest
Jan 16th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. from nltk.metrics import edit_distance
  2.  
  3. class SpellingReplacer(object):
  4. def __init__(self, dict_name = 'en_GB', max_dist = 2):
  5. self.spell_dict = enchant.Dict(dict_name)
  6. self.max_dist = 2
  7.  
  8. def replace(self, word):
  9. if self.spell_dict.check(word):
  10. return word
  11. suggestions = self.spell_dict.suggest(word)
  12.  
  13. if suggestions and edit_distance(word, suggestions[0]) <= self.max_dist:
  14. return suggestions[0]
  15. else:
  16. return word
  17.  
  18. def spell_check(word_list):
  19. checked_list = []
  20. for item in word_list:
  21. replacer = SpellingReplacer()
  22. r = replacer.replace(item)
  23. checked_list.append(r)
  24. return checked_list
  25.  
  26. >>> word_list = ['car', 'colour']
  27. >>> spell_check(words)
  28. ['car', 'color']
  29.  
  30. def edits1(word):
  31. splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
  32. deletes = [a + b[1:] for a, b in splits if b]
  33. transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
  34. replaces = [a + c + b[1:] for a, b in splits for c in alphabet if b]
  35. inserts = [a + c + b for a, b in splits for c in alphabet]
  36. return set(deletes + transposes + replaces + inserts)
  37.  
  38. def min_edit_dist(word1,word2):
  39. len_1=len(word1)
  40. len_2=len(word2)
  41. x = [[0]*(len_2+1) for _ in range(len_1+1)]#the matrix whose last element ->edit distance
  42. for i in range(0,len_1+1):
  43. #initialization of base case values
  44. x[i][0]=i
  45. for j in range(0,len_2+1):
  46. x[0][j]=j
  47. for i in range (1,len_1+1):
  48. for j in range(1,len_2+1):
  49. if word1[i-1]==word2[j-1]:
  50. x[i][j] = x[i-1][j-1]
  51. else :
  52. x[i][j]= min(x[i][j-1],x[i-1][j],x[i-1][j-1])+1
  53. return x[i][j]
  54. from Tkinter import *
  55.  
  56.  
  57. def retrieve_text():
  58. global word1
  59. word1=(app_entry.get())
  60. path="C:Documents and SettingsOwnerDesktopDictionary.txt"
  61. ffile=open(path,'r')
  62. lines=ffile.readlines()
  63. distance_list=[]
  64. print "Suggestions coming right up count till 10"
  65. for i in range(0,58109):
  66. dist=min_edit_dist(word1,lines[i])
  67. distance_list.append(dist)
  68. for j in range(0,58109):
  69. if distance_list[j]<=2:
  70. print lines[j]
  71. print" "
  72. ffile.close()
  73. if __name__ == "__main__":
  74. app_win = Tk()
  75. app_win.title("spell")
  76. app_label = Label(app_win, text="Enter the incorrect word")
  77. app_label.pack()
  78. app_entry = Entry(app_win)
  79. app_entry.pack()
  80. app_button = Button(app_win, text="Get Suggestions", command=retrieve_text)
  81. app_button.pack()
  82. # Initialize GUI loop
  83. app_win.mainloop()
  84.  
  85. from autocorrect import spell
  86.  
  87. print spell('caaaar')
  88. print spell(u'mussage')
  89. print spell(u'survice')
  90. print spell(u'hte')
  91.  
  92. caesar
  93. message
  94. service
  95. the
Add Comment
Please, Sign In to add comment