~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/review-demo

A sample repository showing a pull with a review and line comments

0
0

Show the most common words too

Open chadtmiller.com wants to merge into main from chadtmiller.com/review-demo:top-words
README.md+3−1
@@ -3,5 +3,7 @@
A small word counter, here to show a pull with a review and line comments.
```sh
python3 wc.py notes.txt
python3 wc.py notes.txt 10
```
It prints how many words the file has, then its most common ones.
wc.py+9−0
@@ -1,15 +1,24 @@
import sys
from collections import Counter
def count_words(text):
return len(text.split())
def top_words(text, n=5):
counts = Counter(w.lower() for w in text.split())
return counts.most_common(n)
def main():
path = sys.argv[1]
n = int(sys.argv[2]) if len(sys.argv) > 2 else 5
with open(path) as f:
text = f.read()
print(count_words(text))
for word, count in top_words(text, n):
print(f"{count:>5} {word}")
if __name__ == "__main__":