airust 0.4.10

A modular Rust knowledge engine: lexical search, text matching, optional semantic retrieval and reranking
Documentation
use std::collections::{BTreeMap, BTreeSet};
use unicode_normalization::{char::is_combining_mark, UnicodeNormalization};

pub fn normalize(text: &str) -> String {
    if text.is_ascii() {
        let mut normalized = String::with_capacity(text.len());
        let mut pending_space = false;
        for byte in text.bytes() {
            if matches!(byte, b' ' | b'\t'..=b'\r') {
                pending_space = !normalized.is_empty();
            } else {
                if pending_space {
                    normalized.push(' ');
                    pending_space = false;
                }
                normalized.push(char::from(byte.to_ascii_lowercase()));
            }
        }
        return normalized;
    }
    let mut normalized = String::with_capacity(text.len());
    let mut pending_space = false;
    for character in normalized_characters(text) {
        if character.is_whitespace() {
            pending_space = !normalized.is_empty();
        } else {
            if pending_space {
                normalized.push(' ');
                pending_space = false;
            }
            normalized.push(character);
        }
    }
    normalized
}

pub(crate) fn normalized_characters(text: &str) -> impl Iterator<Item = char> + '_ {
    text.nfkc().flat_map(char::to_lowercase).nfkc()
}

pub fn tokenize(text: &str) -> Vec<String> {
    let normalized = normalize(text);
    tokens_normalized(&normalized).map(str::to_owned).collect()
}

pub(crate) fn tokens_normalized(normalized: &str) -> impl Iterator<Item = &str> {
    normalized
        .split(|c: char| !c.is_alphanumeric() && !is_combining_mark(c))
        .filter(|token| token.chars().any(char::is_alphanumeric))
}

pub fn term_frequencies(text: &str) -> BTreeMap<String, usize> {
    frequencies_normalized(&normalize(text))
}

pub(crate) fn frequencies_normalized(normalized: &str) -> BTreeMap<String, usize> {
    let mut frequencies = BTreeMap::new();
    let mut reuse_keys = false;
    for token in tokens_normalized(normalized) {
        if reuse_keys {
            if let Some(count) = frequencies.get_mut(token) {
                *count += 1;
                continue;
            }
        }
        match frequencies.entry(token.to_owned()) {
            std::collections::btree_map::Entry::Occupied(mut entry) => {
                *entry.get_mut() += 1;
                reuse_keys = true;
            }
            std::collections::btree_map::Entry::Vacant(entry) => {
                entry.insert(1);
            }
        }
    }
    frequencies
}

pub fn unique_terms(text: &str) -> BTreeSet<String> {
    tokenize(text).into_iter().collect()
}

pub fn is_stopword(term: &str) -> bool {
    matches!(
        term,
        "der"
            | "die"
            | "das"
            | "und"
            | "in"
            | "ist"
            | "von"
            | "mit"
            | "zum"
            | "zur"
            | "zu"
            | "ein"
            | "eine"
            | "eines"
            | "the"
            | "and"
            | "is"
            | "of"
            | "to"
            | "a"
            | "with"
            | "for"
            | "on"
            | "at"
            | "this"
            | "that"
    )
}