ipfrs_semantic/semantic_query_optimizer/
functions.rs1#[inline]
6pub(super) fn levenshtein(a: &str, b: &str) -> u8 {
7 let (a, b) = (a.as_bytes(), b.as_bytes());
8 let (alen, blen) = (a.len(), b.len());
9 let mut dp = vec![vec![0u16; blen + 1]; alen + 1];
10 for (i, row) in dp.iter_mut().enumerate() {
11 row[0] = i as u16;
12 }
13 for (j, cell) in dp[0].iter_mut().enumerate() {
14 *cell = j as u16;
15 }
16 for i in 1..=alen {
17 for j in 1..=blen {
18 dp[i][j] = if a[i - 1] == b[j - 1] {
19 dp[i - 1][j - 1]
20 } else {
21 1 + dp[i - 1][j].min(dp[i][j - 1]).min(dp[i - 1][j - 1])
22 };
23 }
24 }
25 dp[alen][blen].min(255) as u8
26}