use basemyai_core::{Embedder, Result};
use crate::EMBEDDING_DIM;
pub struct HashEmbedder;
impl HashEmbedder {
#[must_use]
pub fn new() -> Self {
Self
}
fn vec_for(text: &str) -> Vec<f32> {
let mut v = vec![0.0_f32; EMBEDDING_DIM];
for (i, b) in text.bytes().enumerate() {
v[i % EMBEDDING_DIM] += f32::from(b) + 1.0;
}
v[0] += 1.0;
v
}
}
impl Default for HashEmbedder {
fn default() -> Self {
Self::new()
}
}
impl Embedder for HashEmbedder {
fn embed(&self, text: &str) -> Result<Vec<f32>> {
Ok(Self::vec_for(text))
}
fn embed_batch(&self, texts: &[String]) -> Result<Vec<Vec<f32>>> {
Ok(texts.iter().map(|t| Self::vec_for(t)).collect())
}
fn model_id(&self) -> &str {
"hash-deterministic-testutil"
}
fn dim(&self) -> usize {
EMBEDDING_DIM
}
}