def setErrors(data): length = len(data) i, j = 0, 7 while j <= length: randIndex = randint(i, j - 1) temp = data[randIndex] temp = '1' if int(temp) == 0 else '0' data = data[:randIndex] + temp + data[randIndex + 1:] i = j j += 7 return data file = ReadFile('txt_files/Text.txt', analize=True) source = Huffman(file.data) encripted = source.encript(file.file_text) channel = Hamming() encripted = channel.encode(encripted) #set errors errorData = setErrors(encripted) writeToFile('Encripted Text', errorData) #fix errors channel = Hamming() trueData = channel.decode(errorData) decripted = source.decript(trueData) writeToFile('Decripted Text', decripted)
ham = Hamming() emis = "" while len(source) >= 4: mot = source[0:4] emis += ham.hamming(mot) source = source[4:] FileUtil.write("emis.txt", emis) recu = NoiseGenerator.convert(emis) FileUtil.write("recu.txt", recu) correct = "" while len(recu) >= 7: mot = recu[0:7] correct += ham.correction(mot) recu = recu[7:] FileUtil.write("correct.txt", correct) message = "" while len(correct) >= 7: mot = correct[0:7] message += ham.decode(mot) correct = correct[7:] FileUtil.write("message.txt", message)