Rust · Set Similarity

Set Similarity

use numkong::BinarySimilarity;use numkong::Hamming;use numkong::Jaccard; 8.0.0-rc.1 · b1f866ab

Packed-binary metrics work on packed words instead of boolean slices. That is the right model once the workload is “semantic hash” rather than “array of booleans”.

use numkong::{Dot, Hamming, Jaccard, u1x8};

let a = [u1x8(0b10101010), u1x8(0b11110000)];
let b = [u1x8(0b10101110), u1x8(0b11000000)];
let hamming = u1x8::hamming(&a, &b).unwrap();
let jaccard = u1x8::jaccard(&a, &b).unwrap();
let overlap = u1x8::dot(&a, &b).unwrap(); // set bits in common

Dot on u1x8 is the intersection count Jaccard normalises, so a ranking pass can skip the division.

Integer set Jaccard works on sorted arrays of integer identifiers.

let set_a = [1_u32, 3, 5, 7, 9];
let set_b = [3_u32, 5, 8, 9, 10];
let jaccard_sets = u32::jaccard(&set_a, &set_b).unwrap();
assert!(jaccard_sets > 0.0 && jaccard_sets < 1.0); // |A ∩ B| / |A ∪ B|

API

TraitBinarySimilarityBinarySimilarity bundles binary distance metrics: Hamming and Jaccard.
set.rs:206 ↗
pub trait BinarySimilarity: Hamming + Jaccard

Implementors

TraitHammingComputes the Hamming distance between two binary vectors.2
set.rs:75 ↗
pub trait Hamming: StorageElement

Counts differing bits for u1x8, or differing bytes for u8.

Range: [0, n]. Fails with Error::ShapeMismatch if lengths differ, or Error::KernelFailed carrying the kernel's status.

Implemented for: u1x8, u8.

Implementors

OutputResult type of the Hamming distance.
set.rs:77 ↗
type Output;
hammingComputes the Hamming distance between two equally long slices.
set.rs:79 ↗
fn hamming(a: &[Self], b: &[Self]) -> Result<Self::Output, Error>
TraitJaccardComputes the Jaccard distance between two sets represented as bit/integer vectors.2
set.rs:135 ↗
pub trait Jaccard: StorageElement

d = 1 − |A ∩ B| / |A ∪ B|

Range: [0, 1]. Fails with Error::ShapeMismatch if lengths differ, or Error::KernelFailed carrying the kernel's status.

Implemented for: u1x8, u16, u32.

Implementors

OutputResult type of the Jaccard distance.
set.rs:137 ↗
type Output;
jaccardComputes the Jaccard distance between two equally long slices.
set.rs:139 ↗
fn jaccard(a: &[Self], b: &[Self]) -> Result<Self::Output, Error>