Skip to content

Commit af2647e

Browse files
committed
Initial commit of rank-writing.py.
1 parent 19474fc commit af2647e

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

python3/rank-writing.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
import re
5+
from subprocess import Popen,PIPE
6+
7+
parser = argparse.ArgumentParser()
8+
parser.add_argument('files',type=str, nargs='+')
9+
args = parser.parse_args()
10+
11+
def call(cmd,in_f=None):
12+
if in_f:
13+
with open(in_f,'r') as f:
14+
p = Popen(cmd,stdout=PIPE,stderr=PIPE,stdin=f)
15+
else:
16+
p = Popen(cmd,stdout=PIPE,stderr=PIPE)
17+
out = p.communicate()[0].decode()
18+
if p.returncode != 0:
19+
raise Exception("Error running {}".format(cmd))
20+
return out
21+
22+
def getNumWriteGoodSuggestions(f_name):
23+
out = call(["write-good",f_name])
24+
return out.count("-------------")
25+
26+
def getNumDictionSuggestions(f_name):
27+
out = call(["diction","--suggest",f_name])
28+
r = re.search("(\S*) phrases? in (\S*) sentences? found.",out)
29+
if r:
30+
if r.group(1) == "No":
31+
return 0
32+
else:
33+
return int(r.group(1))
34+
35+
def getNumAspellSuggestions(f_name):
36+
out = call(["aspell","list"],in_f=f_name)
37+
return len(out.split())
38+
39+
def getSuggestions(f_name):
40+
suggestions = [
41+
getNumDictionSuggestions(f_name),
42+
getNumWriteGoodSuggestions(f_name),
43+
getNumAspellSuggestions(f_name)
44+
]
45+
return (sum(suggestions), suggestions, f_name)
46+
47+
for tup in sorted(map(getSuggestions,args.files),reverse=True):
48+
print("\n=== {} ===".format(tup[2]))
49+
print(" Total: {}".format(tup[0]))
50+
print(" ├── diction: {}".format(tup[1][0]))
51+
print(" ├── write-good: {}".format(tup[1][1]))
52+
print(" └── aspell: {}".format(tup[1][2]))

0 commit comments

Comments
 (0)