-
Notifications
You must be signed in to change notification settings - Fork 0
/
docsright.py
699 lines (555 loc) · 18.8 KB
/
docsright.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
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
"""Spell checker for comments in Python code.
It supports two types of file:
* `.md`: markdown files
* `.py`: python files
* `.ipynb`: jupyter notebook files
In markdown files, it treats all lines,
in python files only the lines in docstrings,
in notebook files only the markdown contained in markdown cells.
All strings that belong to code or math are replaces by `CODE` and `MATH`, resp.
Also other strings that may lead to false positives are removed, such as
hyperlinks and path names.
The resulting material is separated into words, and these are fed to a
spell checker.
The wrong words are listed in 2 files, from which it is easy to correct mistakes
in the original and to add words to the allowed words.
The program works on the basis of a specific project, a directory under
`projects` in this repo.
That directory should contain a file `tasks.txt` that specifies a number of
input directories to check.
The output will end up in the same project directory, in the files
* `summary.txt`
Contains the mistakes in the form
```
[wrong]=>[correction]
```
!!! hint "Vim macros"
If you open this file and next to it the file `allowed.txt`,
you can record a Vim macro that adds a wrong word to the file with
allowed words, then deletes the wrong word from `summary.txt` and moves the
cursor to the next word. If you assing this macro to the letter `g`,
you can press `@g` repeatedly to change the status of wrong words to
allowed.
* `locations.txt`
Contains the mistakes in the form
```
[wrong]=>[correction] [path] [section]
```
!!! hint "Vim macros"
If you open this file and next to it an arbitrary file
you can record a vim macro that stores the wrong, correction, path and section
parts into registers (say `w`, `c`, `f`, `p` respectively),
and then moves to the other window, opens the file
indicated with `path` and searches for `wrong`.
If you want to replace the word by the suggestion, you can type
`cw<Ctrl r>c<Esc> to do that. Moreover,
You can type `/<Ctr r>p` to search for the specific section if needed.
"""
import sys
import ast
import re
from nbformat import read as nbRead, NO_CONVERT
from symspellpy import SymSpell, Verbosity
from tf.core.files import (
fileExists,
initTree,
isFile,
isDir,
dirAllFiles,
expanduser as ex,
unexpanduser as ux,
abspath,
)
from tf.core.helpers import console
HELP = """
USAGE
./docsright.sh project
where
project is a directory under projects in this repo.
"""
TTTICK_RE = re.compile(
r"""
```
.*?
```
""",
re.S | re.X,
)
TTICK_RE = re.compile(r"""``[^`\n]*``""")
TICK_RE = re.compile(r"""`[^`]*`""")
MATH_RE = re.compile(r"""\$[^$\n]+\$""", re.S)
MATHD_RE = re.compile(r"""\$\$.+?\$\$""", re.S)
MATHA_RE = re.compile(r"""\\\(.+?\\\)""", re.S)
BRACES_RE = re.compile(r"""\{\{.+?\}\}""", re.S)
X_ENTS = re.compile(r"""&(?:lt|gt|amp|quot|apos|nbsp);""")
OLD_TTICK_RE = re.compile(
r"""
(?:^|\n)
\s*
```
(?:\s*[a-z]+\s*)?
\s*\n
.*?
\n
\s*
```
\s*
(?:\n|$)
""",
re.S | re.X,
)
ELEM_DEL1_RE = re.compile(r"""<(td|th|pre|code)\b[^>]*>.*?</\1>""", re.S | re.I)
ELEM_DEL2_RE = re.compile(r"""<(style|script|span)\b[^>]*>.*?</\1>""", re.S | re.I)
ELEM_UNWRAP_RE = re.compile(r"""</?(?:[a-z]+[0-9]*)\b[^>]*>""", re.S | re.I)
CMT_RE = re.compile(r"""<!--.*?-->""", re.S)
INITIALS_RE = re.compile(r"""^[A-Z][a-z]?$""")
HEX_RE = re.compile(r"""^[a-f0-9]+$""", re.I)
NUM_PL = re.compile(r"""^[0-9]+s$""", re.I)
SIZES_RE = re.compile(
r"""
^
[0-9][0-9.]*
(?:
x
[0-9]*
)?
(?:
th|
st|
nd|
rd|
pt|
ppi|
(?:
[KMGTP]B?
)
)?
$
""", re.I | re.X)
SYM_RE = re.compile(r"""«[a-zA-Z0-9_]*»""")
PARAM_RE = re.compile(
r"""
^
[a-zA-Z0-9_]+
(?:
,
\s*
[a-zA-Z0-9]+
)*
:
\s+
(?:
string|
boolean|
integer|
float|
tuple|
list|
dict|
function|
set|
frozenset|
iterable|
object|
mixed |
void |
AttrDict |
np\ array |
image\ as\ np\ array
)
""",
re.M | re.X,
)
RETURN_RE = re.compile(
r"""
^
\s*
(?:
string|
boolean|
integer|
tuple|
list|
dict|
function|
set|
frozenset|
iterable|
object|
mixed |
AttrDict
)
\s*
$
""",
re.M | re.X,
)
DOTNAME_RE = re.compile(
r"""
\b
[a-zA-Z0-9_-]+
(?:
[./]
[a-zA-Z0-9_-]+
)+
\b
""",
re.X,
)
ALINK_RE = re.compile(r"""\[[^]]+\]\[[^]]+\]""")
ILINK_RE = re.compile(r"""!\[[^]]+\]\([^)]+\)""")
LINK_RE = re.compile(r"""\[([^]]+)\]\([^)]+\)""")
NLINK_RE = re.compile(r"""https?://[a-z0-9_./-]*""")
HEADING_RE = re.compile(r"""(?:^|")#+\s+(.*?)\s*(?:$|\\n)""", re.M)
HEAD_COLON_RE = re.compile(r"""^[ #]*:::\s*.*$""", re.M)
PNAME_RE = re.compile(r"""^[a-zA-Z0-9~_.()-]+$""")
def linkRepl(match):
body = match.group(1)
result = " LINK " if PNAME_RE.match(body) else body
return result
def headRepl(match):
title = match.group(1)
result = " HEAD " if PNAME_RE.match(title) else title
return result
WORD_RE = re.compile(r"""\w+""")
SUMMARY_FILE = "summary.txt"
LOCATIONS_FILE = "locations.txt"
class Spell:
def __init__(self, project, task=None):
console("Warming up")
self.project = project
self.task = task
projectDir = f"projects/{project}"
initTree(projectDir, fresh=False)
self.projectDir = projectDir
symspell = SymSpell(2, 7)
dictionaryPath = "frequency_dictionary_en_82_765.txt"
bigramPath = "frequency_bigramdictionary_en_243_342.txt"
symspell.load_dictionary(dictionaryPath, 0, 1)
symspell.load_bigram_dictionary(bigramPath, 0, 2)
self.symspell = symspell
with open(f"{projectDir}/allowed.txt") as fh:
self.allowed = {x for line in fh if (x := line.strip())}
with open(f"{projectDir}/allowed.txt", "w") as fh:
text = "\n".join(sorted(self.allowed, key=lambda x: x.lower()))
fh.write(f"{text}\n")
ignoreDirs = []
ignoreFiles = []
with open(f"{projectDir}/xxxDir.txt") as fh:
ignoreDirs = [x for line in fh if (x := line.strip())]
with open(f"{projectDir}/xxxFile.txt") as fh:
ignoreFiles = [re.compile(x) for line in fh if (x := line.strip())]
self.ignoreDirs = ignoreDirs
self.ignoreFiles = ignoreFiles
self.words = {}
self.wrong = {}
self.stats = {}
self.messages = {}
def checkAll(self):
ignoreDirs = self.ignoreDirs
ignoreFiles = self.ignoreFiles
project = self.project
givenTask = self.task
projectDir = self.projectDir
taskFile = f"{projectDir}/tasks.txt"
stats = self.stats
words = self.words
wrong = self.wrong
messages = self.messages
incomingTotal = 0
filteredTotal = 0
filesTotal = 0
if not fileExists(taskFile):
console(f"No task.txt file in project {project}. Nothing to do!")
return
console("Reading all tasks", newline=False)
with open(taskFile) as fh:
tasks = [x for t in fh if not (x := t.strip()).startswith("#")]
self.tasks = []
for t, task in enumerate(tasks):
if givenTask is not None and t + 1 != givenTask:
continue
src = abspath(ex(task)).removesuffix("/")
srcx = ux(src)
srcIsFile = isFile(src)
srcIsDir = isDir(src)
theseStats = {}
stats[task] = theseStats
if not (srcIsFile or srcIsDir):
messages.setdefault(task, []).append(
"not an existing file or directory"
)
continue
srcFiles = [
x.removeprefix(src).removeprefix("/")
for x in dirAllFiles(src, ignore=ignoreDirs)
if (x.endswith(".py") or x.endswith(".ipynb") or x.endswith(".md"))
and not any(pat.search(x) for pat in ignoreFiles)
]
self.tasks.append(srcx)
console(f" {len(srcFiles)}", newline=False)
incoming = 0
filtered = 0
files = len(srcFiles)
for srcFile in srcFiles:
(inc, filt) = self.operation(srcx, srcFile)
incoming += inc
filtered += filt
theseStats["files"] = files
theseStats["incoming"] = incoming
theseStats["filtered"] = filtered
incomingTotal += incoming
filteredTotal += filtered
filesTotal += files
tasks = self.tasks
task = "TOTAL"
theseStats = {}
stats[task] = theseStats
theseStats["files"] = filesTotal
theseStats["incoming"] = incomingTotal
theseStats["filtered"] = filteredTotal
console(" done")
console("Performing spellcheck on all tasks ...")
self.check()
console("Delivering results of all tasks ...")
self.deliver()
console(
f"{'n':>3} | {'task':<40} | {'files':>5} | {'lines':>6} | {'text':>6} "
f"| {'words':>5} | {'wrong':>5} | {'occs':>6}"
)
sepLine = (
f"{'-' * 3} | {'-' * 40} | {'-' * 5} | {'-' * 6} | {'-' * 6} "
f"| {'-' * 5} | {'-' * 5} | {'-' * 6}"
)
console(sepLine)
for i, task in enumerate(tasks + ["TOTAL"]):
theseStats = stats[task]
files = theseStats["files"]
incoming = theseStats["incoming"]
filtered = theseStats["filtered"]
total = 0
wrongs = 0
locations = 0
isTotal = task == "TOTAL"
if isTotal:
console(sepLine)
for word in words:
info = words[word]
if not isTotal and task not in info:
continue
total += 1
if word in wrong:
wrongs += 1
occs = list(info.values()) if isTotal else [info[task]]
for theseOccs in occs:
for names in theseOccs.values():
locations += len(names)
iRep = "" if isTotal else (i + 1)
console(
f"{iRep:>3} | {task:<40} | {files:>5} | {incoming:>6} | {filtered:>6} "
f"| {total:>5} | {wrongs:>5} | {locations:>6}"
)
if len(messages):
console("Errors and warnings:")
for task, msgs in messages.items():
console(f"{task}:")
for msg in msgs:
console(msg)
console("")
else:
console("All went well.")
def operation(self, srcx, srcFile):
isPy = srcFile.endswith(".py")
isMd = srcFile.endswith(".md")
isIpynb = srcFile.endswith(".ipynb")
if not isPy and not isMd and not isIpynb:
return (0, 0)
srcFileF = f"{ex(srcx)}/{srcFile}"
messages = self.messages
chunks = {}
with open(srcFileF) as fh:
incoming = 0
filtered = 0
if isMd:
dstLines = []
for line in fh:
incoming += 1
dstLines.append(apply(line))
chunks[""] = dstLines
if isIpynb:
try:
notebook = nbRead(fh, NO_CONVERT)
except Exception as e:
messages.setdefault(srcx, []).append(
f"{srcFile}: reading problem: {str(e)}"
)
return (0, 0)
cells = notebook["cells"]
for cellNr, cell in enumerate(cells):
if cell["cell_type"] != "markdown":
continue
dstLines = []
md = cell["source"]
for line in md.split("\n"):
incoming += 1
dstLines.append(apply(line))
chunks[cellNr] = dstLines
if isPy:
text = fh.read()
try:
code = ast.parse(text)
except Exception as e:
messages.setdefault(srcx, []).append(
f"{srcFile}: could not parse: {str(e)}"
)
return (0, 0)
for node in ast.walk(code):
tp = type(node)
isMod = tp is ast.Module
isClass = tp is ast.ClassDef
isFunc = tp is ast.FunctionDef
if not (isMod or isClass or isFunc):
continue
kind = (
""
if isMod
else f"class {node.name}"
if isClass
else f"def {node.name}"
)
try:
docstring = ast.get_docstring(node)
except Exception as f:
messages.setdefault(srcx, []).append(
f"{srcFile}:{kind} docstring problem: {str(f)}"
)
break
if docstring:
dstLines = []
lines = docstring.split("\n")
for line in lines:
incoming += 1
dstLines.append(apply(line))
chunks[kind] = dstLines
words = self.words
for name, dstLines in chunks.items():
text = "\n".join(dstLines)
text = CMT_RE.sub(" ", text)
text = ELEM_DEL1_RE.sub(" ", text)
text = HEAD_COLON_RE.sub(" REFERENCE ", text)
text = HEADING_RE.sub(headRepl, text)
text = ALINK_RE.sub(r" LINK ", text)
text = ILINK_RE.sub(r"LINK ", text)
text = LINK_RE.sub(linkRepl, text)
text = NLINK_RE.sub(r" LINK ", text)
text = TTTICK_RE.sub("\nCODE\n", text)
text = MATHD_RE.sub(" MATH ", text)
text = MATHA_RE.sub(" MATH ", text)
text = MATH_RE.sub(" MATH ", text)
text = BRACES_RE.sub(" VAR ", text)
text = X_ENTS.sub("", text)
good = True
for i, line in enumerate(text.split("\n")):
tc = line.count("`")
if tc % 2 == 1:
good = False
messages.setdefault(srcx, []).append(
f"{srcFile}:{i + 1} odd number ({tc}) of ticks in: {line}"
)
if good:
text = TTICK_RE.sub(" CODE ", text)
text = TICK_RE.sub(" CODE ", text)
else:
messages.setdefault(srcx, []).append(
f"{srcFile}: No `code` replacement because of unmatched `s"
)
text = SYM_RE.sub("", text)
text = ELEM_DEL2_RE.sub(" ", text)
text = ELEM_UNWRAP_RE.sub(" ", text)
for line in text.split("\n"):
line = line.strip()
if line == "":
continue
filtered += 1
for word in {word for word in WORD_RE.findall(line)}:
if (
word.isnumeric()
or HEX_RE.match(word)
or NUM_PL.match(word)
or SIZES_RE.match(word)
or INITIALS_RE.match(word)
):
continue
words.setdefault(word, {}).setdefault(srcx, {}).setdefault(
srcFile, set()
).add(name)
return (incoming, filtered)
def check(self):
symspell = self.symspell
allowed = self.allowed
words = self.words
wrong = self.wrong
for word in sorted(words):
if word in allowed:
continue
suggestions = list(
symspell.lookup(
word, Verbosity.TOP, max_edit_distance=2, transfer_casing=True
)
)
corr = "XX" if len(suggestions) == 0 else suggestions[0].term
if len(suggestions) == 0 or word != corr:
wrong[word] = corr
def deliver(self):
projectDir = self.projectDir
words = self.words
wrong = self.wrong
summaryFile = f"{projectDir}/{SUMMARY_FILE}"
locationsFile = f"{projectDir}/{LOCATIONS_FILE}"
self.lastSrcx = None
with open(summaryFile, "w") as sh:
with open(locationsFile, "w") as lh:
for word in sorted(wrong, key=lambda x: x.lower()):
self.deliverOccs(word, words[word], lh)
sh.write(f"{word}\n")
lh.write("\n")
def deliverOccs(self, word, occs, fh):
lastSrcx = self.lastSrcx
for srcx, info in occs.items():
if srcx != lastSrcx:
fh.write(f"={srcx}\n")
lastSrcx = srcx
for fl, names in sorted(info.items()):
for name in sorted(names):
fh.write(
f"{word}|{fl}|{str(name) if type(name) is int else (name or 'e')}\n"
)
self.lastSrcx = lastSrcx
def apply(line):
line = line.strip()
if RETURN_RE.match(line) or PARAM_RE.match(line):
line = ""
elif line.startswith("!!! "):
line = line[4:]
line = DOTNAME_RE.sub(" ", line)
return line
def main(cargs=sys.argv[1:]):
if len(cargs) not in {1, 2}:
console(HELP)
console(f"{len(cargs)} argument(s) passed, instead of 1 or 2")
return -1
project = cargs[0]
if len(cargs) == 2:
task = cargs[1]
if task.isdecimal() and not task.startswith("0"):
task = int(task)
else:
console(f"Invalid task task number: {task}")
return -1
else:
task = None
S = Spell(project, task=task)
S.checkAll()
return 0
if __name__ == "__main__":
sys.exit(main())