-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.py
More file actions
61 lines (47 loc) · 1.49 KB
/
book.py
File metadata and controls
61 lines (47 loc) · 1.49 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
52
53
54
55
56
57
58
59
60
61
import math
class Book:
def __init__(self, name, char_count):
self._name = name
self.char_count = char_count
self._characters = {}
self._characters_prob = {}
self._entropy = 0
def get_name(self):
return self._name
def set_name(self, name):
self._name = name
def get_char_count(self):
return self.char_count
def set_char_count(self, char_count):
self.char_count = char_count
def get_characters(self):
return self._characters
def set_characters(self, characters):
self._characters = characters
def char_relative_prob(self):
for char in self._characters:
rel_prob = self._characters[char] / self.char_count
self._characters_prob[char] = rel_prob
def get_characters_prob(self):
return self._characters_prob
def calc_entropy(self):
entropy = 0
for char, rel_prob in self._characters_prob.items():
entropy += rel_prob * math.log2(rel_prob)
entropy *= -1
self._entropy = entropy
return entropy
def get_entropy(self):
return self._entropy
def sort_char_rel_prob(self):
sorted_chars = sorted(self._characters_prob, key=self._characters_prob.get)
sorted_chars.reverse()
sorted_probs_dict = {}
for char in sorted_chars:
sorted_probs_dict[char] = self._characters_prob[char]
return sorted_probs_dict
def total_char_prob(self):
total_prob = 0
for char, char_prob in self._characters_prob.items():
total_prob += char_prob
return total_prob