-
Notifications
You must be signed in to change notification settings - Fork 0
/
glu_spell.py
346 lines (282 loc) · 9.93 KB
/
glu_spell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
'''
Spell check the GLU source code
'''
import re
import os
import string
import tokenize
# Borrows Aspell code from Wojciech MuXa's Aspell module (BSD licensed)
#
# Original author: Wojciech Mula
# e-mail: [email protected]
# www : http://wmula.republika.pl/proj/aspell-python/
try:
import ctypes
import ctypes.util
except ImportError:
raise ImportError('ctypes library is needed')
class AspellError(Exception): pass
class AspellConfigError(AspellError): pass
class AspellSpellerError(AspellError): pass
class Aspell(object):
'''
Aspell speller object. Allows to check spelling, get suggested
spelling list, manage user dictionarias, and other.
Must be closed with 'close' method, or one may experience
problems, like segfaults.
'''
def __init__(self, configkeys=None, libname=None):
'''
Parameters:
* configkeys - list of configuration parameters;
each element is a pair key & value (both strings)
if None, then default configuration is used
* libname - explicity set aspell library name;
if None then default name is used
'''
if libname is None:
libname = ctypes.util.find_library('aspell')
self.__lib = ctypes.CDLL(libname)
# Initialize speller
# 1. create configuration
config = self.__lib.new_aspell_config()
if config == None:
raise AspellError("Can't create aspell config object")
# 2. parse configkeys arg.
if configkeys is not None:
assert isinstance(configkeys, (tuple,list)), 'Tuple or list expeced'
if len(configkeys) == 2 and \
isinstance(configkeys[0], str) and \
isinstance(configkeys[1], str):
configkeys = [configkeys]
for key, value in configkeys:
assert isinstance(key, str), 'Key must be string'
assert isinstance(value, str), 'Value must be string'
if not self.__lib.aspell_config_replace(config, key, value):
raise self._aspell_config_error(config)
# 3. create speller
possible_error = self.__lib.new_aspell_speller(config)
self.__lib.delete_aspell_config(config)
if self.__lib.aspell_error_number(possible_error) != 0:
self.__lib.delete_aspell_can_have_error(possible_error)
raise AspellError("Can't create speller object")
self.__speller = self.__lib.to_aspell_speller(possible_error)
def check(self, word):
'''
Check if word is present in main, personal or session
dictionary. Boolean value is returned
'''
if not isinstance(word,str):
raise TypeError('String expeced')
return bool(self.__lib.aspell_speller_check(self.__speller,word,len(word)))
def suggest(self, word):
'''
Return list of spelling suggestions of given word.
Works even if word is correct.
'''
if not isinstance(word,str):
raise TypeError('String expeced')
return self._aspellwordlist(
self.__lib.aspell_speller_suggest(
self.__speller,word,len(word)))
def personal_dict(self, word=None):
'''
Aspell's personal dictionary is a user defined, persistent
list of word (saved in certain file).
If 'word' is not given, then method returns list of words stored in
dict. If 'word' is given, then is added to personal dict. New words
are not saved automatically, method 'save_all' have to be call.
'''
if word is not None:
# add new word
assert isinstance(word, str), 'String expeced'
self.__lib.aspell_speller_add_to_personal(
self.__speller,word,len(word))
self._aspell_check_error()
else:
# return list of words from personal dictionary
return self._aspellwordlist(
self.__lib.aspell_speller_personal_word_list(self.__speller))
def session_dict(self, word=None, clear=False):
'''
Aspell's session dictionary is a user defined, volatile
list of word, that is destroyed with aspell object.
If 'word' is None, then list of words from session dictionary
is returned. If 'word' is present, then is added to dict.
If 'clear' is True, then session dictionary is cleared.
'''
if clear:
self.__lib.aspell_speller_clear_session(self.__speller)
self._aspell_check_error()
return
if word is not None:
# add new word
assert isinstance(word, str), 'String expeced'
self.__lib.aspell_speller_add_to_session(
self.__speller,
word,
len(word))
self._aspell_check_error()
else:
# return list of words from personal dictionary
return self._aspellwordlist(
self.__lib.aspell_speller_session_word_list(self.__speller))
def add_replacement_pair(self, misspelled, correct):
'''
Add replacement pair, i.e. pair of misspelled and correct
word. It affects on order of words appear on list returned
by 'suggest' method.
'''
assert isinstance(misspelled,str), 'String is required'
assert isinstance(correct,str), 'String is required'
self.__lib.aspell_speller_store_replacement(self.__speller,
misspelled,len(misspelled),correct,len(correct))
self._aspell_check_error()
def save_all(self):
'''
Saves all words added to personal or session dictionary to
the apell's defined file.
'''
self.__lib.aspell_speller_save_all_word_lists(self.__speller)
self._aspell_check_error()
def configkeys(self):
'''
Returns list of all available config keys that can be passed
to contructor.
List contains a 3-tuples:
1. key name
2. default value of type:
* bool
* int
* string
* list of string
3. short description
if None, then this key is undocumented is should not
be used, unless one know what really do
'''
config = self.__lib.aspell_speller_config(self.__speller)
if config is None:
raise AspellConfigError("Can't get speller's config")
keys_enum = self.__lib.aspell_config_possible_elements(config, 1)
if keys_enum is None:
raise AspellError("Can't get list of config keys")
class KeyInfo(ctypes.Structure):
_fields_ = [
('name', ctypes.c_char_p),
('type', ctypes.c_int),
('default', ctypes.c_char_p),
('desc', ctypes.c_char_p),
('flags', ctypes.c_int),
('other_data', ctypes.c_int)]
key_next = self.__lib.aspell_key_info_enumeration_next
key_next.restype = ctypes.POINTER(KeyInfo)
config = []
while True:
key_info = key_next(keys_enum)
if not key_info:
break
else:
key_info = key_info.contents
if key_info.type == 0:
# string
config.append( (key_info.name,key_info.default,key_info.desc) )
elif key_info.type == 1:
# integer
config.append( (key_info.name,int(key_info.default),key_info.desc) )
elif key_info.type == 2:
# boolean
default = key_info.default.lower() == 'true'
config.append( (key_info.name,default,key_info.desc) )
elif key_info.type == 3:
# list
config.append( (key_info.name,key_info.default.split(),key_info.desc) )
self.__lib.delete_aspell_key_info_enumeration(keys_enum)
return config
def close(self):
'''
Close aspell speller object.
'''
self.__lib.delete_aspell_speller(self.__speller)
# XXX: internal function, do not call directly
def _aspellwordlist(self, wordlist_id):
'''
XXX: internal function
Converts aspell list into python list.
'''
elements = self.__lib.aspell_word_list_elements(wordlist_id)
list = []
while True:
wordptr = self.__lib.aspell_string_enumeration_next(elements)
if not wordptr:
break
else:
word = ctypes.c_char_p(wordptr)
list.append(word.value)
self.__lib.delete_aspell_string_enumeration(elements)
return list
def _aspell_config_error(self, config):
'''
XXX: internal function
Raise excpetion if operation of speller config
caused an error. Additionaly destroy config object.
'''
# make exception object & copy error msg
exc = AspellConfigError(
ctypes.c_char_p(
self.__lib.aspell_config_error_message(config)
).value
)
# then destroy config objcet
self.__lib.delete_aspell_config(config)
# and then raise exception
raise exc
def _aspell_check_error(self):
'''
XXX: internal function
Raise exception if previous speller operation
caused an error.
'''
if self.__lib.aspell_speller_error(self.__speller) != 0:
msg = self.__lib.aspell_speller_error_message(self.__speller)
raise AspellSpellerError(msg)
def test():
# TODO: more test cases
a = Aspell(('lang', 'en'))
print a.check('when')
print a.suggest('wehn')
a.add_replacement_pair('wehn', 'ween')
print a.suggest('wehn')
print a.session_dict()
print a.check('pyaspell')
a.session_dict('pyaspell')
print a.session_dict()
print a.check('pyaspell')
a.session_dict(clear=True)
print a.session_dict()
a.close()
def main():
a = Aspell(('lang', 'en'))
for word in open('glu_words.lst'):
a.personal_dict(word.strip())
re_split = re.compile('[^A-Za-z]+').split
seen = set()
def processStrings(type, token, (srow, scol), (erow, ecol), line):
if type in (tokenize.COMMENT,tokenize.STRING):
#print tokenize.tok_name[type], token, (srow, scol), (erow, ecol), line
words = re_split(line)
for wordn,word in enumerate(words):
#word = ''.join(w for w in word if w in string.ascii_letters)
lword = word.lower()
if lword not in seen and not a.check(word):
seen.add(lword)
print '%-25s [%04d:%03d+%03d]: %s' % (filename, srow, scol, wordn, word) # ,a.suggest(word)
for path,dirs,filenames in os.walk('glu'):
if '.svn' in dirs:
dirs.remove('.svn')
for filename in filenames:
if filename.endswith('.py'):
srcfile = os.path.join(path,filename)
src = open(srcfile)
tokenize.tokenize(src.readline,processStrings)
if __name__ == '__main__':
main()