forked from bamos/python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrank-writing.py
More file actions
executable file
·61 lines (53 loc) · 1.94 KB
/
Copy pathrank-writing.py
File metadata and controls
executable file
·61 lines (53 loc) · 1.94 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
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
#!/usr/bin/env python3
import argparse
import re
from subprocess import Popen,PIPE
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")
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 == "aspell":
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 == "diction":
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))