Generate simple embedding based on word hashing
(&self, text: &str)
| 123 | |
| 124 | /// Generate simple embedding based on word hashing |
| 125 | fn generate_embedding(&self, text: &str) -> Vec<f32> { |
| 126 | let words: Vec<&str> = text.split_whitespace().collect(); |
| 127 | let mut embedding = vec![0.0; 128]; // 128-dim vector |
| 128 | |
| 129 | for (i, word) in words.iter().enumerate() { |
| 130 | let hash = word.chars().map(|c| c as u32).sum::<u32>(); |
| 131 | let idx = (hash % 128) as usize; |
| 132 | embedding[idx] += 1.0 / (i + 1) as f32; |
| 133 | } |
| 134 | |
| 135 | // Normalize |
| 136 | let magnitude: f32 = embedding.iter().map(|x| x * x).sum::<f32>().sqrt(); |
| 137 | if magnitude > 0.0 { |
| 138 | for v in &mut embedding { |
| 139 | *v /= magnitude; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | embedding |
| 144 | } |
| 145 | |
| 146 | /// Compute cosine similarity between embeddings |
| 147 | fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 { |
no outgoing calls
no test coverage detected