ActiveState Powered by ActiveState

Recipe 117221: spell checking


use popen2 module to drive the ispell typo checker

Python
 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')

Discussion

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/

Comments

  1. 1. At 11:07 a.m. on 13 oct 2002, Noah Spurrier said:

    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

  2. 2. At 7:13 a.m. on 1 oct 2004, Chris Thomson said:

    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>

  3. 3. At 7:15 a.m. on 1 oct 2004, Chris Thomson said:

    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(', '))
    
  4. 4. At 7:49 p.m. on 13 jun 2005, Ryan Kelly said:

    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

  5. 5. At 8:10 a.m. on 27 feb 2007, Mauro Cherubini said:

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

Sign in to comment