Skip to content

Commit 009f5e1

Browse files
committed
Merge branch 'master' of github.com:bamos/python-scripts
2 parents fdb8f2f + c537660 commit 009f5e1

1 file changed

Lines changed: 33 additions & 24 deletions

File tree

python3/rank-writing.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,21 @@
55
from subprocess import Popen,PIPE
66

77
parser = argparse.ArgumentParser()
8+
parser.add_argument('--aspell',action='store_true')
9+
parser.add_argument('--diction',action='store_true')
10+
parser.add_argument('--write-good',action='store_true')
811
parser.add_argument('files',type=str, nargs='+')
912
args = parser.parse_args()
1013

14+
# If no arguments are provided, show everything.
15+
if not (args.aspell or args.diction or args.write_good):
16+
args.aspell = args.diction = args.write_good = True
17+
18+
programs = []
19+
if args.aspell: programs.append("aspell")
20+
if args.diction: programs.append("diction")
21+
if args.write_good: programs.append("write-good")
22+
1123
def call(cmd,in_f=None):
1224
if in_f:
1325
with open(in_f,'r') as f:
@@ -19,34 +31,31 @@ def call(cmd,in_f=None):
1931
raise Exception("Error running {}".format(cmd))
2032
return out
2133

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())
34+
def getNumSuggestions(program,f_name):
35+
if program == "write-good":
36+
out = call(["write-good",f_name])
37+
return out.count("-------------")
38+
elif program == "aspell":
39+
out = call(["diction","--suggest",f_name])
40+
r = re.search("(\S*) phrases? in (\S*) sentences? found.",out)
41+
if r:
42+
if r.group(1) == "No":
43+
return 0
44+
else:
45+
return int(r.group(1))
46+
elif program == "diction":
47+
out = call(["aspell","list"],in_f=f_name)
48+
return len(out.split())
49+
else:
50+
raise Exception("Unrecognized program: {}".format(program))
3851

3952
def getSuggestions(f_name):
40-
suggestions = [
41-
getNumDictionSuggestions(f_name),
42-
getNumWriteGoodSuggestions(f_name),
43-
getNumAspellSuggestions(f_name)
44-
]
53+
suggestions = list(map(lambda x: getNumSuggestions(x,f_name),programs))
4554
return (sum(suggestions), suggestions, f_name)
4655

4756
for tup in sorted(map(getSuggestions,args.files),reverse=True):
4857
print("\n=== {} ===".format(tup[2]))
4958
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]))
59+
div = ["├──"]*(len(programs)-1) + ["└──"]
60+
for program_output in list(zip(div,programs,tup[1])):
61+
print(" {} {}: {}".format(*program_output))

0 commit comments

Comments
 (0)