use popen2 module to drive the ispell typo checker
Python, 24 lines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# # ispell interface tested with ispell 3.2.03 import popen2 class ispell: def __init__(self): self._f = popen2.Popen3("ispell") self._f.fromchild.readline() #skip the credit line def __call__(self, word): self._f.tochild.write(word+'\n') self._f.tochild.flush() s = self._f.fromchild.readline() self._f.fromchild.readline() #skip the blank line if s[:8]=="word: ok": return None else: return (s[17:-1]).split(', ') f = ispell() print f('hello') print f('stinge')
The class can only check a single word, it return None if the entry is valid dictionnary or a list containing the ispell suggestions.
Note that for real work, you can found a complete wrapper around ispell (and spell) at: http://www.scriptfoundry.com/snake/snakespell/snakespell/
Which ispell are you using? Which ispell are you using? Every ispell I tried wants to read from a file. Is there an option to spell one word from stdin?
Also, try pexpect instead of popen:
Here is a modified version for other versions of ispell.
Arrg try again!
Other Methods. Hi,
Since this is an extremely high-ranking page when Googling for "python spell check" and similar phrases, I thought I'd take the time to point out several new [well, compared to this recipie :-)] Python modules for spell checking. Hopefully you might find one to suit your needs:
"aspell-python" is a wrapper around aspell: http://www.republika.pl/wmula/proj/aspell-python/index.html
"pyenchant" is a wrapper around enchant, a backend-neutral spell check system: http://pyenchant.sourceforge.net
"pyaspell" is another aspell wrapper: http://savannah.nongnu.org/projects/pyaspell/
"myspell-python" is a wrapper around MySpell: http://developer.berlios.de/projects/myspell-python/
Disclaimer: I am the author of PyEnchant
Spell checking with Python and Ispell using the French dictionary. I found this hack to work for me using Ispell and the French dictionary (largerly based from the examples from this page):