spark: add word frequency counter utility

This commit is contained in:
spark committed 2026-10-21 12:52:46 +00:00
1 parent 214e10b530
commit 34609d1b3f
1 file changed
+26
+26
View File
@@ -0,0 +1,26 @@
#!/usr/bin/env python3
"""Word frequency counter - counts and sorts unique words in text files."""
import sys
from collections import Counter
import re
def count_words(text):
"""Extract words and count frequencies."""
words = re.findall(r"\b[a-zA-Z]+'?[a-zA-Z]*\b", text.lower())
return Counter(words)
def main():
if len(sys.argv) < 2:
print("Usage: wordfreq.py <file>", file=sys.stderr)
sys.exit(1)
with open(sys.argv[1], 'r') as f:
text = f.read()
counts = count_words(text)
for word, count in sorted(counts.items(), key=lambda x: (-x[1], x[0])):
print(f"{count:6d} {word}")
if __name__ == '__main__':
main()