i have this spell checker which i have writted:
import operator
class Corrector(object):
def __init__(self,possibilities):
self.possibilities = possibilities
def similar(self,w1,w2):
w1 = w1[:len(w2)]
w2 = w2[:len(w1)]
return sum([1 if i==j else 0 for i,j in zip(w1,w2)])/float(len(w1))
def correct(self,w):
corrections = {}
for c in self.possibilities:
probability = self.similar(w,c) * self.possibilities[c]/sum(self.possibilities.values())
corrections[c] = probability
return max(corrections.iteritems(),key=operator.itemgetter(1))[0]
here possibilities is a dictionary like:
{word1:value1}
where value is the number of times the word appeared in the corpus.
The similar function returns the probability of similarity between the words: w1 and w2.
in the correct
function, you see that the software loops through all possible outcomes and then computes a probability for each of them being the correct spelling for w.
can i speed up my code by somehow removing the loop?
now i know there might be no answer to this question, if i can't just tell me that i cant!