2015-07-18 02:09:56 +00:00
|
|
|
# cython: profile=True
|
2015-07-19 20:03:39 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2015-07-19 23:36:22 +00:00
|
|
|
from libc.stdint cimport uint32_t, int32_t
|
2015-07-16 23:19:29 +00:00
|
|
|
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-19 20:03:39 +00:00
|
|
|
from preshed.counter cimport PreshCounter
|
2015-07-22 23:12:00 +00:00
|
|
|
import json
|
2015-07-16 23:19:29 +00:00
|
|
|
|
2015-07-19 13:18:17 +00:00
|
|
|
from ..attrs cimport ORTH, ID, SPACY, TAG, HEAD, DEP, ENT_IOB, ENT_TYPE
|
2015-07-16 23:19:29 +00:00
|
|
|
from ..tokens.doc cimport Doc
|
|
|
|
from ..vocab cimport Vocab
|
2015-07-19 13:18:17 +00:00
|
|
|
from ..structs cimport LexemeC
|
2015-07-16 23:19:29 +00:00
|
|
|
from ..typedefs cimport attr_t
|
|
|
|
from .bits cimport BitArray
|
|
|
|
from .huffman cimport HuffmanCodec
|
|
|
|
|
|
|
|
from os import path
|
|
|
|
import numpy
|
2015-07-19 13:18:17 +00:00
|
|
|
from .. import util
|
2015-07-16 23:19:29 +00:00
|
|
|
|
|
|
|
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 14:38:29 +00:00
|
|
|
def encode(self, attr_t[:] msg, BitArray bits):
|
2015-07-16 23:19:29 +00:00
|
|
|
cdef int i
|
2015-07-17 14:38:29 +00:00
|
|
|
for i in range(len(msg)):
|
|
|
|
bits.append(msg[i])
|
2015-07-16 23:19:29 +00:00
|
|
|
|
2015-07-17 19:20:48 +00: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-16 23:19:29 +00:00
|
|
|
|
|
|
|
|
2015-07-19 13:18:17 +00:00
|
|
|
def _gen_orths(Vocab vocab):
|
|
|
|
cdef attr_t orth
|
|
|
|
cdef size_t addr
|
|
|
|
for orth, addr in vocab._by_orth.items():
|
|
|
|
lex = <LexemeC*>addr
|
|
|
|
yield orth, c_exp(lex.prob)
|
|
|
|
|
|
|
|
|
2015-07-19 20:03:39 +00:00
|
|
|
def _gen_chars(Vocab vocab):
|
|
|
|
cdef attr_t orth
|
|
|
|
cdef size_t addr
|
2015-07-24 01:48:23 +00:00
|
|
|
char_weights = {i: 1e-20 for i in range(256)}
|
2015-07-19 20:03:39 +00:00
|
|
|
cdef unicode string
|
2015-07-19 20:39:45 +00:00
|
|
|
cdef bytes char
|
|
|
|
cdef bytes utf8_str
|
2015-07-19 20:03:39 +00:00
|
|
|
for orth, addr in vocab._by_orth.items():
|
|
|
|
lex = <LexemeC*>addr
|
|
|
|
string = vocab.strings[lex.orth]
|
2015-07-19 20:39:45 +00:00
|
|
|
utf8_str = string.encode('utf8')
|
|
|
|
for char in utf8_str:
|
2015-07-24 01:48:23 +00:00
|
|
|
char_weights.setdefault(ord(char), 0.0)
|
|
|
|
char_weights[ord(char)] += c_exp(lex.prob)
|
|
|
|
char_weights[ord(' ')] += c_exp(lex.prob)
|
2015-07-19 20:03:39 +00:00
|
|
|
return char_weights.items()
|
|
|
|
|
|
|
|
|
2015-07-16 23:19:29 +00:00
|
|
|
cdef class Packer:
|
2015-07-19 23:36:22 +00:00
|
|
|
def __init__(self, Vocab vocab, attr_freqs, char_freqs=None):
|
|
|
|
if char_freqs is None:
|
|
|
|
char_freqs = _gen_chars(vocab)
|
2015-07-16 23:19:29 +00:00
|
|
|
self.vocab = vocab
|
2015-07-19 20:03:39 +00:00
|
|
|
self.orth_codec = HuffmanCodec(_gen_orths(vocab))
|
2015-07-19 23:36:22 +00:00
|
|
|
self.char_codec = HuffmanCodec(char_freqs)
|
2015-07-19 13:18:17 +00:00
|
|
|
|
2015-07-19 20:03:39 +00:00
|
|
|
codecs = []
|
|
|
|
attrs = []
|
2015-07-19 13:18:17 +00:00
|
|
|
for attr, freqs in sorted(attr_freqs):
|
|
|
|
if attr in (ORTH, ID, SPACY):
|
|
|
|
continue
|
2015-07-19 20:03:39 +00:00
|
|
|
codecs.append(HuffmanCodec(freqs))
|
2015-07-17 19:20:48 +00:00
|
|
|
attrs.append(attr)
|
2015-07-16 23:19:29 +00:00
|
|
|
self._codecs = tuple(codecs)
|
2015-07-17 19:20:48 +00:00
|
|
|
self.attrs = tuple(attrs)
|
2015-07-16 23:19:29 +00:00
|
|
|
|
2015-07-17 14:38:29 +00:00
|
|
|
def pack(self, Doc doc):
|
2015-07-21 18:22:45 +00:00
|
|
|
bits = self._orth_encode(doc)
|
|
|
|
if bits is None:
|
|
|
|
bits = self._char_encode(doc)
|
2015-07-20 01:27:59 +00:00
|
|
|
cdef int i
|
|
|
|
if self.attrs:
|
|
|
|
array = doc.to_array(self.attrs)
|
|
|
|
for i, codec in enumerate(self._codecs):
|
2015-07-22 23:12:00 +00:00
|
|
|
codec.encode(array[:, i], bits)
|
|
|
|
return bits.as_bytes()
|
|
|
|
|
2015-07-24 01:48:23 +00:00
|
|
|
def unpack(self, data):
|
2015-07-22 23:12:00 +00:00
|
|
|
doc = Doc(self.vocab)
|
|
|
|
self.unpack_into(data, doc)
|
|
|
|
return doc
|
2015-07-16 23:19:29 +00:00
|
|
|
|
2015-07-24 01:48:23 +00:00
|
|
|
def unpack_into(self, byte_string, Doc doc):
|
2015-07-22 23:12:00 +00:00
|
|
|
bits = BitArray(byte_string)
|
2015-07-17 19:20:48 +00:00
|
|
|
bits.seek(0)
|
2015-07-19 23:36:22 +00:00
|
|
|
cdef int32_t length = bits.read32()
|
|
|
|
if length >= 0:
|
2015-07-22 23:12:00 +00:00
|
|
|
self._orth_decode(bits, length, doc)
|
2015-07-19 23:36:22 +00:00
|
|
|
else:
|
2015-07-22 23:12:00 +00:00
|
|
|
self._char_decode(bits, -length, doc)
|
|
|
|
|
2015-07-19 20:03:39 +00:00
|
|
|
array = numpy.zeros(shape=(len(doc), len(self._codecs)), dtype=numpy.int32)
|
2015-07-17 19:20:48 +00:00
|
|
|
for i, codec in enumerate(self._codecs):
|
2015-07-22 23:12:00 +00:00
|
|
|
codec.decode(bits, array[:, i])
|
2015-07-19 20:03:39 +00:00
|
|
|
|
|
|
|
doc.from_array(self.attrs, array)
|
|
|
|
return doc
|
|
|
|
|
2015-07-21 18:22:45 +00:00
|
|
|
def _orth_encode(self, Doc doc):
|
2015-07-27 00:12:01 +00:00
|
|
|
for t in doc:
|
|
|
|
if t.is_oov:
|
|
|
|
return None
|
2015-07-19 20:39:45 +00:00
|
|
|
cdef BitArray bits = BitArray()
|
2015-07-19 23:36:22 +00:00
|
|
|
cdef int32_t length = len(doc)
|
|
|
|
bits.extend(length, 32)
|
2015-07-21 18:22:45 +00:00
|
|
|
orths = doc.to_array([ORTH])
|
|
|
|
n_bits = self.orth_codec.encode_int32(orths[:, 0], bits)
|
|
|
|
if n_bits == 0:
|
|
|
|
return None
|
2015-07-19 20:39:45 +00:00
|
|
|
for token in doc:
|
|
|
|
bits.append(bool(token.whitespace_))
|
|
|
|
return bits
|
|
|
|
|
2015-07-21 18:22:45 +00:00
|
|
|
def _char_encode(self, Doc doc):
|
|
|
|
cdef bytes utf8_str = doc.string.encode('utf8')
|
2015-07-19 20:39:45 +00:00
|
|
|
cdef BitArray bits = BitArray()
|
2015-07-19 23:36:22 +00:00
|
|
|
cdef int32_t length = len(utf8_str)
|
|
|
|
# Signal chars with negative length
|
|
|
|
bits.extend(-length, 32)
|
2015-07-27 19:53:38 +00:00
|
|
|
self.char_codec.encode(bytearray(utf8_str), bits)
|
2015-07-20 01:27:59 +00:00
|
|
|
cdef int i, j
|
|
|
|
for i in range(doc.length):
|
2015-11-03 13:17:13 +00:00
|
|
|
for j in range(doc.c[i].lex.length-1):
|
2015-07-19 20:03:39 +00:00
|
|
|
bits.append(False)
|
|
|
|
bits.append(True)
|
2015-11-03 13:17:13 +00:00
|
|
|
if doc.c[i].spacy:
|
2015-07-19 20:03:39 +00:00
|
|
|
bits.append(False)
|
2015-07-19 20:39:45 +00:00
|
|
|
return bits
|
2015-07-19 20:03:39 +00:00
|
|
|
|
2015-07-22 23:12:00 +00:00
|
|
|
def _orth_decode(self, BitArray bits, int32_t n, Doc doc):
|
|
|
|
cdef attr_t[:] orths = numpy.ndarray(shape=(n,), dtype=numpy.int32)
|
|
|
|
self.orth_codec.decode_int32(bits, orths)
|
|
|
|
cdef int i
|
|
|
|
cdef bint space
|
|
|
|
spaces = iter(bits)
|
|
|
|
for i in range(n):
|
|
|
|
orth = orths[i]
|
2015-07-24 01:48:23 +00:00
|
|
|
space = next(spaces)
|
2015-07-22 23:12:00 +00:00
|
|
|
lex = self.vocab.get_by_orth(doc.mem, orth)
|
|
|
|
doc.push_back(lex, space)
|
|
|
|
return doc
|
|
|
|
|
2015-07-27 19:43:58 +00:00
|
|
|
def _char_decode(self, BitArray bits, int32_t n_bytes, Doc doc):
|
|
|
|
cdef bytearray utf8_str = bytearray(n_bytes)
|
2015-07-20 01:27:59 +00:00
|
|
|
self.char_codec.decode(bits, utf8_str)
|
2015-07-19 20:03:39 +00:00
|
|
|
|
2015-07-19 20:39:45 +00:00
|
|
|
cdef unicode string = utf8_str.decode('utf8')
|
2015-07-19 20:03:39 +00:00
|
|
|
cdef int start = 0
|
|
|
|
cdef bint is_spacy
|
2015-07-27 19:43:58 +00:00
|
|
|
cdef int n_unicode_chars = len(string)
|
2015-07-20 01:27:59 +00:00
|
|
|
cdef int i = 0
|
|
|
|
cdef bint is_end_token
|
|
|
|
for is_end_token in bits:
|
2015-07-19 20:03:39 +00:00
|
|
|
if is_end_token:
|
2015-07-22 11:39:17 +00:00
|
|
|
span = string[start:i+1]
|
2015-07-22 23:12:00 +00:00
|
|
|
lex = self.vocab.get(doc.mem, span)
|
2015-07-27 19:43:58 +00:00
|
|
|
is_spacy = (i+1) < n_unicode_chars and string[i+1] == u' '
|
2015-07-22 23:12:00 +00:00
|
|
|
doc.push_back(lex, is_spacy)
|
2015-07-19 20:03:39 +00:00
|
|
|
start = i + 1 + is_spacy
|
2015-07-20 01:27:59 +00:00
|
|
|
i += 1
|
2015-07-27 19:43:58 +00:00
|
|
|
if i >= n_unicode_chars:
|
2015-07-20 01:27:59 +00:00
|
|
|
break
|
2015-07-22 23:12:00 +00:00
|
|
|
return doc
|