2021-10-26 10:29:27 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-05-13 05:15:27 +00:00
|
|
|
import sys
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-05-13 05:15:27 +00:00
|
|
|
class Letter:
|
2021-10-26 10:29:27 +00:00
|
|
|
def __init__(self, letter: str, freq: int):
|
|
|
|
self.letter: str = letter
|
|
|
|
self.freq: int = freq
|
|
|
|
self.bitstring: dict[str, str] = {}
|
2019-05-13 05:15:27 +00:00
|
|
|
|
2021-10-26 10:29:27 +00:00
|
|
|
def __repr__(self) -> str:
|
2019-10-05 05:14:13 +00:00
|
|
|
return f"{self.letter}:{self.freq}"
|
2019-05-13 05:15:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TreeNode:
|
2021-10-26 10:29:27 +00:00
|
|
|
def __init__(self, freq: int, left: Letter | TreeNode, right: Letter | TreeNode):
|
|
|
|
self.freq: int = freq
|
|
|
|
self.left: Letter | TreeNode = left
|
|
|
|
self.right: Letter | TreeNode = right
|
2019-05-13 05:15:27 +00:00
|
|
|
|
|
|
|
|
2021-10-26 10:29:27 +00:00
|
|
|
def parse_file(file_path: str) -> list[Letter]:
|
2019-05-16 11:20:42 +00:00
|
|
|
"""
|
2019-05-13 05:15:27 +00:00
|
|
|
Read the file and build a dict of all letters and their
|
2020-01-18 12:24:33 +00:00
|
|
|
frequencies, then convert the dict into a list of Letters.
|
2019-05-13 05:15:27 +00:00
|
|
|
"""
|
2021-10-26 10:29:27 +00:00
|
|
|
chars: dict[str, int] = {}
|
2019-05-13 05:15:27 +00:00
|
|
|
with open(file_path) as f:
|
|
|
|
while True:
|
|
|
|
c = f.read(1)
|
|
|
|
if not c:
|
|
|
|
break
|
2022-10-16 05:25:38 +00:00
|
|
|
chars[c] = chars[c] + 1 if c in chars else 1
|
2022-11-29 15:56:41 +00:00
|
|
|
return sorted((Letter(c, f) for c, f in chars.items()), key=lambda x: x.freq)
|
2019-05-13 05:15:27 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2021-10-26 10:29:27 +00:00
|
|
|
def build_tree(letters: list[Letter]) -> Letter | TreeNode:
|
2019-05-16 11:20:42 +00:00
|
|
|
"""
|
2019-05-13 05:15:27 +00:00
|
|
|
Run through the list of Letters and build the min heap
|
|
|
|
for the Huffman Tree.
|
|
|
|
"""
|
2021-10-26 10:29:27 +00:00
|
|
|
response: list[Letter | TreeNode] = letters # type: ignore
|
|
|
|
while len(response) > 1:
|
|
|
|
left = response.pop(0)
|
|
|
|
right = response.pop(0)
|
2019-05-13 05:15:27 +00:00
|
|
|
total_freq = left.freq + right.freq
|
|
|
|
node = TreeNode(total_freq, left, right)
|
2021-10-26 10:29:27 +00:00
|
|
|
response.append(node)
|
2022-11-29 15:56:41 +00:00
|
|
|
response.sort(key=lambda x: x.freq)
|
2021-10-26 10:29:27 +00:00
|
|
|
return response[0]
|
2019-05-13 05:15:27 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2021-10-26 10:29:27 +00:00
|
|
|
def traverse_tree(root: Letter | TreeNode, bitstring: str) -> list[Letter]:
|
2019-05-16 11:20:42 +00:00
|
|
|
"""
|
2019-05-13 05:15:27 +00:00
|
|
|
Recursively traverse the Huffman Tree to set each
|
2021-07-12 06:16:31 +00:00
|
|
|
Letter's bitstring dictionary, and return the list of Letters
|
2019-05-13 05:15:27 +00:00
|
|
|
"""
|
2022-11-20 11:00:27 +00:00
|
|
|
if isinstance(root, Letter):
|
2021-07-12 06:16:31 +00:00
|
|
|
root.bitstring[root.letter] = bitstring
|
2019-05-13 05:15:27 +00:00
|
|
|
return [root]
|
2021-10-26 10:29:27 +00:00
|
|
|
treenode: TreeNode = root # type: ignore
|
2019-05-13 05:15:27 +00:00
|
|
|
letters = []
|
2021-10-26 10:29:27 +00:00
|
|
|
letters += traverse_tree(treenode.left, bitstring + "0")
|
|
|
|
letters += traverse_tree(treenode.right, bitstring + "1")
|
2019-05-13 05:15:27 +00:00
|
|
|
return letters
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2021-10-26 10:29:27 +00:00
|
|
|
def huffman(file_path: str) -> None:
|
2019-05-16 11:20:42 +00:00
|
|
|
"""
|
2019-05-13 05:15:27 +00:00
|
|
|
Parse the file, build the tree, then run through the file
|
2021-07-12 06:16:31 +00:00
|
|
|
again, using the letters dictionary to find and print out the
|
2019-05-13 05:15:27 +00:00
|
|
|
bitstring for each letter.
|
|
|
|
"""
|
|
|
|
letters_list = parse_file(file_path)
|
|
|
|
root = build_tree(letters_list)
|
2021-07-12 06:16:31 +00:00
|
|
|
letters = {
|
|
|
|
k: v for letter in traverse_tree(root, "") for k, v in letter.bitstring.items()
|
|
|
|
}
|
|
|
|
print(f"Huffman Coding of {file_path}: ")
|
2019-05-13 05:15:27 +00:00
|
|
|
with open(file_path) as f:
|
|
|
|
while True:
|
|
|
|
c = f.read(1)
|
|
|
|
if not c:
|
|
|
|
break
|
2021-07-12 06:16:31 +00:00
|
|
|
print(letters[c], end=" ")
|
2019-05-13 05:15:27 +00:00
|
|
|
print()
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-05-13 05:15:27 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
# pass the file path to the huffman function
|
|
|
|
huffman(sys.argv[1])
|