messense / jieba-rs

The Jieba Chinese Word Segmentation Implemented in Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

add_word panic: attempt to subtract with overflow

leewis101 opened this issue · comments

use jieba_rs::Jieba;

fn main() {
    let mut jieba = Jieba::new();
    jieba.add_word("测试", Some(10), None);
}

thread 'main' panicked at 'attempt to subtract with overflow', ~/.cargo/registry/src/github.com-1ecc6299db9ec823/jieba-rs-0.4.10/src/lib.rs:254:31

Thanks for reporting.

The freq in the default dictionary is

测试 2444 vn

threrefore it would result overflow because freq is 10 and old_freq is 2444

self.total += freq - old_freq;

I would create a PR to break it into two ops (subtraction then addition) to avoid overflow.

Fixed in master.