Spaces:
Running on Zero
Running on Zero
File size: 1,623 Bytes
bd69aa2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import pytest
from embedding_utils import (
cosine_similarity_matrix,
parse_input_lines,
principal_component_projection,
)
def test_parse_input_lines_trims_blanks_and_preserves_order():
assert parse_input_lines(" Saya suka membaca.\n\nIndonesia itu indah. ") == [
"Saya suka membaca.",
"Indonesia itu indah.",
]
def test_parse_input_lines_rejects_more_than_six_samples():
raw = "\n".join(f"kalimat {index}" for index in range(7))
with pytest.raises(ValueError, match="Maksimal 6"):
parse_input_lines(raw)
def test_cosine_similarity_matrix_returns_pairwise_cosines():
actual = cosine_similarity_matrix([[1.0, 0.0], [0.0, 1.0], [1.0, 1.0]])
assert actual[0] == pytest.approx([1.0, 0.0, 2**-0.5])
assert actual[1] == pytest.approx([0.0, 1.0, 2**-0.5])
assert actual[2] == pytest.approx([2**-0.5, 2**-0.5, 1.0])
def test_cosine_similarity_handles_zero_vectors_as_zero_similarity():
assert cosine_similarity_matrix([[0.0, 0.0], [1.0, 2.0]]) == [
[0.0, 0.0],
[0.0, 1.0],
]
def test_principal_component_projection_returns_two_coordinates_per_sample():
points = principal_component_projection([[1.0, 0.0], [0.0, 1.0]])
assert len(points) == 2
assert all(len(point) == 2 for point in points)
assert points[0][1] == pytest.approx(0.0)
assert points[0][0] == pytest.approx(-points[1][0])
assert abs(points[0][0] - points[1][0]) == pytest.approx(2**0.5)
def test_principal_component_projection_centers_a_single_sample_at_origin():
assert principal_component_projection([[2.0, -3.0]]) == [[0.0, 0.0]]
|