Rakib Project
Rakib Project
Rakib Project
Topic:
Text Compression and Decompression
ID:
2214010017
Class:
Class 1, CS 202X, SIE
Name:
Ashfi Nazmus Rakib
Score:
Teacher:
Prof. Jun Zhang
Design Time:
Week 12~15, Semester 2, 2023~2024 Academic Year
-I-
武汉工程大学计算机科学与工程学院 综合设计报告
Table of Contents
Abstract ……………………………………………………………………………………...III
Chapter 1 Introduction …………………………………………………………………….. 1
Chapter 2 System Design …………………………………………………………………….. 3
2.1 Requirements ………………………………………………..………………………….…3
2.2 Main Functions ……………………………………………..………………………….…3
Chapter 3 Implementation ……………………………………………………………………..5
3.1 Implementations of Key Algorithms ………………………..………………………….…5
3.2Implementations of Main Data Structures …………………..………………………….…6
Chapter 4 Test and Result Analysis …………………………………………..………………..7
Conclusion .……………………………………………………..………………………….…9
Acknowledge …………………………………………………..………………………….…9
Reference ……….……………..………………………………..……………………….…10
- II -
武汉工程大学计算机科学与工程学院 综合设计报告
Abstract
- III -
Design Report of Integrated Application, WIT
Chapter 1 Introduction
Text compression is a fundamental aspect of data storage and transmission
we can assign unique codes to each character, reducing the overall size of the
text data.
and information theory, playing a vital role in efficient data storage and
transmission. With the exponential growth of digital data, the need for
used method for lossless data compression that offers an optimal solution
where more frequent characters are assigned shorter codes, and less
frequent characters are assigned longer codes. This method ensures that
-1-
武汉工程大学计算机科学与工程学院 综合设计报告
The applications of Huffman coding are extensive and include, but are not
limited to:
File Compression: Huffman coding is used in various file formats to reduce the
size of documents, images, and multimedia files without losing any information.
Data Storage: Storage devices benefit from Huffman coding by using less
physical space to store data, which is particularly useful in large-scale storage
systems.
Error Detection and Correction: Huffman coding can be combined with other
techniques to detect and correct errors in data transmission and storage.
This project aims to implement a Huffman coding system that can compress
in data compression.
-2-
Design Report of Integrated Application, WIT
2.1 Requirements
-3-
武汉工程大学计算机科学与工程学院 综合设计报告
-4-
武汉工程大学计算机科学与工程学院 综合设计报告
-5-
Design Report of Integrated Application, WIT
Chapter 3 Implementation
import heapq
import json
class HuffmanNode:
def __init__(self, char, freq):
self.char = char
self.freq = freq
self.left = None
self.right = None
def calculate_frequencies(text):
return {char: text.count(char) for char in text}
def build_huffman_tree(frequencies):
priority_queue = [HuffmanNode(char, freq) for char, freq in frequencies.items()]
heapq.heapify(priority_queue)
return priority_queue[0]
-6-
武汉工程大学计算机科学与工程学院 综合设计报告
code_map[root.char] = current_code
def load_from_file(filename):
with open(filename, 'r') as file:
return file.read()
-7-
武汉工程大学计算机科学与工程学院 综合设计报告
def test_huffman_coding_system():
text = "The quick brown fox jumps over the lazy dog."
frequencies = calculate_frequencies(text)
root = build_huffman_tree(frequencies)
codes = generate_codes(root)
save_to_file(codes, 'huffman_codes.json') # Save codes for
decoding
-8-
武汉工程大学计算机科学与工程学院 综合设计报告
loaded_codes = json.loads(load_from_file('huffman_codes.json'))
decoded_text = decode_text(encoded_text, root)
save_to_file(decoded_text, 'file2.txt')
Conclusion
-9-
武汉工程大学计算机科学与工程学院 综合设计报告
Acknowledge
Reference
- 10 -
武汉工程大学计算机科学与工程学院 综合设计报告
implementation tips. Here are some hypothetical references that one could
use:
Thomas, published by John Wiley & Sons, Inc. This textbook provides
coding.
- 11 -