-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbargraph.py
More file actions
45 lines (32 loc) · 1.21 KB
/
bargraph.py
File metadata and controls
45 lines (32 loc) · 1.21 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
import matplotlib.pyplot as plt
import collections
import re
def read_text(file_path):
with open(file_path, "r", encoding="utf-8") as file:
text = file.read().lower() # Convert to lowercase
text = re.sub(r"[^a-z\s]", "", text)
words = text.split()
return words
# Load both texts
file1_words = read_text("corpora/lovecraftSentences.txt") # First text
file2_words = read_text("corpora/poeSentences.txt") # Second text
# Count word frequency
file1_freq = collections.Counter(file1_words)
file2_freq = collections.Counter(file2_words)
# Find common words
common_words = set(file1_freq.keys()).intersection(set(file2_freq.keys()))
# Prepare for plotting
words_to_plot = list(common_words)[:10] # 10 words
file1_counts = [file1_freq[word] for word in words_to_plot]
file2_counts = [file2_freq[word] for word in words_to_plot]
# Plot comparison
plt.figure(figsize=(10, 5))
plt.bar(words_to_plot, file1_counts, alpha=0.6, label="Colour Out of Space")
plt.bar(words_to_plot, file2_counts, alpha=0.6, label="Another Text", color="purple")
plt.xlabel("Words")
plt.ylabel("Frequency")
plt.title("Word Frequency Comparison")
plt.legend()
plt.show()
# Save plot to a file
plt.savefig("docs/images/plot.png")