Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[6fe32e]: plugins / spellcheck.py Maximize Restore History

Download this file

spellcheck.py    515 lines (400 with data), 17.3 kB

  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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import wx
import wx.stc
import wx.lib.scrolledpanel as scrolled
import os
import time
import traceback
import sys
try:
UNICODE = sys.modules['__main__'].UNICODE
except:
UNICODE = 0
class defdict(dict):
def __getitem__(self, key):
try:
return dict.__getitem__(self, key)
except KeyError:
return key
goodch = dict.fromkeys('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
transtable = ''.join([(' ', i.lower())[i in goodch] for i in [chr(j) for j in xrange(256)]])
non_word = defdict([(chr(i), ' ') for i in xrange(256) if chr(i) not in goodch])
fil = os.path.join(os.path.normpath(os.path.join(os.getcwd(),
os.path.split(__file__)[0])),
'dictionary.txt')
dictionary = {}
alphabet = 'abcdefghijklmnopqrstuvwxyz'
def _dri(word, alphabet):
for i in xrange(len(word)):
x = word[:i]
y = word[i+1:]
#will attempt to delete every individual character from the word
yield x + y
z = word[i:]
for j in alphabet:
#will attempt to replace every individual character from the word
#with another character
yield x + j + y
#will attempt to insert a new character into every position in the
#word except for the last
yield x + j + z
#will attempt to swap every internal pair of characters
yield x + word[i+1:i+2] + word[i:i+1] + word[i+2:]
for j in alphabet:
#inserts a new character at the end of the word
yield word + j
def suggest(word, dcts):
_a = alphabet
x = {}
for i in _dri(word, _a):
if i not in x:
for j in dcts:
if i in j:
x[i] = None
break
x = x.keys()
x.sort()
return x
class SpellCheck(scrolled.ScrolledPanel):
def __init__(self, parent, root):
scrolled.ScrolledPanel.__init__(self, parent, -1)
self.document = None
self.root = root
self.funcdefs = {}
self.dcts = {}
self.start = 0
ws = wx.BoxSizer(wx.HORIZONTAL)
#-------------------------- Left button/pref column --------------------------
s = wx.BoxSizer(wx.VERTICAL)
s2 = wx.BoxSizer(wx.HORIZONTAL)
go = wx.Button(self, -1, "Check!")
bs = go.GetDefaultSize()[1]
go.SetToolTipString("Check spelling in the current document")
s2.Add(go, 0, wx.RIGHT, 4)
self.Bind(wx.EVT_BUTTON, self.OnSpellCheck, go)
checks = wx.Button(self, -1, "Check Sel")
checks.SetToolTipString("Check spelling in the selection of the current document")
s2.Add(checks, 0)
self.Bind(wx.EVT_BUTTON, self.OnSpellCheckSel, checks)
s.Add(s2, 0, wx.ALL, 2)
s2 = wx.BoxSizer(wx.HORIZONTAL)
self.ignore_funcdefs = wx.CheckBox(self, -1, "Ignore Defs")
self.ignore_funcdefs.SetToolTipString("If checked, will ignore all function definitions")
s2.Add(self.ignore_funcdefs, 0, wx.TOP|wx.BOTTOM, 6)
clear = wx.Button(self, -1, "Clear")
clear.SetToolTipString("Clear the results of the previous spell check")
s2.Add(clear, 0, wx.LEFT, 4)
self.Bind(wx.EVT_BUTTON, self.OnClear, clear)
s.Add(s2, 0, wx.ALL|wx.EXPAND, 2)
## ws.Add(s, 0, wx.ALL|wx.EXPAND, 2)
#------------------------ Custom dictionaries column -------------------------
s.Add(wx.StaticLine(self, -1, style=wx.LI_HORIZONTAL), 0, wx.EXPAND|wx.ALL, 4)
## s = wx.BoxSizer(wx.VERTICAL)
s2 = wx.BoxSizer(wx.HORIZONTAL)
s2.Add(wx.StaticText(self, -1, "Custom Dictionaries:"), 1, wx.RIGHT|wx.ALIGN_LEFT|wx.EXPAND, 4)
s2.Add(wx.StaticText(self, -1, ""), 0, wx.EXPAND)
addd = wx.Button(self, -1, "+", style=wx.BU_EXACTFIT)
addd.SetToolTipString("Add a new custom dictionary")
s2.Add(addd, 0, wx.RIGHT|wx.ALIGN_RIGHT, 4)
self.Bind(wx.EVT_BUTTON, self.OnAddD, addd)
deld = wx.Button(self, -1, "-", style=wx.BU_EXACTFIT)
deld.SetToolTipString("Remove a custom dictionary")
s2.Add(deld, 0, wx.ALIGN_RIGHT)
self.Bind(wx.EVT_BUTTON, self.OnDelD, deld)
s.Add(s2, 0, wx.ALL, 2)
s.Add(wx.StaticLine(self, -1, style=wx.LI_HORIZONTAL), 0, wx.EXPAND|wx.ALL, 2)
_ = list(self.root.config['DICTIONARIES'])
_.sort()
self.dictionaries = wx.CheckListBox(self, -1, choices=_)
s.Add(self.dictionaries, 1, wx.EXPAND)
ws.Add(s, 0, wx.ALL|wx.EXPAND, 2)
#---------------------------- Misspellings column ----------------------------
s = wx.BoxSizer(wx.VERTICAL)
s2 = wx.BoxSizer(wx.HORIZONTAL)
s2.Add(wx.StaticText(self, -1, "Possible Misspellings:"), 1, wx.ALIGN_LEFT|wx.EXPAND, 4)
cha = wx.Button(self, -1, "./ All", style=wx.BU_EXACTFIT)
cha.SetToolTipString("Make sure all of the 'Possible Misspellings' are checked")
s2.Add(cha, 0, wx.RIGHT, 4)
self.Bind(wx.EVT_BUTTON, self.OnCheckAll, cha)
add = wx.Button(self, -1, "+./", style=wx.BU_EXACTFIT)
add.SetToolTipString("Add the checked words to a custom dictionary")
s2.Add(add, 0)
self.Bind(wx.EVT_BUTTON, self.OnAdd, add)
ignore = wx.Button(self, -1, "- ./", style=wx.BU_EXACTFIT)
ignore.SetToolTipString("Ignore the checked words while this document stays open")
s2.Add(ignore, 0, wx.RIGHT, 4)
self.Bind(wx.EVT_BUTTON, self.OnIgnore, ignore)
s.Add(s2, 0, wx.ALL|wx.EXPAND, 2)
s.Add(wx.StaticLine(self, -1, style=wx.LI_HORIZONTAL), 0, wx.EXPAND|wx.ALL, 2)
self.badsp = wx.CheckListBox(self, -1, choices=[], style=wx.LB_SINGLE)
s.Add(self.badsp, 1, wx.EXPAND)
self.badsp.Bind(wx.EVT_LISTBOX, self.OnClick)
ws.Add(s, 1, wx.ALL|wx.EXPAND, 2)
#---------------------------- Corrections column -----------------------------
s = wx.BoxSizer(wx.VERTICAL)
s.Add(wx.StaticText(self, -1, "Possible Corrections:", size=(-1, bs)), 0, wx.ALL|wx.ALIGN_CENTER|wx.EXPAND, 2)
s.Add(wx.StaticLine(self, -1, style=wx.LI_HORIZONTAL), 0, wx.EXPAND|wx.ALL, 2)
self.replsp = FindPrefixListBox(self, -1, choices=[], style=wx.LB_SINGLE)
s.Add(self.replsp, 1, wx.EXPAND)
self.replsp.Bind(wx.EVT_LISTBOX, self.OnClickRepl)
ws.Add(s, 1, wx.ALL|wx.EXPAND, 2)
self.SetSizer(ws)
self.SetAutoLayout(1)
self.SetupScrolling()
wx.CallAfter(self.load_d)
def load_d(self):
rp = self.root.getglobal('runpath')
df = os.path.join(rp, 'dictionary.txt')
while os.path.exists(df):
try:
dct = open(df, 'rb').read()
except Exception, why:
self.root.SetStatusText("Dictionary load failed! %s"%why)
break
if dct.startswith('\xef\xbb\xbf'):
dct = dct[3:]
try:
dct = dct.decode('utf-8')
except:
self.root.SetStatusText("Dictionary load failed!")
traceback.print_exc()
break
x = dict.fromkeys(map(str, dct.split()))
_ = x.pop('', None)
if not UNICODE:
for i in x:
if type(i) is unicode:
self.root.SetStatusText("Dictionary load failed!")
self.root.SetStatusText("You must use a unicode-enabled PyPE/wxPython for the given dictionary")
break
dictionary.update(x)
af = os.path.join(rp, 'alphabet.txt')
while os.path.exists(af):
try:
ab = open(af, 'rb').read()
except Exception, why:
self.root.SetStatusText("Alphabet load failed! %s"%why)
break
if ab.startswith('\xef\xbb\xbf'):
ab = ab[3:]
try:
ab = ab.decode('utf-8')
except:
self.root.SetStatusText("Alphabet load failed!")
traceback.print_exc()
break
x = ''.join(ab.split())
x = dict.fromkeys([str(i).lower() for i in x.split(',') if i]).keys()
if not UNICODE:
for i in x:
if type(i) is unicode:
self.root.SetStatusText("Alphabet load failed!")
self.root.SetStatusText("You must use a unicode-enabled PyPE/wxPython for the given alphabet")
break
global alphabet
alphabet = tuple(x)
break
def OnCheckAll(self, evt):
for i in xrange(self.badsp.GetCount()):
self.badsp.Check(i)
def OnSpellCheckSel(self, evt):
self.OnSpellCheck(evt, 1)
def OnSpellCheck(self, evt, sel=0):
num, win = self.root.getNumWin(evt)
t = time.time()
if win != self.document:
self.document = win
self.funcdefs.clear()
if self.ignore_funcdefs.GetValue():
self.funcdefs = dict.fromkeys([i.lower() for i in win.tooltips])
else:
self.funcdefs.clear()
fd = self.funcdefs
_dict = dictionary
self.dcts = dcts = {}
d = self.root.config["DICTIONARIES"]
for i in xrange(self.dictionaries.GetCount()):
if self.dictionaries.IsChecked(i):
dcts.update(d[self.dictionaries.GetString(i)])
words = {}
if not sel:
doc = wx.stc.StyledTextCtrl.GetText(win)
self.start = 0
else:
doc = wx.stc.StyledTextCtrl.GetSelectedText(win)
self.start = min(win.GetSelection())
tt = transtable
try:
doc = doc.encode('ascii')
except:
tt = non_word
wrds = doc.translate(tt).split()
wc = len(wrds)
for i in wrds:
if i not in _dict and i not in fd and i not in win.ignore and i not in dcts:
words[i] = words.get(i, 0) + 1
del wrds
words = words.items()
words.sort()
self.OnClear(evt)
cnt = 0
for i,j in words:
if j > 1:
i = "%s (%i times)"%(i,j)
cnt += j
_ = self.badsp.Append(i)
## self.badsp.Check(self.badsp.GetCount()-1)
self.root.SetStatusText("Found %i misspellings of %i words in %i words in %.1f seconds"%(cnt, len(words), wc, time.time()-t))
def verify_and_select(self):
#select the document...
try:
self.document.ignore
for i,j in enumerate(self.root.control):
if j == self.document:
self.root.control.SetSelection(i)
break
except Exception, why:
print why
self.badsp.Clear()
self.replsp.Clear()
self.document = None
self.funcdefs.clear()
return 0
return 1
def verify_findbox(self, evt):
self.root.OnShowReplacebar(evt)
fb = self.document.GetParent().GetWindow2()
fb.case.SetValue(0)
fb.wrap.SetValue(1)
x = self.badsp.GetStringSelection().split()[0]
if fb.box1.GetValue() != x:
fb.box1.SetValue(x)
fb.box2.SetValue('')
fb.wholeword = 1
return fb
def OnClick(self, evt):
if not self.verify_and_select():
return
self.replsp.Clear()
x = evt.GetString().split()[0]
lst = suggest(x, (dictionary, self.funcdefs, self.dcts))
for i in lst:
_ = self.replsp.Append(i)
fb = self.verify_findbox(evt)
self.document.SetSelection(self.start, self.start)
fb.OnFindN(evt)
wx.CallAfter(self.badsp.SetFocus)
def OnIgnore(self, evt):
for i in xrange(self.badsp.GetCount()-1, -1, -1):
if self.badsp.IsChecked(i):
self.document.ignore[self.badsp.GetString(i).split()[0]] = None
self.badsp.Delete(i)
def OnClickRepl(self, evt):
if not self.verify_and_select():
return
fb = self.verify_findbox(evt)
fb.smartcase.SetValue(1)
fb.box2.SetValue(evt.GetString().split()[0])
def OnClear(self, evt):
self.badsp.Clear()
self.replsp.Clear()
def OnAdd(self, evt):
dct = self.root.config['DICTIONARIES']
k = list(dct)
k.sort()
dlg = wx.SingleChoiceDialog(self,
'Which dictionary do you want to add words to?', 'Which Dictionary?',
k, wx.CHOICEDLG_STYLE)
add = None
if dlg.ShowModal() == wx.ID_OK:
add = dlg.GetStringSelection()
dlg.Destroy()
if not add:
return
add = dct[add]
i = 0
for i in xrange(self.badsp.GetCount()-1, -1, -1):
if self.badsp.IsChecked(i):
add[self.badsp.GetString(i).split()[0]] = None
self.badsp.Delete(i)
def OnAddD(self, evt):
dlg = wx.TextEntryDialog(self, "Dictionary Name?", "What would you like your new dictionary to be named?", "")
resp = dlg.ShowModal()
valu = dlg.GetValue()
dlg.Destroy()
if resp != wx.ID_OK:
raise cancelled
dct = self.root.config['DICTIONARIES']
if valu in dct:
self.root.SetStatusText('Dictionary adding failed, dictionary %s already exists'%valu)
return
elif not valu.strip():
self.root.SetStatusText('Dictionary adding failed, cannot create unnamed dictionary')
return
dct[valu] = {}
#refresh the dictionary list...
insp = len([i for i in dct if i < valu])
self.dictionaries.Insert(valu, insp)
def OnDelD(self, evt):
k = list(self.root.config['DICTIONARIES'])
k.sort()
dlg = wx.SingleChoiceDialog(self,
'Which dictionary would\nyou like to delete?', 'Delete Dictionary?',
k, wx.CHOICEDLG_STYLE)
dele = None
if dlg.ShowModal() == wx.ID_OK:
dele = dlg.GetStringSelection()
dlg.Destroy()
if not dele:
return
dct = self.root.config['DICTIONARIES']
if dct[dele]:
#are you sure?
dlg = wx.MessageDialog(self, '''\
Are you sure you want to delete the custom dictionary: '%s'?
It has %i words.'''.replace(16*' ', '')%(dele, len(dct[dele])),
"Are you sure?", wx.OK|wx.CANCEL)
retr = dlg.ShowModal()
dlg.Destroy()
if retr != wx.ID_OK:
self.root.SetStatusText('Dictionary deletion cancelled')
return
del dct[dele]
dsp = len([i for i in dct if i < dele])
self.dictionaries.Delete(dsp)
#FindPrefixListBox derived from the wxPython demo
class FindPrefixListBox(wx.ListBox):
def __init__(self, parent, id=-1, choices=[], style=wx.LB_SINGLE):
wx.ListBox.__init__(self, parent, id, choices=choices, style=wx.LB_SINGLE)
self.typedText = ''
self.Bind(wx.EVT_KEY_DOWN, self.OnKey)
def FindPrefix(self, prefix):
self.log.WriteText('Looking for prefix: %s\n' % prefix)
if prefix:
prefix = prefix.lower()
length = len(prefix)
# Changed in 2.5 because ListBox.Number() is no longer supported.
# ListBox.GetCount() is now the appropriate way to go.
for x in xrange(self.GetCount()):
text = self.GetString(x)
text = text.lower()
if text[:length] == prefix:
return x
return -1
def OnKey(self, evt):
key = evt.GetKeyCode()
if key >= 32 and key <= 127:
self.typedText = self.typedText + chr(key)
item = self.FindPrefix(self.typedText)
if item != -1:
self.SetSelection(item)
elif key == wx.WXK_BACK: # backspace removes one character and backs up
self.typedText = self.typedText[:-1]
if not self.typedText:
self.SetSelection(0)
else:
item = self.FindPrefix(self.typedText)
if item != -1:
self.SetSelection(item)
else:
self.typedText = ''
evt.Skip()