Exemple #1
0
    def test_huffman_5(self):
        sent = 'StringTeessttt'; codes = dict(); root = huffman(sent)
        for ch in sent:
            codes[ch] = get_huffman_code(ch, root)

        self.assertDictEqual(codes, {'S': '0100', 't': '11', 'r': \
                                   '1001', 'i': '1011', 'n': '1000', 'g': '1010', 'T': '0101', \
        'e': '011', 's': '00'})
Exemple #2
0
    def test_huffman_4(self):
        sent = 'testing this string'; codes = dict(); root = huffman(sent)
        for ch in sent:
            codes[ch] = get_huffman_code(ch, root)

        self.assertDictEqual(codes, {'t': '01', 'e': '10110', 's': '111', \
                                   'i': '110', 'n': '100', 'g': '001', ' ': '000', 'h': '10111', \
        'r': '1010'} )
Exemple #3
0
    def test_huffman_3(self):
        sent = 'asdf;k;lkjasdfk'; codes = dict(); root = huffman(sent)
        for ch in sent:
            codes[ch] = get_huffman_code(ch, root)  

        self.assertDictEqual(codes, {'a': '100', 's': '011', 'd': '101', \
                                   'f': '010', ';': '111', 'k': '00', \
                                      'l': '1101', 'j': '1100'})  
Exemple #4
0
    def test_huffman_2(self):
        sent = 'asdf;k;lkjasdfk dasiirFFDg'; codes = dict(); root = huffman(sent)
        for ch in sent:
            codes[ch] = get_huffman_code(ch, root)  

        self.assertDictEqual(codes, \
                           {'a': '001', 's': '100', 'd': '010', 'f': '1011', \
                              ';': '1101', 'k': '011', 'l': '11100', 'j': \
                              '11111', ' ': '11000', 'i': '000', 'r': '11101',\
                              'F': '1010', 'D': '11001', 'g': '11110'})
Exemple #5
0
def compress_text(text, save=False, save_path=None):
    huff_tree = huffman(text)
    bit_dict = {}
    bit_map(huff_tree, bit_dict, "")
    res = process_bit_map(text, bit_dict)
    if save:
        file = open(save_path, "w")
        file.write(res)
        file.close()
    return process_bit_map(text, bit_dict)
Exemple #6
0
 def test_huffman_1(self):
     sent = 'aaaaggccttt'
     codes = dict()
     root = huffman(sent)
     for ch in sent:
         codes[ch] = get_huffman_code(ch, root)
     self.assertDictEqual(codes, {
         'a': '0',
         'g': '111',
         'c': '110',
         't': '10'
     })
Exemple #7
0
from huffman import *

letters = {}
tree = []
temp = []

L = raw_input('Enter text you wish to be encoded: ')

for i in L:
    try:
        letters[i] += 1
    except KeyError:
        letters[i] = 1

for j in letters.keys():
    temp.append((letters[j],'',j))

tree = sorted(huffman(temp))

print tree
Exemple #8
0
# -*- coding: koi8_u -*-
from huffman import *

a=huffman("all models are wrong but some are usefull")
a.encode()
a.print_tree()
a.decode()