<commit_before><commit_msg>Add serialization tests for tensorizer<commit_after># coding: utf-8
from __future__ import unicode_literals

from ..util import make_tempdir
from ...pipeline import TokenVectorEncoder as Tensorizer

import pytest


def test_serialize_tensorizer_roundtrip_bytes(en_vocab):
    tensorizer = Tensorizer(en_vocab)
    tensorizer.model = tensorizer.Model()
    tensorizer_b = tensorizer.to_bytes()
    new_tensorizer = Tensorizer(en_vocab).from_bytes(tensorizer_b)
    assert new_tensorizer.to_bytes() == tensorizer_b


def test_serialize_tensorizer_roundtrip_disk(en_vocab):
    tensorizer = Tensorizer(en_vocab)
    tensorizer.model = tensorizer.Model()
    with make_tempdir() as d:
        file_path = d / 'tensorizer'
        tensorizer.to_disk(file_path)
        tensorizer_d = Tensorizer(en_vocab).from_disk(file_path)
        assert tensorizer.to_bytes() == tensorizer_d.to_bytes()
{'fraction_non_alphanumeric': 0.08232711306256861, 'fraction_numerical': 0.0010976948408342481, 'mean_word_length': 4.922077922077922, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2268710', 'n_tokens_mistral': 299, 'n_tokens_neox': 295, 'n_words': 55}