Advertisement
CSenshi

GABELIA

Dec 28th, 2019
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. import sys
  2. import heapq as hp
  3. import os
  4. import PrefCode
  5.  
  6. ''' class for heap tree '''
  7.  
  8.  
  9. class Node:
  10.     def __init__(self, index, freq):
  11.         self.index = index
  12.         self.freq = freq
  13.         self.left = None
  14.         self.right = None
  15.  
  16.     def __lt__(self, other):
  17.         return self.freq < other.freq
  18.  
  19.  
  20. def rec(elem, len, res):
  21.     if elem.index != None:
  22.         res.append((elem.index, len))
  23.         return
  24.     rec(elem.left, len+1, res)
  25.     rec(elem.right, len+1, res)
  26.  
  27.  
  28. def huffman(arr):
  29.     arr = [(arr[x], x) for x in range(len(arr))]
  30.  
  31.     # create queue
  32.     pq = []
  33.     for key in arr:
  34.         node = Node(freq=key[0], index=key[1])
  35.         hp.heappush(pq, node)
  36.  
  37.     # create tree
  38.     while len(pq) > 1:
  39.         f = hp.heappop(pq)
  40.         s = hp.heappop(pq)
  41.  
  42.         merged = Node(freq=f.freq + s.freq, index=None)
  43.         merged.left = f
  44.         merged.right = s
  45.  
  46.         hp.heappush(pq, merged)
  47.  
  48.     # evaluate code lengths
  49.     res = []
  50.     rec(hp.heappop(pq), 0, res)
  51.     return res
  52.  
  53.  
  54. def craft_value(arr):
  55.     # arr = [(freq, len)]
  56.     sum = 0
  57.     for _, len in arr:
  58.         sum += 2**(0-len)
  59.     return sum
  60.  
  61.  
  62. def build_code(arr):
  63.     # Create tuple of (length, index, code)
  64.     for i in range(n):
  65.         arr[i] = (arr[i][1], arr[i][0], '')
  66.  
  67.     # sort by length of codes
  68.     arr = sorted(arr, key=lambda x: x[0])
  69.  
  70.     dlm = ''
  71.     for i in range(len(arr)):
  72.         tmp = list(arr[i])
  73.  
  74.         tmp[2] = dlm + '0' * (arr[i][0] - len(dlm))
  75.         dlm = "{0:b}".format((int(tmp[2], 2) + 1))
  76.         dlm = '0' * (len(tmp[2]) - len(dlm)) + dlm
  77.  
  78.         arr[i] = tmp
  79.  
  80.     # sort by length of codes
  81.     arr = sorted(arr, key=lambda x: x[1])
  82.  
  83.     return arr
  84.  
  85.  
  86. if __name__ == "__main__":
  87.     if len(sys.argv) != 3:
  88.         print("Usage : python3 Huffman.py src-fname dest-fname")
  89.         exit()
  90.  
  91.     src_fname = sys.argv[1]
  92.     dst_fname = sys.argv[2]
  93.  
  94.     with open(src_fname, 'r') as f:
  95.         n = int(f.readline())
  96.         arr = list(map(float, f.readline().split()))
  97.  
  98.     # find length of codes with probabilities
  99.     arr = huffman(arr)
  100.  
  101.     # sort with indices
  102.     arr.sort(key=lambda x: x[0])
  103.  
  104.     # write length of codes in file
  105.     tmp_fname = 'studentFiles/tmp'
  106.     with open(tmp_fname, 'w+') as tf:
  107.         # total codes
  108.         tf.write('{}\n'.format(len(arr)))
  109.  
  110.         for _, len in arr:
  111.             tf.write('{} '.format(len))
  112.  
  113.     PrefCode.main(tmp_fname, dst_fname)
  114.     os.remove(tmp_fname)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement