home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Freelog 116
/
FreelogNo116-JuilletSeptembre2013.iso
/
Bureautique
/
gImageReader
/
gimagereader_0.9-1_win32.exe
/
bin
/
enchant
/
checker
/
CmdLineChecker.pyc
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2011-03-24
|
6KB
|
194 lines
# Source Generated with Decompyle++
# File: in.pyc (Python 2.7)
'''
enchant.checker.CmdLineChecker: Command-Line spell checker
This module provides the class CmdLineChecker, which interactively
spellchecks a piece of text by interacting with the user on the
command line. It can also be run as a script to spellcheck a file.
'''
import sys
from enchant.checker import SpellChecker
from enchant.utils import printf
class CmdLineChecker:
'''A simple command-line spell checker.
This class implements a simple command-line spell checker. It must
be given a SpellChecker instance to operate on, and interacts with
the user by printing instructions on stdout and reading commands from
stdin.
'''
_DOC_ERRORS = [
'stdout',
'stdin']
def __init__(self):
self._stop = False
self._checker = None
def set_checker(self, chkr):
self._checker = chkr
def get_checker(self, chkr):
return self._checker
def run(self):
'''Run the spellchecking loop.'''
self._stop = False
for err in self._checker:
self.error = err
printf([
'ERROR:',
err.word])
printf([
'HOW ABOUT:',
err.suggest()])
status = self.read_command()
while not status and not (self._stop):
status = self.read_command()
if self._stop:
break
continue
def print_help(self):
printf([
'0..N: replace with the numbered suggestion'])
printf([
'R0..rN: always replace with the numbered suggestion'])
printf([
'i: ignore this word'])
printf([
'I: always ignore this word'])
printf([
'a: add word to personal dictionary'])
printf([
'e: edit the word'])
printf([
'q: quit checking'])
printf([
'h: print this help message'])
printf([
'----------------------------------------------------'])
printf([
'HOW ABOUT:',
self.error.suggest()])
def read_command(self):
cmd = raw_input('>> ')
cmd = cmd.strip()
if cmd.isdigit():
repl = int(cmd)
suggs = self.error.suggest()
if repl >= len(suggs):
printf([
'No suggestion number',
repl])
return False
None([
"Replacing '%s' with '%s'" % (self.error.word, suggs[repl])])
self.error.replace(suggs[repl])
return True
if None[0] == 'R':
if not cmd[1:].isdigit():
printf([
"Badly formatted command (try 'help')"])
return False
repl = None(cmd[1:])
suggs = self.error.suggest()
if repl >= len(suggs):
printf([
'No suggestion number',
repl])
return False
None.error.replace_always(suggs[repl])
return True
if None == 'i':
return True
if None == 'I':
self.error.ignore_always()
return True
if None == 'a':
self.error.add()
return True
if None == 'e':
repl = raw_input('New Word: ')
self.error.replace(repl.strip())
return True
if None == 'q':
self._stop = True
return True
if None.startswith(cmd.lower()):
self.print_help()
return False
None([
"Badly formatted command (try 'help')"])
return False
def run_on_file(self, infile, outfile = None, enc = None):
'''Run spellchecking on the named file.
This method can be used to run the spellchecker over the named file.
If <outfile> is not given, the corrected contents replace the contents
of <infile>. If <outfile> is given, the corrected contents will be
written to that file. Use "-" to have the contents written to stdout.
If <enc> is given, it specifies the encoding used to read the
file\'s contents into a unicode string. The output will be written
in the same encoding.
'''
inStr = ''.join(file(infile, 'r').readlines())
if enc is not None:
inStr = inStr.decode(enc)
self._checker.set_text(inStr)
self.run()
outStr = self._checker.get_text()
if enc is not None:
outStr = outStr.encode(enc)
if outfile is None:
outF = file(infile, 'w')
elif outfile == '-':
outF = sys.stdout
else:
outF = file(outfile, 'w')
outF.write(outStr)
outF.close()
run_on_file._DOC_ERRORS = [
'outfile',
'infile',
'outfile',
'stdout']
def _run_as_script():
'''Run the command-line spellchecker as a script.
This function allows the spellchecker to be invoked from the command-line
to check spelling in a file.
'''
OptionParser = OptionParser
import optparse
op = OptionParser()
op.add_option('-o', '--output', dest = 'outfile', metavar = 'FILE', help = 'write changes into FILE')
op.add_option('-l', '--lang', dest = 'lang', metavar = 'TAG', default = 'en_US', help = 'use language idenfified by TAG')
op.add_option('-e', '--encoding', dest = 'enc', metavar = 'ENC', help = 'file is unicode with encoding ENC')
(opts, args) = op.parse_args()
if len(args) < 1:
raise ValueError('Must name a file to check')
if len(args) > 1:
raise ValueError('Can only check a single file')
chkr = SpellChecker(opts.lang)
cmdln = CmdLineChecker()
cmdln.set_checker(chkr)
cmdln.run_on_file(args[0], opts.outfile, opts.enc)
if __name__ == '__main__':
_run_as_script()