def load_posting_list_parts(self, stem): offset, size = self.seek_list[stem][0] self.index_file.seek(offset) binary_data = self.index_file.read(size) decoded_posting_list = Huffman.decode( binary_data, self.symbol_to_encoding_dict) return [stem] + decoded_posting_list.split(posting_list_separator)
class TestHuffman(unittest.TestCase): def setUp(self): self.huffman = Huffman() self.phrase = 'The quick brown fox jumped over the lazy dog. Filler text to create a bigger shift in the character probabilities.' self.huffman.build(self.phrase) def testPhrase(self): self.huffman.build(self.phrase) enc = self.huffman.encode(self.phrase) self.assertEqual(self.phrase, self.huffman.decode(enc))
code_table[line[5:-1]] = line[0:4] decoded_file = str() for i in range(len(raw_file)/7): mark = "".join(raw_file[7*i:7*i+7]) + "\n" + check("".join(raw_file[7*i:7*i+7]), error_flag) decoded_file = decoded_file + code_table[check("".join(raw_file[7*i:7*i+7]), error_flag)] # check, not necessary # print code_table # print decoded_file print "[*]Length after Hanmming decode:", len(decoded_file) # write output-file f = open(output_file_name, "w+") f.write(decoded_file) f.close() except Exception, e: print "===========================" print mark print "+++++++++++++++++++++++++++" raise e #test if function is correct if __name__ == "__main__": error_flag("error_flag.txt") make_code_table("hamming_table_out.txt") encode("hamming_table_out.txt", "huffman_out.txt", "hamming_out.txt") decode("hamming_table_out.txt", "hamming_out.txt", "hamming_decode_out.txt") import Huffman Huffman.decode("huffman_table_out.txt", "hamming_decode_out.txt", "huffman_decode_out.txt")
fin = open(input_file_name, "r") raw_file = fin.read() fin.close() # fake transmission message_out = str() for i in raw_file: if random() > Z[int(i)][0]: message_out = message_out + "1" else: message_out = message_out + "0" # check, not necessary # print message_out print "[*]Length after transport:", len(message_out) # write output-file f = open(output_file_name, "w+") f.write(message_out) f.close() except Exception, e: raise e #test if function is correct if __name__ == "__main__": transmission("hamming_out.txt", "channel_out.txt") import Hamming, Huffman # Hamming is decoding ;D Hamming.decode("hamming_table_out.txt", "channel_out.txt", "hamming_decode_out_from_channel.txt") # Huffman is decoding XD Huffman.decode("huffman_table_out.txt", "hamming_decode_out_from_channel.txt", "huffman_decode_out_from_channel.txt")
# -*- coding:utf-8 -*- import Huffman, Hamming, Channel #test if function is correct if __name__ == "__main__": # Huffman is encoding XD Huffman.make_code_table("frequency.txt", "huffman_table.out") Huffman.encode("huffman_table.out", "raw_file.txt", "huffman.out", 4) # Hamming is encoding ;D print "=====message with Hamming-coding=====" Hamming.error_flag("error_flag.out") Hamming.make_code_table("hamming_table.out") Hamming.encode("hamming_table.out", "huffman.out", "hamming.out") # message is in the channel Channel.transmission("hamming.out", "channel.out") # Hamming is decoding ;D Hamming.decode("hamming_table.out", "channel.out", "hamming_decode_out_from_channel.out", "error_flag.out") # Huffman is decoding XD Huffman.decode("huffman_table.out", "hamming_decode_out_from_channel.out", "huffman_decode_out_from_channel.out") print "=====message *without* Hamming-coding=====" # message is in the channel Channel.transmission("hamming.out", "channel.out") # Huffman is decoding XD Huffman.decode("huffman_table.out", "channel.out", "huffman_decode_out_from_channel2.out") print "=====Huffman=====" Huffman.efficiency("huffman_table.out", "frequency.txt")