2008-03-16 18:47:54 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import os, sys
|
2008-03-16 16:08:37 -04:00
|
|
|
from subprocess import Popen, PIPE, STDOUT
|
2007-03-06 10:05:38 -05:00
|
|
|
import cache
|
|
|
|
|
|
|
|
_speller = None
|
|
|
|
_can_spell = os.system('which ispell > /dev/null 2>&1') == 0
|
|
|
|
|
|
|
|
def can_spell():
|
|
|
|
return _can_spell
|
|
|
|
def get_speller():
|
|
|
|
global _speller
|
2008-11-08 23:13:12 -05:00
|
|
|
if _can_spell and not _speller:
|
|
|
|
_speller = Speller()
|
2007-03-06 10:05:38 -05:00
|
|
|
return _speller
|
2008-11-08 23:13:12 -05:00
|
|
|
def free():
|
2009-11-11 23:22:14 -05:00
|
|
|
global _speller
|
2008-11-08 23:13:12 -05:00
|
|
|
if _speller:
|
2008-11-10 14:58:12 -05:00
|
|
|
_speller.stop()
|
2009-07-23 10:04:12 -04:00
|
|
|
_speller = None
|
2007-03-06 10:05:38 -05:00
|
|
|
|
2008-03-14 17:17:04 -04:00
|
|
|
class Speller(object):
|
2007-03-06 10:05:38 -05:00
|
|
|
def __init__(self, cmd='ispell'):
|
|
|
|
self.pipe = None
|
|
|
|
self.cache = cache.CacheDict()
|
|
|
|
self.cmd = cmd
|
|
|
|
self.start()
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
assert self.pipe is None
|
2008-03-16 16:26:32 -04:00
|
|
|
self.pipe = Popen('%s -a' % self.cmd, shell=True, stdin=PIPE, stdout=PIPE)
|
|
|
|
self.pipe.stdout.readline()
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
def stop(self):
|
2008-03-16 16:26:32 -04:00
|
|
|
self.pipe.stdin.close()
|
|
|
|
self.pipe.stdout.close()
|
2007-03-06 10:05:38 -05:00
|
|
|
self.pipe = None
|
|
|
|
|
|
|
|
def restart(self):
|
|
|
|
self.stop()
|
|
|
|
self.start()
|
|
|
|
|
|
|
|
def flush(self, word):
|
|
|
|
if word in self.cache:
|
|
|
|
del self.cache[word]
|
|
|
|
|
|
|
|
def check(self, word, caps=False, title=True):
|
|
|
|
# here are some quick checks:
|
|
|
|
# 1. zero-length words
|
|
|
|
# 2. all-caps word
|
|
|
|
# 3. words whose first letter is capitalized
|
|
|
|
if len(word) == 0:
|
|
|
|
return True
|
|
|
|
elif not caps and word.isupper():
|
|
|
|
return True
|
|
|
|
elif not title and word[0].isupper():
|
|
|
|
return True
|
|
|
|
|
|
|
|
result = False
|
|
|
|
if word in self.cache:
|
|
|
|
result = self.cache[word]
|
|
|
|
else:
|
|
|
|
if self.pipe.poll() >= 0:
|
|
|
|
self.pipe = None
|
|
|
|
self.start()
|
2008-03-16 16:08:37 -04:00
|
|
|
self.pipe.stdin.write("%s\n" % (word))
|
|
|
|
self.pipe.stdin.flush()
|
2008-03-16 16:26:32 -04:00
|
|
|
l = self.pipe.stdout.readline()
|
2007-03-06 10:05:38 -05:00
|
|
|
if l.startswith("*") or l.startswith("+") or l.startswith("-"):
|
|
|
|
result = True
|
|
|
|
while True:
|
2008-03-16 16:26:32 -04:00
|
|
|
l = self.pipe.stdout.readline()
|
2007-03-06 10:05:38 -05:00
|
|
|
if l == "\n":
|
|
|
|
break
|
|
|
|
self.cache[word] = result
|
|
|
|
return result
|
|
|
|
|
|
|
|
def learn(self, word):
|
|
|
|
if self.pipe.poll() >= 0:
|
|
|
|
self.pipe = None
|
|
|
|
self.start()
|
2008-03-16 16:26:32 -04:00
|
|
|
self.pipe.stdin.write("*%s\n" % (word))
|
|
|
|
self.pipe.stdin.flush()
|
2007-03-06 10:05:38 -05:00
|
|
|
self.flush(word)
|
|
|
|
|
2008-03-16 18:47:54 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
s = Speller()
|
|
|
|
while True:
|
|
|
|
sys.stdout.write('enter a word: ')
|
|
|
|
line = sys.stdin.readline()
|
|
|
|
if not line:
|
2020-08-31 20:58:27 -04:00
|
|
|
print()
|
2008-03-16 18:47:54 -04:00
|
|
|
break
|
|
|
|
elif line == '\n':
|
|
|
|
continue
|
|
|
|
elif ' ' in line:
|
2020-08-31 20:58:27 -04:00
|
|
|
print('please enter a single world')
|
2008-03-16 18:47:54 -04:00
|
|
|
continue
|
|
|
|
for word in line.split('-'):
|
|
|
|
word = word.strip()
|
|
|
|
if s.check(word):
|
2020-08-31 20:58:27 -04:00
|
|
|
print('%s: ok' % word)
|
2008-03-16 18:47:54 -04:00
|
|
|
else:
|
2020-08-31 20:58:27 -04:00
|
|
|
print('%s: misspelled' % word)
|