forked from Ekopalypse/NppPythonScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckUnUsualWords.py
More file actions
74 lines (55 loc) · 3.07 KB
/
CheckUnUsualWords.py
File metadata and controls
74 lines (55 loc) · 3.07 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
from Npp import notepad, editor, NOTIFICATION, SCINTILLANOTIFICATION, STATUSBARSECTION, MODIFICATIONFLAGS
import os
class WORD_CHECKER:
def __init__(self):
print('__init__')
self.report = ('Total: {0:<5} '
'Unique: {1:<5} '
'Total non-misspelled: {2:<5}({3:.1%}) '
'Total misspelled: {4:<4}({5:.1%}) '
'Unique misspelled: {6:<4}({7:.1%})')
editor.callbackSync(self.on_modified, [SCINTILLANOTIFICATION.MODIFIED])
notepad.callback(self.on_buffer_activated, [NOTIFICATION.BUFFERACTIVATED])
current_dict_path = os.path.join(notepad.getPluginConfigDir(), 'Hunspell')
current_dict_file = os.path.join(current_dict_path, 'ES-5000.dic')
with open(current_dict_file, 'r') as f:
self.current_dict = [word.decode('utf8')
for word in f.read().splitlines()[1:]] # skip length entry
self.DEBUG_MODE = False
self.on_buffer_activated({}) # must be last line here as it triggers check_words
def check_words(self):
words = []
def __get_words(m):
if m.group(2):
words.append(m.group(2).decode('utf8'))
editor.research('(^//.*)|([[:alpha:]]+(?=\h|[[:punct:]]|\R|\Z))', __get_words)
if self.DEBUG_MODE:
print(u'words contains:\n {}'.format(' '.join(words)))
error_words = [word.lower()
for word in words
if word.lower() not in self.current_dict and # insensitive word check
not word.isupper() # ignore all uppercase only words
]
if self.DEBUG_MODE:
print(u'error_words contains:\n {}'.format(' '.join(error_words)))
print(u'error_words unique contains:\n {}'.format(' '.join(set(error_words))))
total = len(words)
unique = len(set(words))
misspelled = len(error_words)
misspelled_unique = len(set(error_words))
notepad.setStatusBar(STATUSBARSECTION.DOCTYPE,
self.report.format(total,
unique,
total-misspelled, # non-misspelled
(float(total-misspelled) / total) if misspelled else 1, # non-misspelled %
misspelled,
(float(misspelled) / total) if misspelled else 0,
misspelled_unique,
(float(misspelled_unique) / total) if misspelled_unique else 0))
def on_modified(self, args):
if ((args['modificationType'] & MODIFICATIONFLAGS.INSERTTEXT) or
(args['modificationType'] & MODIFICATIONFLAGS.DELETETEXT)):
self.check_words()
def on_buffer_activated(self, args):
self.check_words()
WORD_CHECKER()