-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
40 lines (32 loc) · 1.02 KB
/
main.py
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 string
from collections import Counter
import matplotlib.pyplot as plt
import numpy as np
# %%
with open("corpus.txt", "r") as file:
content = file.read()
# %%
words = content.translate(str.maketrans("", "", string.punctuation)).split()
word_counts = Counter(words)
total_words = sum(word_counts.values())
relative_frequencies = {
word: count / total_words for word, count in word_counts.items()
}
# %%
sorted_counts = sorted(relative_frequencies.values(), reverse=True)
ranks = np.arange(1, len(sorted_counts) + 1)
plt.figure(figsize=(6, 6))
plt.scatter(ranks, sorted_counts, alpha=0.7, color="blue", s=10)
plt.xscale("log")
plt.yscale("log")
plt.xlabel("Word Rank")
plt.ylabel("Relative Frequency")
plt.title("Word Frequency Distribution (Zipf's Law)")
plt.xticks([1, 10, 100, 1000, 10000], labels=["1", "10", "100", "1000", "10000"])
plt.yticks(
[0.00001, 0.0001, 0.001, 0.01, 0.1],
labels=["0.00001", "0.0001", "0.001", "0.01", "0.1"],
)
plt.grid(True, which="both", linestyle="--", linewidth=0.5)
plt.show()