Coder Social home page Coder Social logo

Comments (2)

robamler avatar robamler commented on June 19, 2024

Thank you for reporting! Could you expand on your use case? The point of any source coding encoder is to convert a message (i.e., a sequence of symbols) to a bit string. The message can be of arbitrary length. In particular, when using a symbol code like Huffman coding, there's nothing wrong with encoding a message that consists of only a single symbol:

import constriction
import numpy as np

def single_code_word(symbol, code_book):
    encoder = constriction.symbol.QueueEncoder()
    encoder.encode_symbol(symbol, code_book)
    code_word, length = encoder.get_compressed()
    return f'{code_word[0]:0{length}b}'[::-1] # works for code words of length <=32

probabilities = np.array([0.1, 0.2, 0.3, 0.4])
code_book = constriction.symbol.huffman.EncoderHuffmanTree(probabilities)
print({symbol: single_code_word(symbol, code_book) for symbol in range(4)})

Are you looking for a more convenient way to do this? If so, I'd be curious which of the following best describes your use case:

  1. being able to call dict(tree) where tree is an EncoderHuffmanTree, and getting a dictionary that maps symbols to some representations of bit strings (like in the above example);
  2. having a method on EncoderHuffmanTree that takes a symbol as argument and outputs some representation of this symbol's code word;
  3. having a simple way to summarize an EncoderHuffmanTree in a human-readable string for debugging; or
  4. something else?

from constriction.

robamler avatar robamler commented on June 19, 2024

Closing this for now because it's not actionable without further information. As mentioned in my last comment, the specific requested feature already exists, and while it is very well possible that the existing solution may not be suitable for all use cases, further information is needed to clarify what exactly is missing. Without clarification of the intended use case, any attempt from my side to guess and address the underlying issue would likely run into the XY problem.

Please feel free to reopen if you can clarify the broader picture of what you're trying to achieve.

from constriction.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.