Exemple #1
0
#Kabopan - Readable Algorithms. Public Domain, 2007-2009

from kbp.entro.huffman import pop_entry, generate_tree
from kbp.entro._encoding import generate_codes


assert pop_entry([{"symbol":"a", "weight" : 15}]) == {"symbol":"a", "weight":15}
assert pop_entry([{"node":"this"}]) == "this"


tree_test = generate_tree("a")

assert tree_test == {'symbol': 'a', 'weight': 1}
assert generate_codes(tree_test) == {"a":""}

test_string = "abracadabra"
test_tree = generate_tree(test_string)

assert test_tree == {'left0': {'symbol': 'a', 'weight': 5},
                     'right1': {'left0': {'left0': {'symbol': 'c', 'weight': 1},
                                          'right1': {'symbol': 'd', 'weight': 1},
                                          'weight': 2},
                                'right1': {'left0': {'symbol': 'b', 'weight': 2},
                                           'right1': {'symbol': 'r', 'weight': 2},
                                           'weight': 4},
                                'weight': 6},
                     'weight': 11}

test_codes = generate_codes(test_tree)
assert test_codes == {'a':'0', 'c':'100', 'd':'101', 'b':'110', 'r':'111'}
Exemple #2
0
            'left0': {
                'symbol': 'r'
            },
            'right1': {
                'left0': {
                    'symbol': 'c'
                },
                'right1': {
                    'symbol': 'd'
                }
            }
        }
    }
}

test_codes = generate_codes(test_tree)
assert test_codes == {
    'a': '0',
    'b': '10',
    'r': '110',
    'c': '1110',
    'd': '1111'
}

from kbp.entro._encoding import encode, decode

test_encoding = encode(test_codes, test_string)

assert test_encoding == 'Y\xcfX'
assert decode(test_tree, test_encoding) == test_string
Exemple #3
0
#Kabopan - Readable Algorithms. Public Domain, 2007-2009

from kbp.entro._encoding import generate_codes, get_weights_and_symbols

assert generate_codes({'left0': {'symbol': 'a', 'weight': 1},
                     'right1': {'symbol': 'b', 'weight': 1},
                     'weight': 2}) == {'a': '0', 'b': '1'}

assert get_weights_and_symbols("a") == [{"symbol":"a", "weight":1}]
assert get_weights_and_symbols("abababbc") == [ \
 {"symbol":"a", "weight":3},
 {"symbol":"b", "weight":4},
 {"symbol":"c", "weight":1}]
Exemple #4
0
from kbp.entro.huffman import pop_entry, generate_tree
from kbp.entro._encoding import generate_codes

assert pop_entry([{
    "symbol": "a",
    "weight": 15
}]) == {
    "symbol": "a",
    "weight": 15
}
assert pop_entry([{"node": "this"}]) == "this"

tree_test = generate_tree("a")

assert tree_test == {'symbol': 'a', 'weight': 1}
assert generate_codes(tree_test) == {"a": ""}

test_string = "abracadabra"
test_tree = generate_tree(test_string)

assert test_tree == {
    'left0': {
        'symbol': 'a',
        'weight': 5
    },
    'right1': {
        'left0': {
            'left0': {
                'symbol': 'c',
                'weight': 1
            },
Exemple #5
0
#Kabopan - Readable Algorithms. Public Domain, 2007-2009

from kbp.entro._encoding import generate_codes, get_weights_and_symbols

assert generate_codes({
    'left0': {
        'symbol': 'a',
        'weight': 1
    },
    'right1': {
        'symbol': 'b',
        'weight': 1
    },
    'weight': 2
}) == {
    'a': '0',
    'b': '1'
}

assert get_weights_and_symbols("a") == [{"symbol": "a", "weight": 1}]
assert get_weights_and_symbols("abababbc") == [ \
 {"symbol":"a", "weight":3},
{"symbol":"b", "weight":4},
{"symbol":"c", "weight":1}]
Exemple #6
0
description = "entropy encoder"

families = {
    "Huffman":{
        "huffman":["Huffman tree", huffman.generate_tree, ""],
        #"ahuffman":["Adaptative Huffman tree", lambda:str(), "not implemented"],
        },
    "Arithmetic":{
        #"range":["Range Encoding", lambda:str(), "not implemented"],
        #"arithmetic":["Arithmetic Encoding", lambda:str(), "not implemented"],
        },
    "Shannon-Fano":{
        "sfc":["Shannon-Fano coding", sfc.encode, ""]
        }
    }

algorithms = get_algorithms(families)

Help = makehelp(description, __version__, "<data_to_encode>", families)

if len(argv) == 1:
    print(Help)
    exit()

requested_algorithms, inputs = get_parameters(argv, 1, algorithms)
tab = "   "


for s in requested_algorithms:
        print "%s\t%s" % (algorithms[s][1], encoding.generate_codes(algorithms[s][0](inputs[0])))
Exemple #7
0
assert split_weights([1, 2, 3]) == 1
assert split_weights([8, 7, 1]) == 0
assert split_weights([1, 1, 8]) == 1

assert generate_tree([{"symbol": "a", "weight": 35}]) == {"symbol": "a"}
assert generate_tree([{"symbol": "a", "weight": 2}, {"symbol": "b", "weight": 2}]) == {
    "left0": {"symbol": "a"},
    "right1": {"symbol": "b"},
}

test_string = "abracadabra"
test_tree = encode(test_string)

assert test_tree == {
    "left0": {"symbol": "a"},
    "right1": {
        "left0": {"symbol": "b"},
        "right1": {"left0": {"symbol": "r"}, "right1": {"left0": {"symbol": "c"}, "right1": {"symbol": "d"}}},
    },
}

test_codes = generate_codes(test_tree)
assert test_codes == {"a": "0", "b": "10", "r": "110", "c": "1110", "d": "1111"}

from kbp.entro._encoding import encode, decode

test_encoding = encode(test_codes, test_string)

assert test_encoding == "Y\xcfX"
assert decode(test_tree, test_encoding) == test_string