-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspellcheck.py
executable file
·43 lines (38 loc) · 1.44 KB
/
spellcheck.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
#!/usr/bin/env python
import json
import sys
import errno
from collections import OrderedDict
from enchant.checker import SpellChecker
from enchant.tokenize import EmailFilter, URLFilter
def spellcheck(pkg):
"""Return a list of documentation text with potential misspells."""
chkr = SpellChecker("en_US", filters=[EmailFilter, URLFilter])
misspelled_documentation = []
for ident in pkg.get("Identifiers") or []:
chkr.ignore_always(ident)
for text in pkg.get("Documentation") or []:
chkr.set_text(text.get("Content", ""))
spelling_errors = [OrderedDict([
("Word", c.word),
("Offset", c.wordpos),
("Suggestions", c.suggest()),
]) for c in chkr]
if spelling_errors:
text["Misspellings"] = spelling_errors
misspelled_documentation.append(text)
return misspelled_documentation
if __name__ == "__main__":
for line in sys.stdin:
pkg = json.loads(line, object_pairs_hook=OrderedDict)
misspelled_documentation = spellcheck(pkg)
if misspelled_documentation:
try:
print json.dumps(OrderedDict([
("PackageName", pkg.get("PackageName", "")),
("Documentation", misspelled_documentation),
]), separators=(",", ":"))
except IOError as error:
if error.errno == errno.EPIPE:
break
raise