2017-01-12 23:58:55 +03:00
|
|
|
# coding: utf-8
|
2015-07-17 22:21:10 +03:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-01-12 23:58:55 +03:00
|
|
|
from ...serialize.packer import _BinaryCodec
|
|
|
|
from ...serialize.huffman import HuffmanCodec
|
|
|
|
from ...serialize.bits import BitArray
|
2015-07-17 22:21:10 +03:00
|
|
|
|
2017-01-12 23:58:55 +03:00
|
|
|
import numpy
|
|
|
|
import pytest
|
2015-07-17 22:21:10 +03:00
|
|
|
|
|
|
|
|
2017-01-12 23:58:55 +03:00
|
|
|
def test_serialize_codecs_binary():
|
2015-07-17 22:21:10 +03:00
|
|
|
codec = _BinaryCodec()
|
|
|
|
bits = BitArray()
|
2017-01-12 23:58:55 +03:00
|
|
|
array = numpy.array([0, 1, 0, 1, 1], numpy.int32)
|
|
|
|
codec.encode(array, bits)
|
2015-07-17 22:21:10 +03:00
|
|
|
result = numpy.array([0, 0, 0, 0, 0], numpy.int32)
|
2015-07-17 22:31:44 +03:00
|
|
|
bits.seek(0)
|
|
|
|
codec.decode(bits, result)
|
2017-01-12 23:58:55 +03:00
|
|
|
assert list(array) == list(result)
|
2015-07-17 22:21:10 +03:00
|
|
|
|
|
|
|
|
2017-01-12 23:58:55 +03:00
|
|
|
def test_serialize_codecs_attribute():
|
|
|
|
freqs = {'the': 10, 'quick': 3, 'brown': 4, 'fox': 1, 'jumped': 5,
|
|
|
|
'over': 8, 'lazy': 1, 'dog': 2, '.': 9}
|
|
|
|
int_map = {'the': 0, 'quick': 1, 'brown': 2, 'fox': 3, 'jumped': 4,
|
|
|
|
'over': 5, 'lazy': 6, 'dog': 7, '.': 8}
|
2015-07-17 22:21:10 +03:00
|
|
|
|
2015-07-20 02:38:15 +03:00
|
|
|
codec = HuffmanCodec([(int_map[string], freq) for string, freq in freqs.items()])
|
2015-07-17 22:21:10 +03:00
|
|
|
bits = BitArray()
|
2017-01-12 23:58:55 +03:00
|
|
|
array = numpy.array([1, 7], dtype=numpy.int32)
|
|
|
|
codec.encode(array, bits)
|
2015-07-17 22:21:10 +03:00
|
|
|
result = numpy.array([0, 0], dtype=numpy.int32)
|
2015-07-17 22:31:44 +03:00
|
|
|
bits.seek(0)
|
2015-07-17 22:21:10 +03:00
|
|
|
codec.decode(bits, result)
|
2017-01-12 23:58:55 +03:00
|
|
|
assert list(array) == list(result)
|
2015-07-17 22:21:10 +03:00
|
|
|
|
|
|
|
|
2017-01-12 23:58:55 +03:00
|
|
|
def test_serialize_codecs_vocab(en_vocab):
|
|
|
|
words = ["the", "dog", "jumped"]
|
|
|
|
for word in words:
|
|
|
|
_ = en_vocab[word]
|
|
|
|
codec = HuffmanCodec([(lex.orth, lex.prob) for lex in en_vocab])
|
2015-07-17 22:21:10 +03:00
|
|
|
bits = BitArray()
|
2017-01-12 23:58:55 +03:00
|
|
|
ids = [en_vocab[s].orth for s in words]
|
|
|
|
array = numpy.array(ids, dtype=numpy.int32)
|
|
|
|
codec.encode(array, bits)
|
|
|
|
result = numpy.array(range(len(array)), dtype=numpy.int32)
|
2015-07-17 22:31:44 +03:00
|
|
|
bits.seek(0)
|
2015-07-17 22:21:10 +03:00
|
|
|
codec.decode(bits, result)
|
2017-01-12 23:58:55 +03:00
|
|
|
assert list(array) == list(result)
|