# Shannon Fano sf = ShannonFano.ShannonFano(name) encode_file_name_sf = 'encode/' + i + '.sf' start_sf = time.time() sf.encode(encode_file_name_sf) end_sf = time.time() run_time_sf = (end_sf - start_sf) before_size = os.path.getsize(name) after_size_sf = os.path.getsize(encode_file_name_sf) compression_ratio_sf = before_size / after_size_sf # Huffman Coding hc = Huffman.HuffmanCoding(name) encode_file_name_hc = 'encode/' + i + '.hc' start_hc = time.time() hc.compress(encode_file_name_hc) end_hc = time.time() run_time_hc = end_hc - start_hc after_size_hc = os.path.getsize(encode_file_name_hc) compression_ratio_hc = before_size / after_size_hc print("Shannon-Fano: ", i, sf.row, 'x', sf.col, compression_ratio_sf, run_time_sf, 's') print("Huffman Coding: ", i, hc.row, 'x', hc.col, compression_ratio_hc, run_time_hc, 's')
LZ.generateLZCoding(w, dictionary, number_of_symbols, "symbol_list.txt") print() LZ_size = (os.path.getsize("data_binary_lz.txt")) print() #%% print('-----Huffman Coding-----') all_codes = {} all_reverse_codes = {} huffman_forest = {} huff.generate_huffman_codes(all_codes, all_reverse_codes, huffman_forest, tpm) #printing huffman codes consructed ''' print("Huffman codes:") for symbol,tree in huffman_forest.items(): print(f"Codes when prev symbol is: {symbol}") tree.pr() print() ''' huff.HuffmanCoding(all_codes, "symbol_list.txt") Huffman_size = os.path.getsize("data_binary_hff.txt") #%% print(f'bits required for trivial encoding --> {trivial_size}') print(f'bits required for LZ encoding --> {LZ_size}') print(f'bits required for huffman encoding --> {Huffman_size}') print() print("compression ratio:") print(f"LZ : {(LZ_size/trivial_size)*100}") print(f"Huffman: {(Huffman_size/trivial_size)*100}")