forked from learnbyexample/practice_python_projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.py
More file actions
33 lines (29 loc) · 1.02 KB
/
markdown.py
File metadata and controls
33 lines (29 loc) · 1.02 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
import re
from string import punctuation
def spell_check(words, text):
for w in text.split():
w = w.strip(punctuation + '—')
if w and w.lower() not in words:
yield w
def process_md(words, md_file):
links = re.compile(r'\[([^]]+)\]\([^)]+\)')
inline_code = re.compile(r'`[^`]+`')
hist = {}
code_block = False
with open(md_file) as f:
for line in f:
if line.startswith('```'):
code_block = not code_block
elif not code_block:
line = links.sub(r'\1', line)
line = inline_code.sub('', line)
for w in spell_check(words, line):
hist[w] = hist.get(w, 0) + 1
return hist
if __name__ == '__main__':
word_file = 'word_files/words.txt'
with open(word_file) as f:
words = {line.rstrip().lower() for line in f}
hist = process_md(words, 'md_files/sample.md')
for k in sorted(hist, key=lambda k: (k.lower(), -hist[k])):
print(f'{k}: {hist[k]}')