Files

39 lines
1.1 KiB
Python

#!/usr/bin/env python3
"""Quick word statistics analyzer - counts words, chars, lines in text files."""
import sys
from collections import Counter
def analyze_text(text):
words = text.split()
chars = len(text)
lines = text.count('\n') + 1 if text else 0
word_count = len(words)
char_count = len(text)
unique_words = len(set(w.lower().strip('.,!?;:') for w in words))
most_common = Counter(w.lower().strip('.,!?;:') for w in words).most_common(5)
return {
'lines': lines,
'words': word_count,
'characters': char_count,
'unique_words': unique_words,
'most_common': most_common
}
def main():
if len(sys.argv) < 2:
print("Usage: python word_stats.py <file>")
sys.exit(1)
with open(sys.argv[1], 'r') as f:
text = f.read()
stats = analyze_text(text)
print(f"Lines: {stats['lines']}")
print(f"Words: {stats['words']}")
print(f"Characters: {stats['characters']}")
print(f"Unique words: {stats['unique_words']}")
print(f"Most common: {stats['most_common']}")
if __name__ == '__main__':
main()