* Add function to predict number of bits needed to encode message

This commit is contained in:
Matthew Honnibal 2015-07-20 01:35:11 +02:00
parent b89b489bb4
commit 5a042ee0d3

View File

@ -1,6 +1,7 @@
cimport cython
from libcpp.queue cimport priority_queue
from libcpp.pair cimport pair
import numpy
from ..typedefs cimport attr_t
@ -59,6 +60,16 @@ cdef class HuffmanCodec:
bits.extend(self.codes[i].bits, self.codes[i].length)
return bits
def n_bits(self, msg, overhead=0):
cdef int i
length = 0
for word in msg:
if word not in self._map:
return numpy.nan
i = self._map[word]
length += self.codes[i].length
return length + overhead * len(msg)
def decode(self, bits, msg):
node = self.root
cdef int i = 0