Example #1
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
Example #2
0
            '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'}

from kbp.entro._encoding import encode, decode

encoded_string = encode(test_codes, "abracadabra")
assert encoded_string == 'n\x8a\xdc'
assert decode(test_tree, encoded_string) == test_string
Example #3
0
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'}

from kbp.entro._encoding import encode, decode

encoded_string = encode(test_codes, "abracadabra")
assert encoded_string == 'n\x8a\xdc'
assert decode(test_tree, encoded_string) == test_string
Example #4
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