Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (41 loc) · 1.41 KB
/
Copy pathmain.py
File metadata and controls
51 lines (41 loc) · 1.41 KB
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
import re
from collections import Counter
import matplotlib.pyplot as plt
def clean_text(text):
# Remove punctuation and numbers, convert to lowercase
cleaned_text = re.sub(r'[^a-zA-Z\s]', '', text)
return cleaned_text.lower()
def word_frequency(text):
cleaned_text = clean_text(text)
words = cleaned_text.split()
return Counter(words)
def most_common_words(text, n):
cleaned_text = clean_text(text)
words = cleaned_text.split()
word_counts = Counter(words)
return word_counts.most_common(n)
def plot_word_frequency(word_freq):
words, frequencies = zip(*word_freq)
plt.bar(words, frequencies)
plt.xlabel('Words')
plt.ylabel('Frequency')
plt.title('Top 10 Most Common Words')
plt.xticks(rotation=45, ha='right')
plt.show()
if __name__ == "__main__":
# Read text from a file
file_path = "sample_text.txt" # Change to the path of your text file
with open(file_path, 'r', encoding='utf-8') as file:
text_content = file.read()
# Word frequency analysis
word_freq_dict = word_frequency(text_content)
# Top 10 most common words
top_words = most_common_words(text_content, 10)
# Display results
print("Word Frequency Dictionary:")
print(word_freq_dict)
print("\nTop 10 Most Common Words:")
for word, freq in top_words:
print(f"{word}: {freq}")
# Plotting the word frequency
plot_word_frequency(top_words)