-
Notifications
You must be signed in to change notification settings - Fork 105
/
rank-writing.py
executable file
·106 lines (86 loc) · 2.92 KB
/
rank-writing.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
#!/usr/bin/env python3
__author__ = ['[Brandon Amos](http://bamos.github.io)']
__date__ = '2014.02.14'
"""
`rank-writing.py` ranks the writing quality of my
blog's Markdown posts and my project's Markdown README files.
The following programs should be on your `PATH`:
+ [aspell](http://aspell.net/)
+ [write-good](https://github.com/btford/write-good)
+ [diction](https://www.gnu.org/software/diction/)
```
$ rank-writing.py *.md
=== 2013-05-03-scraping-tables-python.md ===
Total: 53
├── aspell: 34
├── diction: 0
└── write-good: 19
...
=== 2013-04-16-pdf-from-plaintext.md ===
Total: 0
├── aspell: 0
├── diction: 0
└── write-good: 0
```
"""
import argparse
import re
from subprocess import Popen, PIPE
import shutil
import sys
parser = argparse.ArgumentParser()
parser.add_argument('--aspell', action='store_true')
parser.add_argument('--diction', action='store_true')
parser.add_argument('--write-good', action='store_true')
parser.add_argument('files', type=str, nargs='+')
args = parser.parse_args()
# If no arguments are provided, show everything.
if not (args.aspell or args.diction or args.write_good):
args.aspell = args.diction = args.write_good = True
programs = []
if args.aspell:
programs.append("aspell")
if args.diction:
programs.append("diction")
if args.write_good:
programs.append("write-good")
for program in programs:
if not shutil.which(program):
print("Error: '{}' not found on PATH. Please install.".format(program))
sys.exit(-1)
def call(cmd, in_f=None):
if in_f:
with open(in_f, 'r') as f:
p = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=f)
else:
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
out = p.communicate()[0].decode()
if p.returncode != 0:
raise Exception("Error running {}".format(cmd))
return out
def getNumSuggestions(program, f_name):
if program == "write-good":
out = call(["write-good", f_name])
return out.count("-------------")
elif program == "diction":
out = call(["diction", "--suggest", f_name])
r = re.search("(\S*) phrases? in (\S*) sentences? found.", out)
if r:
if r.group(1) == "No":
return 0
else:
return int(r.group(1))
elif program == "aspell":
out = call(["aspell", "list"], in_f=f_name)
return len(out.split())
else:
raise Exception("Unrecognized program: {}".format(program))
def getSuggestions(f_name):
suggestions = list(map(lambda x: getNumSuggestions(x, f_name), programs))
return (sum(suggestions), suggestions, f_name)
for tup in sorted(map(getSuggestions, args.files), reverse=True):
print("\n=== {} ===".format(tup[2]))
print(" Total: {}".format(tup[0]))
div = ["├──"] * (len(programs) - 1) + ["└──"]
for program_output in list(zip(div, programs, tup[1])):
print(" {} {}: {}".format(*program_output))