-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathCNAME
129 lines (107 loc) · 3.74 KB
/
CNAME
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
python.flypython.com
import os
import sys
from collections import Counter
def count_words(file_path):
try:
with open(file_path, 'r') as file:
text = file.read()
words = text.split()
word_counts = Counter(words)
return word_counts
except FileNotFoundError:
print(f"File not found: {file_path}")
return None
def print_word_counts(word_counts):
for word, count in word_counts.items():
print(f"{word}: {count}")
def save_word_counts(word_counts, output_file):
with open(output_file, 'w') as file:
for word, count in word_counts.items():
file.write(f"{word}: {count}\n")
print(f"Word counts saved to {output_file}")
def main():
if len(sys.argv) < 3:
print("Usage: python word_frequency_counter.py <file_path> <output_file>")
return
file_path = sys.argv[1]
output_file = sys.argv[2]
word_counts = count_words(file_path)
if word_counts:
print_word_counts(word_counts)
save_word_counts(word_counts, output_file)
if __name__ == "__main__":
main()
python.flypython.com
import os
import sys
from collections import Counter
import matplotlib.pyplot as plt
def count_words(file_path):
try:
with open(file_path, 'r') as file:
text = file.read()
words = text.split()
word_counts = Counter(words)
return word_counts
except FileNotFoundError:
print(f"File not found: {file_path}")
return None
def print_word_counts(word_counts):
for word, count in word_counts.items():
print(f"{word}: {count}")
def save_word_counts(word_counts, output_file):
with open(output_file, 'w') as file:
for word, count in word_counts.items():
file.write(f"{word}: {count}\n")
print(f"Word counts saved to {output_file}")
def plot_word_frequencies(word_counts):
words = list(word_counts.keys())
counts = list(word_counts.values())
plt.figure(figsize=(10, 5))
plt.bar(words, counts)
plt.xlabel('Words')
plt.ylabel('Frequencies')
plt.title('Word Frequency Distribution')
plt.xticks(rotation=90)
plt.tight_layout()
plt.savefig('word_frequencies.png')
plt.show()
print("Word frequency plot saved as 'word_frequencies.png'")
def main():
if len(sys.argv) < 3:
print("Usage: python word_frequency_counter.py <file_path> <output_file>")
return
file_path = sys.argv[1]
output_file = sys.argv[2]
word_counts = count_words(file_path)
if word_counts:
print_word_counts(word_counts)
save_word_counts(word_counts, output_file)
plot_word_frequencies(word_counts)
if __name__ == "__main__":
main()
def summarize_word_frequencies(word_counts, top_n=10):
most_common = word_counts.most_common(top_n)
print(f"\nTop {top_n} most common words:")
for word, count in most_common:
print(f"{word}: {count}")
def filter_short_words(word_counts, min_length=5):
filtered_counts = {word: count for word, count in word_counts.items() if len(word) >= min_length}
return filtered_counts
# Extended functionalities
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: python word_frequency_counter.py <file_path> <output_file>")
else:
file_path = sys.argv[1]
output_file = sys.argv[2]
word_counts = count_words(file_path)
if word_counts:
print_word_counts(word_counts)
save_word_counts(word_counts, output_file)
plot_word_frequencies(word_counts)
summarize_word_frequencies(word_counts)
filtered_counts = filter_short_words(word_counts)
print("\nFiltered word counts (words with 5+ characters):")
print_word_counts(filtered_counts)