#!/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()