Ejemplo n.º 1
0
def read_compressed_index_from_file_fibonacci(out_file_name):
    print "Reading compressed index with fibonacci..."
    from kbp.univ import fibonacci
    import struct
    index = dict()

    file_name = out_file_name + "_fibonacci"
    f = open(file_name, 'rb')

    word_len_bytes = f.read(4)
    idx1 = 0
    while word_len_bytes:
        word_len = struct.unpack('I', word_len_bytes)[0]
        word = f.read(word_len).decode("utf8")
        freq_bytes = f.read(4)

        freq = struct.unpack('I', freq_bytes)[0]
        arr_len_bytes = f.read(4)
        arr_len = struct.unpack('I', arr_len_bytes)[0]
        arr = ""
        for i in range(0, arr_len):
            string = f.read(4)

            number = struct.unpack('I', string)[0]

            formated = "{0:b}".format(number)
            if len(formated) % 32 != 0:
                zeroes = 32 - len(formated) % 32
                for i in range(zeroes):
                    formated = "0" + formated
            arr += formated
        array = list()
        idx = 0
        while len(arr) > 0 and "11" in arr:
            var = fibonacci.decode(arr) - 1
            idx += 1

            array.append(var)
            arr = arr[arr.index("11") + 2:]

        index[word] = freq, array
        word_len_bytes = f.read(4)
        idx1 += 1

    f.close()
    return index
Ejemplo n.º 2
0
#Kabopan - Readable Algorithms. Public Domain, 2007-2009

from kbp.univ.fibonacci import ( \
 number, generate_numbers, decompose, recompose, encode, decode)

assert number(20) == 6765

assert generate_numbers(20) == [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]

assert decompose(10) == [3, 6]
assert decompose(6764) == [3, 5, 7, 9, 11, 13, 15, 17, 19]

assert recompose(decompose(6764)) == 6764

assert encode(7) == "001011"
assert encode(6764) ==  "00101010101010101011"

assert decode("01011") == 4
assert decode("001011") == 7
Ejemplo n.º 3
0
#Kabopan - Readable Algorithms. Public Domain, 2007-2009

from kbp.univ.fibonacci import ( \
 number, generate_numbers, decompose, recompose, encode, decode)

assert number(20) == 6765

assert generate_numbers(20) == [
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597,
    2584, 4181, 6765
]

assert decompose(10) == [3, 6]
assert decompose(6764) == [3, 5, 7, 9, 11, 13, 15, 17, 19]

assert recompose(decompose(6764)) == 6764

assert encode(7) == "001011"
assert encode(6764) == "00101010101010101011"

assert decode("01011") == 4
assert decode("001011") == 7