Spaces:
Running on Zero
Running on Zero
File size: 2,413 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 49 50 51 52 53 54 55 56 57 58 59 | """Small, dependency-free helpers for the IndoBERT embedding demo."""
from __future__ import annotations
from math import sqrt
import numpy as np
def parse_input_lines(raw_text: str, max_samples: int = 6, max_chars: int = 280) -> list[str]:
"""Parse newline-separated samples and enforce the demo's input limits."""
texts = [line.strip() for line in raw_text.splitlines() if line.strip()]
if not texts:
raise ValueError("Masukkan setidaknya satu kalimat Bahasa Indonesia.")
if len(texts) > max_samples:
raise ValueError(f"Maksimal {max_samples} kalimat dalam satu kali analisis.")
if any(len(text) > max_chars for text in texts):
raise ValueError(f"Setiap kalimat dibatasi hingga {max_chars} karakter.")
return texts
def cosine_similarity_matrix(vectors: list[list[float]]) -> list[list[float]]:
"""Return a symmetric cosine-similarity matrix; zero vectors score 0."""
if not vectors:
return []
dimensions = len(vectors[0])
if any(len(vector) != dimensions for vector in vectors):
raise ValueError("Semua embedding harus memiliki dimensi yang sama.")
norms = [sqrt(sum(value * value for value in vector)) for vector in vectors]
matrix: list[list[float]] = []
for i, left in enumerate(vectors):
row = []
for j, right in enumerate(vectors):
denominator = norms[i] * norms[j]
similarity = sum(a * b for a, b in zip(left, right)) / denominator if denominator else 0.0
row.append(round(similarity, 6))
matrix.append(row)
return matrix
def principal_component_projection(vectors: list[list[float]]) -> list[list[float]]:
"""Project rows to two centered PCA coordinates for a small-sample chart."""
if not vectors:
return []
values = np.asarray(vectors, dtype=np.float64)
if values.ndim != 2:
raise ValueError("Embedding harus berupa matriks dua dimensi.")
centered = values - values.mean(axis=0, keepdims=True)
if values.shape[0] == 1:
return [[0.0, 0.0]]
left, singular_values, _ = np.linalg.svd(centered, full_matrices=False)
component_count = min(2, len(singular_values))
coordinates = left[:, :component_count] * singular_values[:component_count]
if component_count == 1:
coordinates = np.column_stack([coordinates, np.zeros(values.shape[0])])
return coordinates.tolist()
|