#[allow(dead_code)]
pub fn cosine_similarity(a: &[f32], b: &[f32]) -> Option<f32> {
if a.len() != b.len() || a.is_empty() {
return None;
}
let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();
if norm_a == 0.0 || norm_b == 0.0 {
return None;
}
Some(dot / (norm_a * norm_b))
}
#[allow(dead_code)]
pub fn compute_hubness_scores(
embeddings: &[(String, Vec<f32>)],
top_k: usize,
) -> Vec<HubnessScore> {
let n = embeddings.len();
let mut hits = vec![0usize; n];
if top_k == 0 || n < 2 {
let max_hits = n.saturating_sub(1);
let mut scores: Vec<HubnessScore> = embeddings
.iter()
.map(|(id, _)| HubnessScore {
item_id: id.clone(),
neighbor_hits: 0,
normalized_score: 0.0,
})
.collect();
scores.sort_unstable_by(|a, b| {
b.neighbor_hits
.cmp(&a.neighbor_hits)
.then_with(|| a.item_id.cmp(&b.item_id))
});
let _ = max_hits; return scores;
}
let max_possible_hits = n.saturating_sub(1);
for i in 0..n {
let (_, ref qi) = embeddings[i];
let mut sims: Vec<(f32, &str)> = embeddings
.iter()
.enumerate()
.filter(|(j, _)| *j != i)
.filter_map(|(_, (id, emb))| cosine_similarity(qi, emb).map(|s| (s, id.as_str())))
.collect();
sims.sort_unstable_by(|a, b| {
b.0.partial_cmp(&a.0)
.unwrap_or(std::cmp::Ordering::Equal)
.then_with(|| a.1.cmp(b.1))
});
for (_, neighbour_id) in sims.iter().take(top_k) {
if let Some(j) = embeddings
.iter()
.position(|(id, _)| id.as_str() == *neighbour_id)
{
hits[j] += 1;
}
}
}
let mut scores: Vec<HubnessScore> = embeddings
.iter()
.enumerate()
.map(|(i, (id, _))| {
let h = hits[i];
let norm = if max_possible_hits == 0 {
0.0
} else {
h as f32 / max_possible_hits as f32
};
HubnessScore {
item_id: id.clone(),
neighbor_hits: h,
normalized_score: norm,
}
})
.collect();
scores.sort_unstable_by(|a, b| {
b.neighbor_hits
.cmp(&a.neighbor_hits)
.then_with(|| a.item_id.cmp(&b.item_id))
});
scores
}
#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq)]
pub struct HubnessScore {
pub item_id: String,
pub neighbor_hits: usize,
pub normalized_score: f32,
}