Advertisement
575

shannon algo

575
May 9th, 2022
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. import math
  2. from collections import Counter
  3. import operator
  4.  
  5. class ShanonFanno(object):
  6.  
  7.     def __init__(self):
  8.         self.sum_logs_with_pis = 0
  9.         self.size_after_compress = 0
  10.         self.sorted_s = ""
  11.         self.char_dict = dict()
  12.  
  13.     def devide_chars(self, x, itr, code):
  14.         if len(x) == 2 : return [[x[0], itr + 1, code + "0"], [x[1], itr + 1, code + "1"]]
  15.         if len(x) == 1 : return [[x, itr, code]]
  16.         index = self.break_the_node(x)
  17.         return [self.devide_chars(x[:index+1], itr + 1, code + "0"), self.devide_chars(x[index+1:], itr + 1, code + "1")]
  18.  
  19.     def make_count(self):
  20.         self.char_dict = dict(Counter(self.sentence))
  21.         char_ls = sorted(self.char_dict.items(), key=operator.itemgetter(1),reverse=True)
  22.         sorted_s = ""
  23.         for i in char_ls :
  24.             sorted_s += i[0]
  25.         return self.char_dict, sorted_s
  26.  
  27.     def flatten_the_tree(self, tree):
  28.             leaf = False
  29.             flat_list = []
  30.             if len(tree) == 1 : tree = tree[0]
  31.             while(leaf == False):
  32.  
  33.  
  34.                 if type(tree[-1]) is not list:
  35.  
  36.                     if len(tree) != 3 : break
  37.  
  38.                     leaf = True
  39.                     pi = self.sentence.count(tree[0])/self.total
  40.                     log_pi = math.log(1/pi, 2)
  41.                     x = tree.copy()
  42.                     x.append(pi)
  43.                     self.sum_logs_with_pis += pi * log_pi
  44.                     return [x]
  45.                 else:
  46.  
  47.                     flat_right = []
  48.                     flat_left = []
  49.                     flat_right.extend(self.flatten_the_tree(tree[0]))
  50.                     flat_left.extend(self.flatten_the_tree(tree[1]))
  51.                     flat_list.extend(flat_right)
  52.                     flat_list.extend(flat_left)
  53.                     return flat_list
  54.  
  55.  
  56.  
  57.  
  58.     def break_the_node(self, node):
  59.         total = 0
  60.         for i in node :
  61.             total += self.char_dict[i]
  62.         length = len(node)
  63.         count = 0
  64.         last_char_index = 0
  65.         for i in range(length//2):
  66.             count += self.char_dict[self.sorted_s[i]]
  67.             if (count - (total/2) >= 0):
  68.                 last_char_index = i +1
  69.                 break
  70.         return last_char_index
  71.  
  72.  
  73.  
  74.                                  
  75.     def do_the_work(self,s):
  76.         s = s.replace(' ', '')
  77.         self.sentence = s
  78.         self.total = len(s)
  79.         self.char_dict, self.sorted_s = self.make_count()
  80.         last_char_index = self.break_the_node(self.sorted_s)
  81.         left = self.sorted_s[:last_char_index]
  82.         right = self.sorted_s[last_char_index:]
  83.         left_tree = self.devide_chars(left, 1, "0")
  84.         right_tree = self.devide_chars(right, 1, "1")
  85.  
  86.         left_flat = self.flatten_the_tree(left_tree)
  87.         right_flat = self.flatten_the_tree(right_tree)
  88.         self.final_flat = []
  89.         self.final_flat.extend(left_flat)
  90.         self.final_flat.extend(right_flat)
  91.         self.write_final_logs(self.final_flat)
  92.  
  93.     def write_final_logs(self, final_flat):
  94.         for i in final_flat :
  95.             count = self.sentence.count(i[0])
  96.             print("Символ: {0} => [код: {1}], [частота: {2}]".format(i[0],i[2],count))
  97.  
  98. s2 = input('Введите текст до 255 символов')
  99. sh = ShanonFanno()
  100. sh.do_the_work(s2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement