Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import heapq
- from collections import Counter
- from collections import namedtuple
- class Node(namedtuple("Node", ["left", "right"])):
- def walk(self, code, acc):
- self.left.walk(code, acc + "0")
- self.right.walk(code, acc + "1")
- class Leaf(namedtuple("Leaf", ["char"])):
- def walk(self, code, acc):
- code[self.char] = acc or "0"
- def huffman_encode(s):
- h = []
- for ch, freq in Counter(s).items():
- h.append((freq, len(h), Leaf(ch)))
- heapq.heapify(h)
- count = len(h)
- while len(h) > 1:
- freq1, _count1, left = heapq.heappop(h)
- freq2, _count2, right = heapq.heappop(h)
- heapq.heappush(h, (freq1 + freq2, count, Node(left, right)))
- count += 1
- code = {}
- if h:
- [(_freq, _count, root)] = h
- root.walk(code, "")
- return code
- import pickle
- s = open('encoded8bit.bin','r')
- code = huffman_encode(s)
- encoded = "".join(code[ch] for ch in s)
- b = open('56.bin', 'wb')
- pickle.dump(encoded, b)
- b.close()
- code = huffman_encode(s)
Add Comment
Please, Sign In to add comment