def test_add_mid(self): myList = array_list.empty_list() myList.size = 3 myList.array[0] = 1 myList.array[1] = 2 myList.array[2] = 3 compareList = array_list.empty_list() compareList.size = 4 compareList.array[0] = 1 compareList.array[1] = 2 compareList.array[2] = 99 compareList.array[3] = 3 self.assertEqual(array_list.add(myList, 2, 99), compareList)
def count_occurances(file_name): empty_lst = array_list.empty_list() empty_lst.array = [0] * 256 empty_lst.capacity = 256 character_lst = array_list.empty_list() character_lst.array = [0] * 256 character_lst.capacity = 256 file = open(file_name, "r") for line in file: for word in line: character_lst.array[ord(word)] += 1 file.close() if empty_lst == character_lst: return array_list.empty_list() return character_lst
def huffman_decode(input_string, output_string): decoded_str = "" file = open(output_string, "w") occurance_list = array_list.empty_list() occurance_list.capacity = 256 occurance_list.array = [0] * 256 hb_reader = HuffmanBitsReader(input_string) num_character = hb_reader.read_byte() if num_character == 0: try: ascii_val = hb_reader.read_byte() except: file.close() hb_reader.close() return decoded_str #decoded_str += chr(ascii_val) #file.write(decoded_str) #file.close() #return decoded_str for counter in range(num_character): ascii_val = hb_reader.read_byte() occurance = hb_reader.read_int() occurance_list.array[ascii_val] = occurance huffman_tree = build_huffman(occurance_list) num_char = huffman_tree.freq while len(decoded_str) != num_char: decoded_str += huffman_to_string(huffman_tree, hb_reader) file.write(decoded_str) file.close() hb_reader.close() return decoded_str
def test_character_binary_conversion(self): HT1 = Node( ' ', 13, Node(' ', 6, Leaf(' ', 3), Leaf('b', 3)), Node('a', 7, Node('c', 3, Leaf('d', 1), Leaf('c', 2)), Leaf('a', 4))) char_lst = array_list.empty_list() char_lst.array = [ None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, '00', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, '11', '01', '101', '100', None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None ] char_lst.capacity = 256 self.assertEqual(character_binary_conversion(HT1), char_lst)
def count_occurrences(textfile): #inFile = HuffmanBitsReader(textfile) try: inFile = open(textfile, 'r') except: print('File Not Found') exit() #inFile = inFile.readlines() #the ASCII index is what character it is frequency = array_list.empty_list() frequency = ([None] * 256) #char = string.printable #a list of ascII for line in inFile: for letter in line: #line.rstrip(): index = ord(letter) if frequency[index] == None: frequency[index] = 0 frequency[index] += 1 inFile.close() ''' for letter in char: if letter != '\n': count = count_letter(inFile, letter) (character, freq) = (letter, count) if freq > 0: frequency[ord(character)] = freq ''' return frequency
def huffman_decode(inf, outf): hb_reader = HuffmanBitsReader(inf) outFile = open(outf, 'w') new = array_list.empty_list() char_num = hb_reader.read_byte() if char_num == 0: hb_reader.close() outFile.close() return None for i in range(char_num): index = hb_reader.read_byte() occ = hb_reader.read_int() new = array_list.set(new, index, occ) x = build_leaf(new, comes_before) lst = build_nodes(x) string = '' if type(lst) == Node: temp = lst for i in range(check_size(new, chr_code(lst, new))): bit = hb_reader.read_bit() if bit == True: temp = temp.right else: temp = temp.left if type(temp) == Leaf: string += temp.character temp = lst if type(lst) == Leaf: for i in range(lst.occurrence): string += lst.character outFile.write(string) hb_reader.close() outFile.close() return None
def build_huffman(OccuranceList): if OccuranceList == array_list.empty_list(): return None huffman_linked_list = linked_list.empty_list() for index, occurance in enumerate(OccuranceList.array): if occurance != 0: temp_leaf = Leaf( chr(index), occurance) #figure out how to go from into to asciib huffman_linked_list = linked_list.insert_sorted( huffman_linked_list, temp_leaf, comes_before) while linked_list.length(huffman_linked_list) != 1: temp_leaf_1 = linked_list.get(huffman_linked_list, 0) temp_leaf_2 = linked_list.get(huffman_linked_list, 1) huffman_linked_list = linked_list.remove_list(huffman_linked_list, 0) huffman_linked_list = linked_list.remove_list(huffman_linked_list, 0) total_freq = temp_leaf_1.freq + temp_leaf_2.freq if comes_before(temp_leaf_1, temp_leaf_2): if ord(temp_leaf_1.char) < ord(temp_leaf_2.char): temp_node = Node(temp_leaf_1.char, total_freq, temp_leaf_1, temp_leaf_2) else: temp_node = Node(temp_leaf_2.char, total_freq, temp_leaf_1, temp_leaf_2) huffman_linked_list = linked_list.insert_sorted( huffman_linked_list, temp_node, comes_before) return huffman_linked_list.first
def count(inf): inFile = open(inf, 'r') if inFile.readline() == '': inFile.close() return array_list.empty_list() else: inFile.close() inFile = open(inf, 'r') new = array_list.empty_list() for temp in inFile: for char in range(len(temp)): index = ord(temp[char]) val = array_list.get(new, index) if val == None: new = array_list.set(new, index, 1) else: new = array_list.set(new, index, val + 1) inFile.close() return new
def test_count_occurances(self): lst_occurance = array_list.empty_list() lst_occurance.array = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] lst_occurance.capacity = 256 self.assertEqual(count_occurances("test_text.txt"), lst_occurance) self.assertEqual(count_occurances("empty_text.txt"), array_list.empty_list())
def test_build_huffman(self): HT1 = Node( ' ', 13, Node(' ', 6, Leaf(' ', 3), Leaf('b', 3)), Node('a', 7, Node('c', 3, Leaf('d', 1), Leaf('c', 2)), Leaf('a', 4))) HT2 = Leaf('a', 1) empty_lst = array_list.empty_list() self.assertEqual(build_huffman(count_occurances("test_ez.txt")), HT1) self.assertEqual(build_huffman(count_occurances("one_char_text.txt")), HT2) self.assertEqual(build_huffman(empty_lst), None)
def test_length_of_decode(self): hb_reader = HuffmanBitsReader('test_encoded.bin') new = array_list.empty_list() char_num = hb_reader.read_byte() for i in range(char_num): index = hb_reader.read_byte() occ = hb_reader.read_int() new = array_list.set(new, index, occ) x = build_leaf(new, comes_before) lst = build_nodes(x) hb_reader.close() self.assertEqual(check_size(new, chr_code(lst, new)), 29)
def chr_code(nd, x): new = array_list.empty_list() if type(nd) == Leaf: for i in range(x.capacity): if array_list.get(x, i) != None: new = array_list.set(new, i, '0') return new else: for i in range(x.capacity): if array_list.get(x, i) != None: new = array_list.set(new, i, indv_code(nd, chr(i))) return new
def final_array(inputArray, inputList): newArray = array_list.empty_list() newArray.capacity = 256 newArray.array = [None] * 256 for x in range(length(inputList)): temp = ord(inputList.first[0]) for x in range(inputArray.capacity): if x == temp: temp2 = inputArray.array[x - 1] newArray.array[x] = (inputList.first[1], temp2) newArray.size += 1 inputList = inputList.rest return newArray
def character_binary_conversion(HuffmanTree): #if type(HuffmanTree) is Leaf: # return binary_lst = array_list.empty_list() char_lst = pre_order_traversal(HuffmanTree) #if len(char_lst) == 1: # return binary_lst binary_lst.array = [None] * 256 binary_lst.capacity = 256 codes_lst = bin_pre_order_traversal(HuffmanTree).split("|")[1:] for index, leaf_char in enumerate(char_lst): binary_lst.array[ord(leaf_char)] = codes_lst[index] return binary_lst
def txt_to_freq(inputFile): f = open(inputFile, 'r') allLines = f.read() newList = array_list.empty_list() newList.array = [0] * 256 newList.capacity = 256 for x in allLines: temp = ord(x) newList.array[temp - 1] += 1 newList.size += 1 f.close() return newList
def huffman_decode(input, output): read_from = HuffmanBitsReader(input) write_to = open(output, 'w') number_of_codes = read_from.read_byte() times = 0 occur_list = array_list.empty_list() occur_list = ([None] * 256) total_characters = 0 while times < number_of_codes: char = read_from.read_byte() freq = read_from.read_int() total_characters += freq times += 1 occur_list = array_list.set(occur_list, char, freq) if total_characters == 0: write_to.write('') write_to.close() read_from.close() return sorted_list = make_sorted_list(occur_list) pairtree = join_leaves(sorted_list) tree = pairtree.first temp_tree = tree #print(total_characters) #read bit by bit and get the code bit = True code = '' chars = 0 #write_to.write('\n') while bit == True or bit == False and chars < total_characters: bit = read_from.read_bit() if type(tree) != type(Leaf(1, 2)): if bit == True: tree = tree.right code += '1' elif bit == False: tree = tree.left code += '0' if type(tree) == type(Leaf('a', 2)): char = chr(tree.character) write_to.write(char) tree = temp_tree chars += 1 #print(code) #write_to.write('\n') write_to.close() read_from.close()
def test_count(self): y = array_list.empty_list() y = array_list.add(y, ord(' '), 3) y = array_list.add(y, ord('a'), 4) y = array_list.add(y, ord('b'), 3) y = array_list.add(y, ord('c'), 2) y = array_list.add(y, ord('d'), 1) y.size = 0 self.assertEqual(count('test.txt'), y) x = count('test.txt') y = build_leaf(x, comes_before) self.assertEqual(build_leaf(x, comes_before), y) x = build_leaf(x, comes_before) y = build_nodes(x) self.assertEqual(build_nodes(x), y) lst = build_nodes(x) z = array_list.empty_list() z = array_list.add(z, ord(' '), '00') z = array_list.add(z, ord('a'), '01') z = array_list.add(z, ord('b'), '100') z = array_list.add(z, ord('c'), '101') z = array_list.add(z, ord('d'), '11') z.size = 0 self.assertEqual(chr_code(lst, count('test.txt')), z)
def sorted_leaf_list(list): interval = len(list) sorted_list = array_list.empty_list() for i in range(interval): if list[i] != 0: if sorted_list.length == 0: array_list.add(sorted_list, 0, Leaf(str(i), list[i])) else: x = 0 for d in range(sorted_list.length): if comes_before(Leaf(str(i), list[i]), sorted_list.lst[d]) and x == 0: array_list.add(sorted_list, d, Leaf(str(i), list[i])) x = 1 if x == 0: length_s = sorted_list.length array_list.add(sorted_list, length_s, Leaf(str(i), list[i])) return sorted_list
def huffman_decode(inFile, outFile): f = open(outFile, "w") hb_reader = HuffmanBitsReader(inFile) finalString = "" try: totalOccurence = hb_reader.read_byte() newArray = array_list.empty_list() newArray.capacity = 256 newArray.array = [0] * 256 for x in range(0, totalOccurence): myCount = hb_reader.read_byte() newFreak = hb_reader.read_int() newArray.array[myCount - 1] = newFreak Dtree = build_tree(HuffmanTree(array_to_sortedList(newArray))) totalChr = Dtree.root.count ogTree = Dtree.root current = Dtree.root while totalChr != 0: if type(current) == Leaf: finalString += chr(current.ordinal) totalChr -= 1 current = ogTree else: oneBit = hb_reader.read_bit() if oneBit is True: current = current.right else: current = current.left f.write(finalString) hb_reader.close() f.close() return None except: #isempty f.close() hb_reader.close() return None
def test_add_IE3(self): with self.assertRaises(IndexError): array_list.add(array_list.empty_list(), -5, 99)
def test_length1(self): myList = array_list.empty_list() self.assertEqual(array_list.length(myList), 0)
def test_array_repr(self): myPair = array_list.empty_list() self.assertEqual( myPair.__repr__(), "Array([None, None, None, None, None, None, None, None, None, None], Size 0)" )