koch-method-real-words/count_letters.py

40 wiersze
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import sys
def count(infile, outfile):
result = {}
for _,c in enumerate("abcdefghijklmnopqrstuvwxyz"):
result[c] = 0
for word in infile:
for c in set(word):
if c != "\n":
result[c] += 1
for c, count in sorted([[k,v] for k,v in result.items()],
key=lambda kv: kv[1],
reverse=True):
outfile.write("{}: {}\n".format(c,count))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Count, for each letter, in how many words it occurs.")
parser.add_argument("--in",
dest="infile",
default="wordlist.txt",
type=argparse.FileType("r", encoding="UTF-8"),
help="wordlist.txt file as generated by generate-wordlist")
parser.add_argument("--out",
type=argparse.FileType("w", encoding="UTF-8"),
required=False,
default="lettercount.txt",
help="The output file with letters and counts.")
args = parser.parse_args()
count(args.infile, args.out)