#[macro_use(bitset)] extern crate compacts; use compacts::bits::{PopCount, Set}; struct Jaccard; struct Cosine; impl Jaccard { fn similarity(x: &Set, y: &Set) -> f64 { let p = x.count1(); let q = y.count1(); let both = x.and(y).collect::().count1(); both as f64 / (p + q - both) as f64 } } impl Cosine { fn similarity(x: &Set, y: &Set) -> f64 { let p = x.count1(); let q = y.count1(); let both = x.and(y).collect::().count1(); both as f64 / ((p * q) as f64).sqrt() } } #[test] fn jaccard_similarity() { let b1 = bitset!(0, 2, 3, 4); let b2 = bitset!(0, 1, 3, 4, 6); eprintln!("jaccard {}", Jaccard::similarity(&b1, &b2)); } #[test] fn cosine_similarity() { let b1 = bitset!(0, 2, 3, 4); let b2 = bitset!(0, 1, 3, 4, 6); eprintln!("cosine {}", Cosine::similarity(&b1, &b2)); }