Spaces:
Running on Zero
Running on Zero
Download tests/test_embedding_utils.py from ExperienceNow/indobert-embedding-explorer: direct link, hf CLI and curl.
- Browser
- Download file 1.62 kB
-
https://huggingface.co/spaces/ExperienceNow/indobert-embedding-explorer/resolve/main/tests/test_embedding_utils.py
- Command line
-
hf download hf://spaces/ExperienceNow/indobert-embedding-explorer/tests/test_embedding_utils.py
-
curl -L -o test_embedding_utils.py https://huggingface.co/spaces/ExperienceNow/indobert-embedding-explorer/resolve/main/tests/test_embedding_utils.py
1.62 kB
| 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]] | |