Skip to content

Commit

Permalink
much simpler counting script
Browse files Browse the repository at this point in the history
  • Loading branch information
bast committed Mar 27, 2023
1 parent 29df2ad commit 848b95a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions source/count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from collections import defaultdict
import click


@click.command()
@click.argument("path", type=click.Path(exists=True))
def main(path):
"""
Count the number of times each word appears in a file and print 10 most common.
"""
delimiters = ". , ; : ? $ @ ^ < > # % ` ! * - = ( ) [ ] { } / \" '".split()
counts = defaultdict(int)

for line in open(path, "r").readlines():

# replace all delimiters with spaces
for delimiter in delimiters:
line = line.replace(delimiter, " ")

# split the lowercased line into words and increase count
for word in line.lower().split():
counts[word] += 1

# print 10 most common words
for word, count in sorted(counts.items(), key=lambda x: x[1], reverse=True)[:10]:
print(word, count)


if __name__ == "__main__":
main()

0 comments on commit 848b95a

Please sign in to comment.