2015-07-18 05:09:56 +03:00
|
|
|
# cython: profile=True
|
2015-07-17 02:19:29 +03:00
|
|
|
from libc.stdint cimport uint32_t
|
|
|
|
from libc.stdint cimport uint64_t
|
|
|
|
from libc.math cimport exp as c_exp
|
|
|
|
from libcpp.queue cimport priority_queue
|
|
|
|
from libcpp.pair cimport pair
|
|
|
|
|
|
|
|
from cymem.cymem cimport Address, Pool
|
|
|
|
from preshed.maps cimport PreshMap
|
|
|
|
|
2015-07-18 23:43:18 +03:00
|
|
|
from ..attrs cimport ORTH, SPACY, TAG, HEAD, DEP, ENT_IOB, ENT_TYPE
|
2015-07-17 02:19:29 +03:00
|
|
|
from ..tokens.doc cimport Doc
|
|
|
|
from ..vocab cimport Vocab
|
|
|
|
from ..typedefs cimport attr_t
|
|
|
|
from .bits cimport BitArray
|
|
|
|
from .huffman cimport HuffmanCodec
|
|
|
|
|
|
|
|
from os import path
|
|
|
|
import numpy
|
|
|
|
|
|
|
|
cimport cython
|
|
|
|
|
|
|
|
|
|
|
|
# Format
|
|
|
|
# - Total number of bytes in message (32 bit int) --- handled outside this
|
|
|
|
# - Number of words (32 bit int)
|
|
|
|
# - Words, terminating in an EOL symbol, huffman coded ~12 bits per word
|
|
|
|
# - Spaces 1 bit per word
|
|
|
|
# - Attributes:
|
|
|
|
# POS tag
|
|
|
|
# Head offset
|
|
|
|
# Dep label
|
|
|
|
# Entity IOB
|
|
|
|
# Entity tag
|
|
|
|
|
|
|
|
|
|
|
|
cdef class _BinaryCodec:
|
2015-07-17 17:38:29 +03:00
|
|
|
def encode(self, attr_t[:] msg, BitArray bits):
|
2015-07-17 02:19:29 +03:00
|
|
|
cdef int i
|
2015-07-17 17:38:29 +03:00
|
|
|
for i in range(len(msg)):
|
|
|
|
bits.append(msg[i])
|
2015-07-17 02:19:29 +03:00
|
|
|
|
2015-07-17 22:20:48 +03:00
|
|
|
def decode(self, BitArray bits, attr_t[:] msg):
|
|
|
|
cdef int i = 0
|
|
|
|
for bit in bits:
|
|
|
|
msg[i] = bit
|
|
|
|
i += 1
|
|
|
|
if i == len(msg):
|
|
|
|
break
|
2015-07-17 02:19:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
cdef class _AttributeCodec:
|
|
|
|
cdef Pool mem
|
|
|
|
cdef attr_t* _keys
|
2015-07-17 22:20:48 +03:00
|
|
|
cdef dict _map
|
2015-07-17 02:19:29 +03:00
|
|
|
cdef HuffmanCodec _codec
|
|
|
|
|
|
|
|
def __init__(self, freqs):
|
2015-07-17 17:38:29 +03:00
|
|
|
self.mem = Pool()
|
2015-07-17 22:20:48 +03:00
|
|
|
cdef attr_t key
|
|
|
|
cdef float count
|
|
|
|
cdef pair[float, attr_t] item
|
2015-07-17 02:19:29 +03:00
|
|
|
|
2015-07-17 22:20:48 +03:00
|
|
|
cdef priority_queue[pair[float, attr_t]] items
|
2015-07-17 02:19:29 +03:00
|
|
|
|
|
|
|
for key, count in freqs:
|
|
|
|
item.first = count
|
|
|
|
item.second = key
|
|
|
|
items.push(item)
|
2015-07-17 17:38:29 +03:00
|
|
|
weights = numpy.ndarray(shape=(len(freqs),), dtype=numpy.float32)
|
2015-07-17 02:19:29 +03:00
|
|
|
self._keys = <attr_t*>self.mem.alloc(len(freqs), sizeof(attr_t))
|
2015-07-17 22:20:48 +03:00
|
|
|
self._map = {}
|
2015-07-17 02:19:29 +03:00
|
|
|
cdef int i = 0
|
|
|
|
while not items.empty():
|
|
|
|
item = items.top()
|
2015-07-17 17:38:29 +03:00
|
|
|
# We put freq first above, for sorting
|
2015-07-17 02:19:29 +03:00
|
|
|
self._keys[i] = item.second
|
2015-07-17 17:38:29 +03:00
|
|
|
weights[i] = item.first
|
|
|
|
self._map[self._keys[i]] = i
|
2015-07-17 02:19:29 +03:00
|
|
|
items.pop()
|
2015-07-17 17:38:29 +03:00
|
|
|
i += 1
|
2015-07-17 02:19:29 +03:00
|
|
|
self._codec = HuffmanCodec(weights)
|
|
|
|
|
2015-07-17 17:38:29 +03:00
|
|
|
def encode(self, attr_t[:] msg, BitArray dest):
|
2015-07-17 22:20:48 +03:00
|
|
|
cdef int i
|
2015-07-17 02:19:29 +03:00
|
|
|
for i in range(len(msg)):
|
2015-07-17 22:20:48 +03:00
|
|
|
msg[i] = self._map[msg[i]]
|
2015-07-17 17:38:29 +03:00
|
|
|
self._codec.encode(msg, dest)
|
2015-07-17 02:19:29 +03:00
|
|
|
|
2015-07-17 17:38:29 +03:00
|
|
|
def decode(self, BitArray bits, attr_t[:] dest):
|
2015-07-17 02:19:29 +03:00
|
|
|
cdef int i
|
2015-07-17 17:38:29 +03:00
|
|
|
self._codec.decode(bits, dest)
|
|
|
|
for i in range(len(dest)):
|
|
|
|
dest[i] = <attr_t>self._keys[dest[i]]
|
2015-07-17 02:19:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
cdef class Packer:
|
|
|
|
def __init__(self, Vocab vocab, list_of_attr_freqs):
|
|
|
|
self.vocab = vocab
|
|
|
|
codecs = []
|
2015-07-17 22:20:48 +03:00
|
|
|
attrs = []
|
2015-07-17 02:19:29 +03:00
|
|
|
|
|
|
|
for attr, freqs in list_of_attr_freqs:
|
2015-07-18 23:43:18 +03:00
|
|
|
if attr == SPACY:
|
2015-07-17 02:19:29 +03:00
|
|
|
codecs.append(_BinaryCodec())
|
|
|
|
else:
|
|
|
|
codecs.append(_AttributeCodec(freqs))
|
2015-07-17 22:20:48 +03:00
|
|
|
attrs.append(attr)
|
2015-07-17 02:19:29 +03:00
|
|
|
self._codecs = tuple(codecs)
|
2015-07-17 22:20:48 +03:00
|
|
|
self.attrs = tuple(attrs)
|
2015-07-17 02:19:29 +03:00
|
|
|
|
2015-07-17 17:38:29 +03:00
|
|
|
def pack(self, Doc doc):
|
|
|
|
array = doc.to_array(self.attrs)
|
2015-07-17 02:19:29 +03:00
|
|
|
cdef BitArray bits = BitArray()
|
2015-07-18 05:09:56 +03:00
|
|
|
cdef uint32_t length = len(doc)
|
|
|
|
bits.extend(length, 32)
|
2015-07-17 02:19:29 +03:00
|
|
|
for i, codec in enumerate(self._codecs):
|
2015-07-17 22:20:48 +03:00
|
|
|
codec.encode(array[:, i], bits)
|
2015-07-17 02:19:29 +03:00
|
|
|
return bits
|
|
|
|
|
2015-07-17 22:20:48 +03:00
|
|
|
def unpack(self, BitArray bits):
|
|
|
|
bits.seek(0)
|
2015-07-18 05:09:56 +03:00
|
|
|
cdef uint32_t length = bits.read32()
|
2015-07-17 22:20:48 +03:00
|
|
|
array = numpy.zeros(shape=(length, len(self._codecs)), dtype=numpy.int32)
|
|
|
|
for i, codec in enumerate(self._codecs):
|
|
|
|
codec.decode(bits, array[:, i])
|
|
|
|
doc = Doc.from_ids(self.vocab, array[:, 0], array[:, 1])
|
|
|
|
doc.from_array(self.attrs, array)
|
|
|
|
return doc
|