Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 20th, 2010 | Syntax: None | Size: 1.46 KB | Hits: 62 | Expires: Never
Copy text to clipboard
  1. import re, collections
  2.  
  3. # NWORDS = train(words(NAMESTRING))
  4. alphabet = 'abcdefghijklmnopqrstuvwxyz'
  5. class Spelling(object):
  6. # borrowed from http://norvig.com/spell-correct.html
  7.         def words(self,text):
  8.                 return re.findall('[a-z]+', text.lower())
  9.  
  10.         def train(self,features):
  11.                 model = collections.defaultdict(lambda: 1)
  12.                 for f in features:
  13.                         model[f] += 1
  14.                 return model
  15.  
  16.         def __init__(self, NAMES):
  17.                 self.NWORDS = self.train(self.words(NAMES))
  18.                 self.alphabet = 'abcdefghijklmnopqrstuvwxyz'
  19.  
  20.         def edits1(self,word):
  21.                 splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
  22.                 deletes    = [a + b[1:] for a, b in splits if b]
  23.                 transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
  24.                 replaces   = [a + c + b[1:] for a, b in splits for c in self.alphabet if b]
  25.                 inserts    = [a + c + b for a, b in splits for c in self.alphabet]
  26.                 return set(deletes + transposes + replaces + inserts)
  27.  
  28.         def known_edits2(self,word):
  29.                 return set(e2 for e1 in self.edits1(word) for e2 in self.edits1(e1) if e2 in self.NWORDS)
  30.  
  31.         def known(self,words): return set(w for w in words if w in self.NWORDS)
  32.  
  33.         def correct(self,word):
  34.                 candidates = self.known([word]) or self.known(self.edits1(word)) or self.known_edits2(word) or [word]
  35.                 return max(candidates, key=self.NWORDS.get)
  36.                
  37. class str(str):
  38.         def __init__(self):
  39.                 super(str)
  40.                 self.s=Spelling("words word")
  41.         def spell():
  42.                 return s.correct(self.value)
  43.        
  44.  
  45. print repr((raw_input().correct()))