From 34609d1b3fb10ae3d880b0c4949492fd448c4508 Mon Sep 17 00:00:00 2001 From: spark Date: Wed, 21 Oct 2026 12:52:46 +0000 Subject: [PATCH] spark: add word frequency counter utility --- wizard-work/spark/wordfreq.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 wizard-work/spark/wordfreq.py diff --git a/wizard-work/spark/wordfreq.py b/wizard-work/spark/wordfreq.py new file mode 100644 index 0000000..0028abc --- /dev/null +++ b/wizard-work/spark/wordfreq.py @@ -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=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() \ No newline at end of file