Skip to main content

Python Huffman Algorithm Implementation

Project description

huffman-algorithm

Huffman algorithm is a python implementation of the huffman algorithm it's an efficient encoding algorithm for text.

GitHub License GitHub Workflow Status (with event) PyPI - Downloads GitHub Repo stars

✨ Key features

  • Count letters

  • Tree generation

  • Dictionary generation

  • Letter frequency calculation

  • String encoding

  • String decoding

  • Tree visualisation

It works with python 3.12 or later


🔧 Installation

It should work on any python3 version after the 1.10 but it's always good to have the latest version since it will be the one I'm sure it works on :)

⚙ Using PyPi

$ pip install huffman-algorithm

📚 Usage

📕 Dictionary generation

from huffman import Huffman

dictionary = Huffman.generate_dictionary("Hello World")

# How to see the dictionary ?
print(dictionary)    # Output: {'W': '000', 'd': '001', 'e': '010', 'r': '011', 'l': '10', 'o': '110', ' ': '1110', 'H': '1111'}

🌳 Tree generation

from huffman import Huffman

tree = Huffman.generate_tree("Hello World")

# How to see the tree ?
print(tree)    # Output: Node(character=None, frequency=11, children=[Node(character=None, frequency=4, children=[...]])

🔢 Letter frequency calculation

from huffman import Huffman

frequencies = Huffman.get_letters_frequency("Hello World")

# How to see the encoded string ?
print(frequencies)    # Output: {' ': 1, 'H': 1, 'W': 1, 'd': 1, 'e': 1, 'r': 1, 'o': 2, 'l': 3}

⚙️ String encoding

from huffman import Huffman

encoded = Huffman.encode("Hello World")

# How to see the encoded string ?
print(encoded)    # Output: '11110101010110111000011001110001'

⚙️ String decoding

from huffman import Huffman

encoded = Huffman.encode("Hello World")
dictionary = Huffman.generate_dictionary("Hello World")

decoded = Huffman.decode(encoded, dictionary)

# How to see the decoded string ?
print(decoded)    # Output: 'Hello World'

🍕 Contributing

All contribution are welcomed so consider looking at the source code on GitHub

🛡 License

This project is licensed under the MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

huffman_algorithm-1.0.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

huffman_algorithm-1.0.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file huffman_algorithm-1.0.0.tar.gz.

File metadata

  • Download URL: huffman_algorithm-1.0.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for huffman_algorithm-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c93804db6bc834b8b04572ce0f0fdd3d741b26b21735f840c659689ac6304686
MD5 03c04bf2b3d8d6af585fb94073bf2781
BLAKE2b-256 5b655e98a456aa632ca7444a5b818a9964b16fc98d1ed800bf22efadcacf65d3

See more details on using hashes here.

File details

Details for the file huffman_algorithm-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for huffman_algorithm-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e74a53f66bf695b3b5c961bd08a7a3f767dbdd7c7d7abaab508672062ef19472
MD5 aaa0d46eb77460bf7c3e99ed64cf2d98
BLAKE2b-256 5515b5104c0c536c19d0f35dc2edf6bffa2d7fed13913af194a73e898acbefa9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page