mirror of https://github.com/explosion/spaCy.git
* Add function to predict number of bits needed to encode message
This commit is contained in:
parent
b89b489bb4
commit
5a042ee0d3
|
@ -1,6 +1,7 @@
|
||||||
cimport cython
|
cimport cython
|
||||||
from libcpp.queue cimport priority_queue
|
from libcpp.queue cimport priority_queue
|
||||||
from libcpp.pair cimport pair
|
from libcpp.pair cimport pair
|
||||||
|
import numpy
|
||||||
|
|
||||||
from ..typedefs cimport attr_t
|
from ..typedefs cimport attr_t
|
||||||
|
|
||||||
|
@ -59,6 +60,16 @@ cdef class HuffmanCodec:
|
||||||
bits.extend(self.codes[i].bits, self.codes[i].length)
|
bits.extend(self.codes[i].bits, self.codes[i].length)
|
||||||
return bits
|
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):
|
def decode(self, bits, msg):
|
||||||
node = self.root
|
node = self.root
|
||||||
cdef int i = 0
|
cdef int i = 0
|
||||||
|
|
Loading…
Reference in New Issue