Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added README and fixed compressor and decompressor #379

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README(1).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Huffman compressor:
Use huffman.cpp to generate codes file and use compressor.py/decompressor.py for compression/decompression
Note: we only consider spaces or alphabets here and not numbers.
63 changes: 63 additions & 0 deletions compressor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import math
def compress(ifile, codes, ofile):
symbol_codes = {}
with open(codes, 'r') as f:
for line in f:
last_space_index = line.rfind(' ')
if last_space_index != -1:
symbol = line[:last_space_index]
code = line[last_space_index + 1:].strip()
symbol_codes[symbol] = code
with open(ifile, 'r') as f:
itext = f.read().strip()
compressed = ''
for c in itext:
if c.isalpha() :
c=c.upper()
if c in symbol_codes:
compressed += symbol_codes[c]
compressed_bytes = compress_bits(compressed)
with open(ofile, 'wb') as f:
f.write(compressed_bytes)
entropy(itext,compressed)
def entropy(a,b):
D = {}
E = {}
for i in a:
if i not in D:
D[i]=1
else:
D[i]+=1
for i in b:
if i not in E:
E[i]=1
else:
E[i]+=1
entropya=0
for i in D:
entropya+=(D[i]/len(a))*math.log2((len(a)/D[i]))
entropyb=0
for i in E:
entropyb+=(E[i]/len(b))*math.log2((len(b)/E[i]))
print("The information gain from compression is: ",entropya-entropyb)
def compress_bits(compressed):
compressed_bytes = bytearray()
current_byte = 0
bit_count = 0
for bit in compressed:
current_byte <<= 1
current_byte |= int(bit)
bit_count += 1
if bit_count == 8:
compressed_bytes.append(current_byte)
current_byte = 0
bit_count = 0
if bit_count > 0:
current_byte <<= (8 - bit_count)
compressed_bytes.append(current_byte)
return bytes(compressed_bytes)
ifile = input("Enter your input filename: ")
codes = input("Enter the filename where you have saved your codes: ")
ofile = input("Enter the filename where you want your compressed document saved: ")
compress(ifile, codes, ofile)
print("Compressed code has been written to:", ofile)
30 changes: 30 additions & 0 deletions decompressor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def decompress(ifile, codes,ofile):
symbol_codes = {}
with open(codes, 'r') as f:
for line in f:
last_space_index = line.rfind(' ')
if last_space_index != -1:
symbol = line[:last_space_index]
code = line[last_space_index + 1:].strip()
symbol_codes[code] = symbol
with open(ifile, 'rb') as f: # Open the compressed file in binary mode
compressed = f.read()

# Convert binary data to a binary string
compressed_bits = ''.join(format(byte, '08b') for byte in compressed)
decompressed = ''
current = ''
for bit in compressed_bits:
current += bit
if current in symbol_codes.keys():
decompressed += symbol_codes[current]
current = ''
with open(ofile, 'w') as f:
f.write(decompressed)

ifile = input("Enter your input (compressed) filename: ")
codes = input("Enter the filename where you have saved your codes: ")
ofile = input("Enter the filename where you want your decompressed document saved: ")
decompress(ifile, codes,ofile)
print("Decompressed code has been written to:", ofile)

Loading