Welcome, guest | Sign In | My Account | Store | Cart

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/

5 comments

Noah Spurrier 21 years, 6 months ago  # | flag

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:

http://pexpect.sourceforge.net/

Yours, Noah

Chris Thomson 19 years, 6 months ago  # | flag

Here is a modified version for other versions of ispell.

#ispell class found on internet: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/117221
#modified by chris thomson
class ispell:
    def __init__(self):
        self._f = popen2.Popen3("ispell -a")
        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()
    if not (s[:1]=="*" or s[:1]=="+" or s[:1]=="-" or s[:1]=="#" or s[:1]=="?" or s[:1]=="<pre>
#ispell class found on internet: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/117221
#modified by chris thomson
class ispell:
    def __init__(self):
        self._f = popen2.Popen3("ispell -a")
        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()
    if not (s[:1]=="*" or s[:1]=="+" or s[:1]=="-" or s[:1]=="#" or s[:1]=="?" or s[:1]=="

</pre>

Chris Thomson 19 years, 6 months ago  # | flag

Arrg try again!

class ispell:
    def __init__(self):
        self._f = popen2.Popen3("ispell -a")
        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()
    if not (s[:1]=="*" or s[:1]=="+" or s[:1]=="-" or s[:1]=="#" or s[:1]=="?" or s[:1]=="&amp;"):
        return None
        self._f.fromchild.readline()
        if s[:1]=="*" or s[:1]=="+" or s[:1]=="-":     #correct spelling
        return None
        elif s[:1]=="#":  # no matches
            return []
        else:
        m = re.compile("^[&amp;\?] \w+ [0-9]+ [0-9]+:([\w\- ,]+)$", re.M).search("\n"+s, 1)
            return (m.group(1).split(', '))
Ryan Kelly 18 years, 10 months ago  # | flag

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/

Cheers,

Ryan

Disclaimer: I am the author of PyEnchant

Mauro Cherubini 17 years, 1 month ago  # | flag

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):

#!/usr/bin/python

import popen2

class ispell:
    def __init__(self):
        self._f = popen2.Popen3("/sw/bin/ispell -a -d francais")
        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()
        if not (s[:1]=="*" or s[:1]=="+" or s[:1]=="-" or s[:1]=="#" or s[:1]=="?" or s[:1]=="&amp;"):
            return None
        self._f.fromchild.readline()
        if s[:1]=="*" or s[:1]=="+" or s[:1]=="-":     #correct spelling
            return None
        elif s[:1]=="#":  # no matches
            return []
        else:
            #print s
            info, suggested = s.split(': ')
            return suggested.split(', ')

if __name__=="__main__":
    f = ispell()
    print f('scene')
Created by Sébastien Keim on Mon, 4 Mar 2002 (PSF)
Python recipes (4591)
Sébastien Keim's recipes (24)

Required Modules

Other Information and Tasks