-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.py
More file actions
54 lines (38 loc) · 1.49 KB
/
Copy pathencode.py
File metadata and controls
54 lines (38 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import sys
from functions.index import *
from pathlib import Path
from bitarray import bitarray
from time import time
import pickle
def main():
print(sys.argv)
script = sys.argv[0]
flag = sys.argv[1]
filename = sys.argv[2]
type_of_compression = 'h'
if(flag == "s"):
type_of_compression = "s"
txt = Path('./data/' + filename).read_text('Latin-1')
print("Archivo cargado, ", get_size(txt))
compress_time_1 =time()
dendograma = huffman(txt) if type_of_compression == "h" else shannon_fano(txt)
compress_time_2 =time()
print("dendograma, ", get_size(dendograma))
encode_time_1 = time()
dendograma_bitarray = {k: bitarray(v) for k, v in dendograma.items()}
bits = bitarray()
bits.encode(dendograma_bitarray, txt)
encode_time_2 = time()
print("bitarray, ", get_size(bits))
save_time_1 = time()
with open('./data/encode/' + type_of_compression + "_" + filename, 'wb') as fh:
bits.tofile(fh)
with open('./data/encode/d_' + type_of_compression + "_" + filename, 'wb') as f:
pickle.dump(dendograma, f)
save_time_2 = time()
print("Tiempo de Compresion(Shannon o Huffman): ", compress_time_2-compress_time_1, " segundos")
print("Tiempo de Codificacion a bitstram: ", encode_time_2-encode_time_1, " segundos")
print("Tiempo de Guardad en archivo: ", save_time_2-save_time_1)
print("Tiempo total: ", save_time_2 - compress_time_1, " segundos")
if __name__ == '__main__':
main()