Shorten workspace directory names to app, bot and core

This commit is contained in:
2026-12-03 04:26:31 +08:00
parent 3ae1097e1b
commit ef251ae339
28 changed files with 4 additions and 4 deletions
+77
View File
@@ -0,0 +1,77 @@
use jieba_rs::Jieba;
use std::{collections::BTreeSet, sync::OnceLock};
use unicode_normalization::UnicodeNormalization;
pub(crate) fn normalize(text: &str) -> String {
text.nfkc()
.flat_map(char::to_lowercase)
.collect::<String>()
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
}
fn han(c: char) -> bool {
matches!(c as u32, 0x3400..=0x4dbf | 0x4e00..=0x9fff | 0xf900..=0xfaff | 0x20000..=0x323af)
}
// Keep individual Han characters in the index for one-character queries;
// for longer Han runs query only the more selective overlapping bigrams.
fn tokens(text: &str, indexing: bool) -> Vec<String> {
let mut output = Vec::new();
let mut chars = text.chars().peekable();
while let Some(c) = chars.next() {
if han(c) {
let mut previous = c;
let mut single = true;
if indexing {
output.push(c.to_string());
}
while let Some(next) = chars.next_if(|&n| han(n)) {
output.push(format!("{previous}{next}"));
if indexing {
output.push(next.to_string());
}
single = false;
previous = next;
}
if single && !indexing {
output.push(c.to_string());
}
} else if c.is_alphanumeric() || c == '_' {
let mut word = String::from(c);
while let Some(next) = chars.next_if(|&n| !han(n) && (n.is_alphanumeric() || n == '_'))
{
word.push(next);
}
output.push(word);
}
}
output
}
pub(crate) fn index_tokens(text: &str) -> Vec<String> {
tokens(text, true)
}
pub(crate) fn query_terms(text: &str) -> BTreeSet<String> {
static LEXICON: OnceLock<Jieba> = OnceLock::new();
let mut terms = BTreeSet::new();
let mut rest = text;
while let Some(first) = rest.chars().next() {
let is_han = han(first);
let end = rest.find(|c: char| han(c) != is_han).unwrap_or(rest.len());
let (run, tail) = rest.split_at(end);
if is_han {
// Segment only the query. The existing character/bigram postings can
// match each dictionary word even in names absent from the dictionary.
for word in LEXICON.get_or_init(Jieba::new).cut(run, false) {
terms.extend(tokens(word, false));
}
} else {
terms.extend(tokens(run, false));
}
rest = tail;
}
terms
}