airust 0.2.3

A modular knowledge intelligence engine: lexical search, text matching, and explainable relation suggestions
Documentation
use crate::{knowledge::KnowledgeEntry, text};
use serde::Serialize;
use thiserror::Error;

pub use strsim::levenshtein as levenshtein_distance;

pub(crate) fn bounded_distance(a: &str, b: &str, limit: usize) -> Option<usize> {
    let a: Vec<char> = a.chars().collect();
    let b: Vec<char> = b.chars().collect();
    if a.len().abs_diff(b.len()) > limit {
        return None;
    }
    if a.is_empty() {
        return (b.len() <= limit).then_some(b.len());
    }
    if b.is_empty() {
        return (a.len() <= limit).then_some(a.len());
    }
    let limit = limit.min(a.len().max(b.len()));
    let unreachable = limit + 1;
    let mut previous = vec![unreachable; b.len() + 1];
    let mut current = vec![unreachable; b.len() + 1];
    for (i, cell) in previous
        .iter_mut()
        .enumerate()
        .take(limit.saturating_add(1))
    {
        *cell = i;
    }
    for (row, left) in a.iter().enumerate() {
        let row = row + 1;
        let start = row.saturating_sub(limit).max(1);
        let end = row.saturating_add(limit).min(b.len());
        current[0] = if row <= limit { row } else { unreachable };
        if start > 1 {
            current[start - 1] = unreachable;
        }
        let mut best = current[0];
        for column in start..=end {
            current[column] = (previous[column] + 1)
                .min(current[column - 1] + 1)
                .min(previous[column - 1] + usize::from(*left != b[column - 1]));
            best = best.min(current[column]);
        }
        if end < b.len() {
            current[end + 1] = unreachable;
        }
        if best > limit {
            return None;
        }
        std::mem::swap(&mut previous, &mut current);
    }
    (previous[b.len()] <= limit).then_some(previous[b.len()])
}

pub(crate) fn distance_limit(length: usize, threshold: f64) -> usize {
    let mut limit = ((1.0 - threshold) * length as f64) as usize;
    while limit < length && 1.0 - (limit + 1) as f64 / length as f64 >= threshold {
        limit += 1;
    }
    while limit > 0 && (1.0 - limit as f64 / length as f64) < threshold {
        limit -= 1;
    }
    limit
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
pub struct Similarity {
    pub exact: bool,

    pub character: f64,

    pub token: f64,
}

pub fn compare(a: &str, b: &str) -> Similarity {
    compare_normalized(&text::normalize(a), &text::normalize(b))
}

pub(crate) fn compare_normalized(a: &str, b: &str) -> Similarity {
    if a.is_empty() || b.is_empty() {
        return Similarity {
            exact: false,
            character: 0.0,
            token: 0.0,
        };
    }
    let max_len = a.chars().count().max(b.chars().count());
    let terms_a = text::unique_terms(a);
    let terms_b = text::unique_terms(b);
    let union = terms_a.union(&terms_b).count();
    Similarity {
        exact: a == b,
        character: 1.0 - levenshtein_distance(a, b) as f64 / max_len as f64,
        token: if union == 0 {
            0.0
        } else {
            terms_a.intersection(&terms_b).count() as f64 / union as f64
        },
    }
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
pub enum MatchStrategy {
    Exact,
    #[default]
    Fuzzy,
    TokenOverlap,
}

#[derive(Debug, Clone)]
pub struct MatchOptions {
    pub strategy: MatchStrategy,
    pub min_similarity: f64,
    pub limit: usize,
}

impl Default for MatchOptions {
    fn default() -> Self {
        Self {
            strategy: MatchStrategy::Fuzzy,
            min_similarity: 0.8,
            limit: 10,
        }
    }
}

#[derive(Debug, Error, PartialEq, Eq)]
#[error("min_similarity must be finite and between 0 and 1")]
pub struct MatchError;

impl MatchOptions {
    pub(crate) fn validate(&self) -> Result<(), MatchError> {
        if !self.min_similarity.is_finite() || !(0.0..=1.0).contains(&self.min_similarity) {
            return Err(MatchError);
        }
        Ok(())
    }
}

#[cfg(test)]
mod bounded_tests {
    use super::*;

    #[test]
    fn bounded_distance_agrees_with_full_unicode_distance() {
        let mut words = vec![String::new()];
        let mut frontier = vec![String::new()];
        for _ in 0..4 {
            frontier = frontier
                .iter()
                .flat_map(|word| ['a', 'b', 'รค'].map(|letter| format!("{word}{letter}")))
                .collect();
            words.extend(frontier.iter().cloned());
        }
        for left in &words {
            for right in &words {
                let actual = levenshtein_distance(left, right);
                for limit in 0..=4 {
                    assert_eq!(
                        bounded_distance(left, right, limit),
                        (actual <= limit).then_some(actual),
                        "{left:?} {right:?} {limit}"
                    );
                }
            }
        }
        assert_eq!(bounded_distance("a", "bbb", usize::MAX), Some(3));
    }

    #[test]
    fn distance_threshold_preserves_floating_point_boundaries() {
        for length in 1..=100 {
            for distance in 0..=length {
                let threshold = 1.0 - distance as f64 / length as f64;
                let limit = distance_limit(length, threshold);
                assert!(1.0 - limit as f64 / length as f64 >= threshold);
                assert!(limit == length || 1.0 - (limit + 1) as f64 / (length as f64) < threshold);
            }
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct MatchResult<'a> {
    pub entry: &'a KnowledgeEntry,
    pub score: f64,
    pub similarity: Similarity,
}