Files
NLP/lab1/tests/test_features.py

39 lines
1.4 KiB
Python

"""Тесты ручных стилометрических признаков."""
from __future__ import annotations
import numpy as np
from stylometry.datasets import load_feature_frame
from stylometry.features import StylometricVectorizer, feature_dictionary
def test_feature_matrix_shape_and_finiteness() -> None:
frame = load_feature_frame(verbose=False).head(20)
vectorizer = StylometricVectorizer()
matrix = vectorizer.fit_transform(frame)
names = vectorizer.get_feature_names_out()
assert matrix.shape == (20, len(names))
assert np.isfinite(matrix).all()
assert len(set(names)) == len(names)
def test_feature_dictionary() -> None:
frame = load_feature_frame(verbose=False).head(2)
vectorizer = StylometricVectorizer()
vectorizer.fit(frame)
dictionary = feature_dictionary(vectorizer.get_feature_names_out())
assert len(dictionary) == len(vectorizer.get_feature_names_out())
assert {"feature", "group", "description", "status"} <= set(dictionary.columns)
def test_group_subset() -> None:
frame = load_feature_frame(verbose=False).head(5)
vectorizer = StylometricVectorizer(groups=["length", "punctuation"])
matrix = vectorizer.fit_transform(frame)
assert matrix.shape[0] == 5
assert all(
name.startswith("len_") or name.startswith("punct_")
for name in vectorizer.get_feature_names_out()
)