redsees

Untitled

May 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. try:
  3.     import sys, os
  4.     from cStringIO import StringIO
  5.     from heapq import heappush, heappop, heapify
  6.     from collections import defaultdict
  7. except ImportError:
  8.     print "[!] Error finding one or more libraries\n\n"
  9.     exit(-1)
  10.  
  11. class MyCompress():
  12.     Errmsg = ""
  13.  
  14.     def LZW_Compress(self, mymsg):
  15.        
  16.         # Build the ASCII table
  17.         ASCII_TAB_LEN = 256
  18.         ASCII_TAB = dict((chr(i), i) for i in xrange(ASCII_TAB_LEN))
  19.        
  20.         # Prepare for compression
  21.         byte = ""
  22.         result = []
  23.        
  24.         for char in mymsg:
  25.             comb = byte + char
  26.             if comb in ASCII_TAB:
  27.                 byte = comb
  28.             else:
  29.                 result.append(ASCII_TAB[byte])
  30.                 # Add new entry to ASCII table
  31.                 ASCII_TAB[comb] = ASCII_TAB_LEN
  32.                 ASCII_TAB_LEN += 1
  33.                 byte = char
  34.        
  35.         # Process last character in the input string
  36.         if byte:
  37.             result.append(ASCII_TAB[byte])
  38.         return result
  39.        
  40.  
  41.     def LZW_Decompress(self, mymsg):
  42.        
  43.         # Build the dictionary.
  44.         ASCII_TAB_LEN = 256
  45.         ASCII_TAB = dict((i, chr(i)) for i in xrange(ASCII_TAB_LEN))
  46.        
  47.         result = StringIO()
  48.         w = chr(mymsg.pop(0))
  49.         result.write(w)
  50.        
  51.         for k in mymsg:
  52.             if k in ASCII_TAB:
  53.                 entry = ASCII_TAB[k]
  54.             elif k == ASCII_TAB_LEN:
  55.                 entry = w + w[0]
  56.             else:
  57.                 raise ValueError('Bad compressed k: %s' % k)
  58.            
  59.             result.write(entry)
  60.            
  61.             # Add w+entry[0] to the dictionary.
  62.             ASCII_TAB[ASCII_TAB_LEN] = w + entry[0]
  63.             ASCII_TAB_LEN += 1
  64.             w = entry
  65.        
  66.         return result.getvalue()
  67.    
  68.     def HM_Encode(self, mymsg):
  69.         Freq_TAB = defaultdict(int)
  70.         for ch in mymsg:
  71.             Freq_TAB[ch] += 1
  72.        
  73.         # Build heap according to each character's frequency   
  74.         heap = [[freq, [symb, ""]] for symb, freq in Freq_TAB.items()]
  75.         heapify(heap)
  76.        
  77.         # Assign binary to each character
  78.         while len(heap) > 1:
  79.             left = heappop(heap)
  80.             right = heappop(heap)
  81.             for pair in left[1:]:
  82.                 pair[1] = '0' + pair[1]
  83.             for pair in right[1:]:
  84.                 pair[1] = '1' + pair[1]
  85.             heappush(heap, [left[0] + right[0]] + left[1:] + right[1:])
  86.            
  87.         # Sort heap
  88.         huff = sorted(heappop(heap)[1:], key=lambda p: (len(p[-1]), p))
  89.        
  90.         # Print Huffman Encoding table
  91.         print "\n\nChar\tFrequency\tHuffman Code"
  92.         for p in huff:
  93.             print "%s\t%s\t\t%s" % (p[0], Freq_TAB[p[0]], p[1])
Add Comment
Please, Sign In to add comment