Python Forum
I will accept any help with this task (compression code)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I will accept any help with this task (compression code)
#1
I need help with the compression code according to the simplified Huffman algorithm with a fixed code length
compression on the text loaded from the keyboard, then we write to the screen a simple console application under Windows
Reply
#2
Please show what you have tried so far.
We are glad to help, but expect you to make a good effort as well.
If you've already written some code, show what you've got, and where there is an issue.
buran likes this post
Reply
#3
instead of taking text from the string constant, I want user to be able to type any text from the keyboard, I would like to also add decompression
string = 'TRykTrak'

# Creating tree nodes
class NodeTree(object):

    def __init__(self, left=None, right=None):
        self.left = left
        self.right = right

    def children(self):
        return (self.left, self.right)

    def nodes(self):
        return (self.left, self.right)

    def __str__(self):
        return '%s_%s' % (self.left, self.right)


# Main function implementing huffman coding
def huffman_code_tree(node, left=True, binString=''):
    if type(node) is str:
        return {node: binString}
    (l, r) = node.children()
    d = dict()
    d.update(huffman_code_tree(l, True, binString + '0'))
    d.update(huffman_code_tree(r, False, binString + '1'))
    return d


# Calculating frequency
freq = {}
for c in string:
    if c in freq:
        freq[c] += 1
    else:
        freq[c] = 1

freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)

nodes = freq

while len(nodes) > 1:
    (key1, c1) = nodes[-1]
    (key2, c2) = nodes[-2]
    nodes = nodes[:-2]
    node = NodeTree(key1, key2)
    nodes.append((node, c1 + c2))

    nodes = sorted(nodes, key=lambda x: x[1], reverse=True)

huffmanCode = huffman_code_tree(nodes[0][0])

print(' Char | Huffman code ')
print('----------------------')
for (char, frequency) in freq:
    print(' %-4r |%12s' % (char, huffmanCode[char]))
Larz60+ write Feb-10-2021, 01:23 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time, please use bbcode tags on future posts.
Reply
#4
rather rhan reinventing the wheel on the huffman encoding algorithm, choose one of the available packages here (if allowed by your prof):
https://pypi.org/search/?q=huffman

I can't really suggest one over the other since I haven't used any of them, but with a quick glance this one looks like it might fit the bill: https://pypi.org/search/?q=huffman+%2B+encoding
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Program that allows to accept only 10 integers but loops if an odd number was entered gachicardo 4 3,635 Feb-24-2022, 10:40 AM
Last Post: perfringo
  MyProgrammingLab wont accept anything I put in chicks4 2 11,578 Feb-10-2019, 11:44 PM
Last Post: chicks4

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020