-
Notifications
You must be signed in to change notification settings - Fork 1
/
word_chekcer.py
75 lines (61 loc) · 2.51 KB
/
word_chekcer.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
# -*- coding: utf-8 -*-
"""
Created on Sat May 5 13:32:08 2018
@author: rcabg
"""
from word_edition import WordEdition
class WordChecker:
def __init__(self, dictionary, totalFreq):
self.dictionary = dictionary
self.wordEdition = WordEdition()
self.totalFreq = totalFreq
self.maxCandidates = 10
def knownWords(self, words):
return list(word for word in words if word in self.dictionary)
def candidates(self, word):
candidates = (self.knownWords([word]) or
self.knownWords(self.wordEdition.allEdits(word)) or
self.knownWords(self.wordEdition.allEdits2(word)))
return self.sortCandidates(set(candidates))
def sortCandidates(self, candidates):
totalFreq = 0
wordList = list()
freqList = list()
for candidate in candidates:
freq = self.dictionary[candidate]
totalFreq += freq
if len(freqList) == 0:
wordList.append(candidate)
freqList.append(freq)
else:
for index, val in enumerate(freqList):
if freq > val:
freqList.insert(index, freq)
wordList.insert(index, candidate)
break
if index + 1 == len(freqList):
freqList.append(freq)
wordList.append(candidate)
break
for index, val in enumerate(freqList):
freqList[index] = freqList[index] * 100 / totalFreq
return (wordList, freqList)
def getWordProbability(self, word):
return self.dictionary[word] / self.totalFreq
def getCorrection(self, word):
print("You wrote: " + word)
words, freqs = self.candidates(word)
if len(words) == 0:
print("**NO CANDIDATES**")
elif words[0] == word:
print("**Word is correct**")
else:
print("Probably you mean to say: ")
count = 0
for index, candidate in enumerate(words):
print(" *- " + candidate + " - " + str('%.2f' % freqs[index]) + " %")
count += 1
if count == self.maxCandidates:
break
def setMaxCandidates(self, maxCandidates):
self.maxCandidates = maxCandidates