from RLE import rle


print( '\n\n' + rle.Version() )
bset1 = rle(None)
#bset1.SET(441)
uncompressedSize = 10000
uncompressedSizeBytes = uncompressedSize>>3
divider = 2
for x in range (0,uncompressedSize):
    if (x % divider) == 0:
        bset1.SET(x)
print(bset1)
print("set bits from 0 to ", uncompressedSize," divisible by ", divider)
print("compressed size   :", bset1.size(), " bytes")
print("uncompressed size :", uncompressedSizeBytes, " bytes")
print("compression percentage:",(bset1.size()/uncompressedSizeBytes) * 100, "%\n")


bset1 = rle(None)
uncompressedSize = 10000
uncompressedSizeBytes = uncompressedSize>>3
divider = 4
for x in range (0,uncompressedSize):
    if (x % divider) == 0:
        bset1.SET(x)

print(bset1)
print("set bits from 0 to ", uncompressedSize," divisible by ", divider)
print("compressed size   :", bset1.size(), " bytes")
print("uncompressed size :", uncompressedSizeBytes, " bytes")
from BWT import BWT, invert_BWT
from RLE import rle, invert_rle

string = "^BANANA@"
transformed_string = BWT(string)
original_string = invert_BWT(transformed_string)

print string
print transformed_string
print original_string

rle_string = rle(transformed_string)
print rle_string
rle_original = invert_rle(rle_string)
print rle_original
    def send(self, original_text):
        print "original text"
        print original_text

        len_original_text = 1.0*len(original_text)

        # Create dictionary
        dictionary = Dictionary(original_text)

        # Dictionary encoding
        encoded_text = dictionary_encoding(original_text, dictionary)
        
        dict_freq = frequencies_val(original_text)

        print "Dictionary encoded"
        print encoded_text

        with open('Results/dictionary_encoding_output_and_compression_ratio.txt', 'w') as f:
            f.write("Encoded text: \n")
            f.write(encoded_text+"\n\nFrequencies after dictionary applied:\n")
            f.write(str(frequencies(encoded_text)))
            f.write("\n\nlen_original_text: "+str(len_original_text)+"\n")
            f.write("len_dictionary: "+str(len(encoded_text))+"\n")
            f.write("\n\nCompression ratio: "+str(len_original_text/len(encoded_text)))
        with open('dictionary_encoding_output.txt', 'w') as f:
            f.write(encoded_text)

        # Burrows-Wheeler Transform
        bwt_encoded_text = BWT(encoded_text)
        print "BWT:"
        print bwt_encoded_text
       
        with open('Results/bwt_output.txt', 'w') as f:
            f.write("frequencies: \n\n"+str(frequencies_from_dictionary(bwt_encoded_text))+"\n\nEncoded text:\n\n")
            f.write(str(self.send_text_to_list(bwt_encoded_text)))
            f.write("\n\nlen_original_text: "+str(len_original_text)+"\n")
            f.write("len_bwt_encoded_text: "+str(len(bwt_encoded_text))+"\n")
            f.write("Compression ratio: "+str(len_original_text/len(bwt_encoded_text)))

        # Run-length encoding
        rle_encoded_text = rle(bwt_encoded_text)

        l_dictionary = 0
        for e in dict_freq:
            l_dictionary += len(e)*dict_freq[e]
        l_original_text = len(original_text)
        l_RLE = len(rle_encoded_text)
        
        print "l_dictionary: ",l_original_text
        print "l_RLE: ",l_RLE

        with open('Results/rle_encoded_text_and_compression_ratio.txt', 'w') as f:

            f.write("RLE encoded text:\n")
            f.write(str(self.send_text_to_list(rle_encoded_text)))
            f.write("\n\nFrequencies: \n\n"+str(frequencies_from_dictionary(rle_encoded_text)))
            f.write("\n\nlen_original_text: "+str(l_original_text)+"\nlen_RLE: "+str(l_RLE))
            f.write("\n\nCompression ratio: "+str((1.0*l_original_text)/l_RLE))

        print "RLE"
        print rle_encoded_text

        ###### Encryption ######
        start_value = 9
        max_value = 83
        factor = 4

        k = KeyGenerator()
        key = k.generate_key(start_value,max_value,factor)
        #print key

        a = ReducedArrayEncryption(rle_encoded_text,key)
        encrypted = a.encrypt()
        encrypted_text = a.get_text_encrypted(encrypted[1])
        print "Encrypted"
        print encrypted_text


        with open('Results/Begum_Venkataramani_output.txt', 'w') as f:

            f.write("Begum_Venkataramani:\n")
            f.write(str(encrypted_text))
            f.write("\n\nFrequencies: \n\n"+str(frequencies_bv(encrypted_text)))
            f.write("\n\nlen_original_text: "+str(l_original_text)+"\nlen_RLE: "+str(len(encrypted_text)))
            f.write("\n\nCompression ratio: "+str((1.0*l_original_text)/len(encrypted_text)))

        ########################

        # Huffman coding
        huffman_encoded_text, huffman_root, huffman_codes = huffman_encode(encrypted_text) # The root will be necessary to decode
        print "Huffman"
        print huffman_encoded_text

        with open('Results/huffman_output.txt', 'w') as f:
            f.write("huffman_encoded_text:\n")
            f.write(str(huffman_encoded_text))
            f.write("\n\nCodes: \n\n"+str(huffman_codes))
            f.write("\n\nFrequencies: \n\n"+str(frequencies_from_dictionary(huffman_encoded_text)))
            f.write("\n\nlen_original_text: "+str(l_original_text)+"\nlen_RLE: "+str(len(huffman_encoded_text)))
            f.write("\n\nCompression ratio: "+str((1.0*l_original_text)/len(huffman_encoded_text)))

        return [huffman_encoded_text, huffman_root, key, encrypted, dictionary]