Mike Kazantsev mike_kazantsev at fraggod.net
Wed May 20 05:37:25 EDT 2009

abosalim wrote:
> I used this code.It works fine,but on word not whole text.I want to
> extend this code to correct
> text file not only a word,but i don't know.If you have any help,please
> inform me.
> def correct(word):
>     candidates = known([word]) or known(edits1(word)) or known_edits2
> (word) or [word]
>     return max(candidates, key=lambda w: NWORDS[w])

Here I assume that "word" is any string consisting of letters, feel free
to add your own check in place of str.isalpha, like word length or case.
Note that simple ops like concatenation work much faster with buffers
than str / unicode.

  text = 'some text to correct (anything, really)'
  result = buffer('')

  word, c = buffer(''), ''
  for c in text:
    if c.isalpha(): word += c
      if word:
        result += correct(word)
        word = buffer('')
      result += c

Mike Kazantsev // fraggod.net

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 205 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20090520/2c5091ed/attachment.sig>

More information about the Python-list mailing list