messense / jieba-rs

The Jieba Chinese Word Segmentation Implemented in Rust
MIT License
738 stars 46 forks source link

add_word panic: attempt to subtract with overflow #68

Closed leewis101 closed 4 years ago

leewis101 commented 4 years ago
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

MnO2 commented 4 years ago

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.

messense commented 4 years ago

Fixed in master.