def main(d):
	
	result = ""
	bit_str = ""
	h = Hamming()
	if len(d['bits']) % 7: # Invalid message length
		return "Error!"
	for i in xrange(len(d['bits']) / 7):
		chunk = d['bits'][i*7:(i+1)*7]
		h.code(chunk)
		error_bit = h.check()
		if error_bit:
			bit_list = map(int, list(chunk))
			bit_list[error_bit - 1] = int(not bit_list[error_bit - 1])
			chunk = ''.join(map(str, bit_list))
			h.code(chunk)
		if h.check(): # Still error after error correcting
			return "Error!"
		bit_str += ''.join(map(str, h.data()))
	
	#print bit_str
	for i in xrange(len(bit_str) / 8):
		chunk = bit_str[i*8:(i+1)*8]
		ch = 0
		for b in chunk:
			ch <<= 1
			ch |= int(b)
		if ch < 32 or ch > 127:
			return "Error!" # Contains non-printable characters
		result += chr(ch)
	
	return result
def main():

    #define query doc here

    cosine = Cosine(original_path, query_path)
    jacardian = Jacardian(original_path, query_path)
    hamming = Hamming(original_path, query_path)

    print("Cosine similarity is:")
    print(cosine.similarity())

    print("Jacardian similarity is:")
    print(jacardian.similarity())

    print("Hamming similarity is:")
    print(hamming.similarity())
 def is_message_valid(message: str) -> bool:
     if CommunicationSettings.check_sum is CheckSum.Parit_bit:
         return ParityBit.check_parity(message)
     elif CommunicationSettings.check_sum is CheckSum.CRC:
         return CRC.check_CRC(message)
     elif CommunicationSettings.check_sum is CheckSum.Hamming_code:
         return Hamming.detectError(message)
 def encode_message(message: str) -> str:
     encodedMessage = ""
     if CommunicationSettings.check_sum is CheckSum.Parit_bit:
         encodedMessage = ParityBit.add_parity_bit(message)
     elif CommunicationSettings.check_sum is CheckSum.CRC:
         encodedMessage = CRC.encode_data(message)
     elif CommunicationSettings.check_sum is CheckSum.Hamming_code:
         encodedMessage = Hamming.encode_data(message)
     return encodedMessage
Beispiel #5
0
    def to_packet(self, message: str) -> None:
        original_message = message
        if CommunicationSettings.check_sum == CheckSum.Hamming_code:
            message = Hamming.extractKey(message)

        self.key = int(message[0:CommunicationSettings.key_bits], 2)
        self.data = message[CommunicationSettings.
                            key_bits:CommunicationSettings.key_bits + 1]
        self.is_valid = CommunicationSettings.is_message_valid(message)

        if self.data == "1":
            self.retransmit = True
        elif self.data == "0":
            self.retransmit = False
        else:
            self.is_valid = False
Beispiel #6
0
class HammingParameterizedPackage(unittest.TestCase):
    def setUp(self):
        self.tmp = Hamming()

    @parameterized.expand([
        ("", "", 0),
        ("A", "A", 0),
        ("G", "T", 1),
        ("GGACTGAAATCTG", "GGACTGAAATCTG", 0),
        ("GGACGGATTCTG", "AGGACGGATTCT", 9),
    ])
    def test_one_parameterized(self, a, b, dist):
        self.assertEqual(self.tmp.distance(a, b), dist)

    def tearDown(self):
        self.temp = None
    def to_packet(self, message: str) -> None:
        #TODO: Change this convertion to include the ability to decode hamming code
        original_message = message
        if CommunicationSettings.check_sum == CheckSum.Hamming_code:
            message = Hamming.extractKey(message)
        
        key_str = message[0:CommunicationSettings.key_bits]
        self.data = message[CommunicationSettings.key_bits: CommunicationSettings.data_bytes * 8 + CommunicationSettings.key_bits]

        self.key = int(key_str, 2)

        self.valid = CommunicationSettings.is_message_valid(original_message)
        if key_str == "".rjust(CommunicationSettings.key_bits, "1"):
            self.eot = True
        else:
            self.eot = False
Beispiel #8
0

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)
Beispiel #9
0
 def setUp(self):
     self.tmp = Hamming()
Beispiel #10
0
from FileUtil import FileUtil
from Hamming import Hamming
from NoiseGenerator import NoiseGenerator

source = FileUtil.read("source.txt")

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]