quesurifn / yake-rust

MIT License
3 stars 3 forks source link

Fails on non-English texts #3

Open xamgore opened 1 year ago

xamgore commented 1 year ago

Hi! Thanks for such a great crate. There is an issue, though. If we try a text in a non-English language, like the following one:

Здравствуйте уважаемые подписчики и зрители канала гулагунец вам я Владимир осечкин Сегодня у нас 2 мая 2023 года сегодня будет очень важный знаковый эфир будем раскрывать очень важную информацию во многом это впервые будет озвучено на нашем канале Да и в целом в паблике пожалуйста напишите как звук Как слышно Как видно и тогда Через минуту начнем всех еще раз приветствую Спасибо и вижу Владислав модератор чата подключился тоже к просмотру Спасибо ему за помощь Спасибо сандрия и всей команде поддержки Кто донатит кто является спонсором нашего канала потому что именно благодаря вашей поддержке мы сохраняем нашу Независимость и что не менее важно эффективность Так отлично хорошо вижу Спасибо большое дорогие друзья если видно слышно хорошо мы долго думали по поводу того пускать ли в прямой эфир тех непосредственно людей которые сейчас дают очень важные свидетельские показания кто-то из них уже покинул территорию России кто-то находится в России и дает показания в том числе следователям

We'll quickly get an error:

index out of bounds: the len is 12 but the index is 12
  thread 'tests::it_works' panicked at 'index out of bounds: the len is 12 but the index is 12',
  /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/natural-0.3.0/src/distance.rs:90:16

If we look at the code:

// ported from http://hetland.org/coding/python/levenshtein.py
pub fn levenshtein_distance(str1: &str, str2: &str) -> usize {
    let n = str1.len();
    let m = str2.len();

    let mut column: Vec<usize> = (0..n + 1).collect();
    // TODO this probaly needs to use graphemes
    let a_vec: Vec<char> = str1.chars().collect();
    let b_vec: Vec<char> = str2.chars().collect();
    for i in 1..m + 1 {
        let previous = column;
        column = vec![0; n + 1];
        column[0] = i;
        for j in 1..n + 1 {
            let add = previous[j] + 1;
            let delete = column[j - 1] + 1;
            let mut change = previous[j - 1];
            if a_vec[j - 1] != b_vec[i - 1] {  // <---------- here
                change = change + 1
            }
            column[j] = min3(add, delete, change);
        }
    }
    column[n]
}

I'm not sure, whether the algorithm is correct, but here is another implementation.

quesurifn commented 7 months ago

Hey, sorry for getting back to you almost a year later. I'll try to make this a priority in the coming days. There's no excuse for out of bounds. I haven't dealt with non-english chars, but I'm wondering if they exist in a different encoding and hence out of bounds are taking place. I'll take a look.