Skip to content

Commit 848b95a

Browse files
committed
much simpler counting script
1 parent 29df2ad commit 848b95a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

source/count.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from collections import defaultdict
2+
import click
3+
4+
5+
@click.command()
6+
@click.argument("path", type=click.Path(exists=True))
7+
def main(path):
8+
"""
9+
Count the number of times each word appears in a file and print 10 most common.
10+
"""
11+
delimiters = ". , ; : ? $ @ ^ < > # % ` ! * - = ( ) [ ] { } / \" '".split()
12+
counts = defaultdict(int)
13+
14+
for line in open(path, "r").readlines():
15+
16+
# replace all delimiters with spaces
17+
for delimiter in delimiters:
18+
line = line.replace(delimiter, " ")
19+
20+
# split the lowercased line into words and increase count
21+
for word in line.lower().split():
22+
counts[word] += 1
23+
24+
# print 10 most common words
25+
for word, count in sorted(counts.items(), key=lambda x: x[1], reverse=True)[:10]:
26+
print(word, count)
27+
28+
29+
if __name__ == "__main__":
30+
main()

0 commit comments

Comments
 (0)