57 lines
1.3 KiB
Python
57 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
word_freq.py - Count word frequencies in text files.
|
|
Outputs JSON with word counts sorted by frequency.
|
|
"""
|
|
|
|
import json
|
|
import sys
|
|
import re
|
|
from pathlib import Path
|
|
from collections import Counter
|
|
|
|
|
|
def count_words(text):
|
|
"""Extract and count words from text."""
|
|
words = re.findall(r'\b[a-zA-Z]{2,}\b', text.lower())
|
|
return Counter(words)
|
|
|
|
|
|
def process_file(filepath):
|
|
"""Process a single file and return word counts."""
|
|
with open(filepath, 'r') as f:
|
|
text = f.read()
|
|
return count_words(text)
|
|
|
|
|
|
def main():
|
|
args = sys.argv[1:]
|
|
|
|
if not args or args[0] in ('-h', '--help'):
|
|
print("Usage: python3 word_freq.py <file1> [file2 ...]")
|
|
sys.exit(0)
|
|
|
|
all_counts = Counter()
|
|
|
|
for filepath in args:
|
|
p = Path(filepath)
|
|
if not p.exists():
|
|
print(f"ERROR: File not found: {filepath}", file=sys.stderr)
|
|
sys.exit(1)
|
|
all_counts.update(process_file(filepath))
|
|
|
|
# Get top 10 most common words
|
|
top_words = all_counts.most_common(10)
|
|
|
|
result = {
|
|
"total_files": len(args),
|
|
"total_words": sum(all_counts.values()),
|
|
"unique_words": len(all_counts),
|
|
"top_words": [{"word": w, "count": c} for w, c in top_words]
|
|
}
|
|
|
|
print(json.dumps(result, indent=2))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |