-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add parameter report for collection. - Add Zipfs law plot to graph.
- Loading branch information
1 parent
5b25a32
commit 118e120
Showing
4 changed files
with
44 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,47 @@ | ||
from collections import Counter | ||
|
||
from matplotlib import pyplot as plt | ||
from tabulate import tabulate | ||
|
||
from retrieval.util.Math import normalise | ||
from util.FileManager import write_txt | ||
|
||
|
||
def plot_frequency(counter: Counter): | ||
frequencies = normalise(counter.values()) | ||
frequencies.sort(reverse=True) | ||
frequencies = frequencies[:100] | ||
_generate_figure(frequencies, title="Term Frequency", x_label="Rank", | ||
y_label="Probability", file_name='term-frequencies.png') | ||
def zipfs(counter: Counter): | ||
prob_distribution = [] | ||
rows = [] | ||
total_count = sum(counter.values()) | ||
c = 0.0 | ||
|
||
for rank, (word, freq) in enumerate(counter.most_common(100)): | ||
rank += 1 | ||
p = freq / total_count | ||
pr = rank * p | ||
c += pr | ||
prob_distribution.append(p) | ||
rows.append([word, freq, rank, "{:.3f}".format(p), "{:.3f}".format(pr)]) | ||
|
||
def _generate_figure(data, title=None, x_label=None, y_label=None, file_name='figure.png'): | ||
plt.plot(data) | ||
_plot_distribution(prob_distribution) | ||
_report_parameters(rows, c) | ||
|
||
|
||
def _plot_distribution(prob_distribution: list[float]): | ||
zipf_distribution = [0.1 / i for i in range(1, 101)] | ||
_generate_figure(prob_distribution, zipf_distribution, title="Zipf's Law", x_label="Rank", | ||
y_label="Probability", file_name='zipf-plot.png') | ||
|
||
|
||
def _report_parameters(rows: list[list], c: float): | ||
table = tabulate(rows, headers=['Word', 'Freq', 'r', 'Pr', 'r*Pr']) | ||
c = round(c / len(rows), 3) | ||
data = table + f'\n\nc = {c}' | ||
write_txt('zipf-parameters.txt', data) | ||
|
||
|
||
def _generate_figure(*data, title=None, x_label=None, y_label=None, file_name='figure.png'): | ||
for d in data: | ||
plt.plot(d) | ||
plt.title(title) | ||
plt.xlabel(x_label) | ||
plt.ylabel(y_label) | ||
plt.grid() | ||
plt.savefig(file_name) |