[ Index ] |
|
Code source de Kupu-1.3.5 |
1 #!/usr/bin/python 2 3 """Zope 2 spellchecker web service for Kupu""" 4 5 COMMAND = 'aspell -a' 6 7 import popen2, re 8 9 try: 10 from Globals import ClassSecurityInfo 11 except ImportError: 12 pass 13 else: 14 # hmmm... Zope 2... 15 __allow_access_to_unprotected_subobjects__ = 1 16 17 class SpellChecker: 18 """Simple spell checker, uses ispell (or aspell) with pipes""" 19 20 __allow_access_to_unprotected_subobjects__ = 1 21 22 reg_unknown = re.compile('^& (.*?) \d* \d*: (.*)$', re.U) 23 reg_unknown_no_replacement = re.compile('^\# (.*?) \d*.*$', re.U) 24 25 def __init__(self): 26 self.chout, self.chin = popen2.popen2(COMMAND) 27 # throw away intro 28 self.read_line() 29 30 def __del__(self): 31 self.chout.close() 32 self.chin.close() 33 34 def check(self, text): 35 """checks a line of text 36 37 returns None if spelling was okay, and an HTML string with words 38 that weren't recognized marked (with a span class="wrong_spelling") 39 """ 40 result = {} 41 for line in text.split('\n'): 42 line = line.strip() 43 if line: 44 self.write_line(line) 45 while 1: 46 resline = self.read_line() 47 if not resline.strip(): 48 break 49 if resline.strip() != '*': 50 match = self.reg_unknown.match(resline) 51 have_replacement = True 52 if not match: 53 match = self.reg_unknown_no_replacement.match(resline) 54 have_replacement = False 55 assert match, 'Unknown formatted line: %s' % resline 56 word = match.group(1) 57 if result.has_key(word): 58 continue 59 replacements = [] 60 if have_replacement: 61 replacements = match.group(2).split(', ') 62 result[word] = replacements 63 return result 64 65 def read_line(self): 66 buf = [] 67 while 1: 68 char = self.read_char() 69 if not char: 70 return '' 71 if char == '\n': 72 return ''.join(buf) 73 buf.append(char) 74 75 def write_line(self, line): 76 try: 77 self.chin.write('%s\n' % line) 78 self.chin.flush() 79 return 80 except IOError: 81 self.reconnect() 82 self.chin.write('%s\n' % line) 83 self.chin.flush() 84 return 85 raise 86 87 def read_char(self): 88 try: 89 return self.chout.read(1) 90 except IOError: 91 self.reconnect() 92 return self.chout.read(1) 93 raise 94 95 def reconnect(self): 96 try: 97 self.chout.close() 98 except IOError: 99 pass 100 try: 101 self.chin.close() 102 except IOError: 103 pass 104 self.chout, self.chin = popen2.popen2(COMMAND) 105 106 def format_result(result): 107 """convert the result dict to XML""" 108 buf = ['<?xml version="1.0" encoding="UTF-8" ?>\n<spellcheck_result>'] 109 for key, value in result.items(): 110 buf.append('<incorrect><word>') 111 buf.append(key) 112 buf.append('</word><replacements>') 113 buf.append(' '.join(value)) 114 buf.append('</replacements></incorrect>') 115 buf.append('</spellcheck_result>') 116 return ''.join(buf) 117 118 if __name__ == '__main__': 119 c = SpellChecker() 120 while 1: 121 line = raw_input('Enter text to check: ') 122 if line == 'q': 123 break 124 ret = c.check(line) 125 if ret is None: 126 print 'okay' 127 else: 128 print ret
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 15:30:41 2007 | par Balluche grâce à PHPXref 0.7 |