diff --git a/src/lib.rs b/src/lib.rs index 71a6b31..a934b8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,3 @@ -use std::error::Error; use std::io; use std::num::ParseIntError; use std::ops::{Index, Range}; @@ -23,17 +22,17 @@ impl Segmenter { /// /// Note: the `String` types used in this API are defined in the `smartstring` crate. Any /// `&str` or `String` can be converted into the `String` used here by calling `into()` on it. - pub fn from_iters(unigrams: U, bigrams: B) -> Result> + pub fn from_iters(unigrams: U, bigrams: B) -> Self where - U: Iterator>>, - B: Iterator>>, + U: Iterator, + B: Iterator, { - Ok(Self { - unigrams: unigrams.collect::, _>>()?, - bigrams: bigrams.collect::, _>>()?, + Self { + unigrams: unigrams.collect::>(), + bigrams: bigrams.collect::>(), limit: DEFAULT_LIMIT, total: DEFAULT_TOTAL, - }) + } } /// Appends list of words that is the best segmentation of `text` to `out` diff --git a/src/test_data.rs b/src/test_data.rs index 39f37c3..1d9119d 100644 --- a/src/test_data.rs +++ b/src/test_data.rs @@ -13,37 +13,36 @@ pub fn segmenter() -> Segmenter { let uni_file = dir.join("unigrams.txt"); let reader = BufReader::new(File::open(&uni_file).unwrap()); let unigrams = reader.lines().enumerate().map(move |(i, ln)| { - let ln = ln?; + let ln = ln.expect(&format!("line error at {:?}:{}", uni_file, i)); let split = ln .find('\t') - .ok_or_else(|| format!("no tab found in {:?}:{}", uni_file, i))?; + .expect(&format!("no tab found in {:?}:{}", uni_file, i)); let word = ln[..split].into(); - let p = usize::from_str(&ln[split + 1..]) - .map_err(|e| format!("error at {:?}:{}: {}", uni_file, i, e))?; - Ok((word, p as f64)) + let p = usize::from_str(&ln[split + 1..]).expect(&format!("error at {:?}:{}", uni_file, i)); + (word, p as f64) }); let bi_file = dir.join("bigrams.txt"); let reader = BufReader::new(File::open(&bi_file).unwrap()); let bigrams = reader.lines().enumerate().map(move |(i, ln)| { - let ln = ln?; + let ln = ln.expect(&format!("line error at {:?}:{}", bi_file, i)); let word_split = ln .find(' ') - .ok_or_else(|| format!("no space found in {:?}:{}", bi_file, i))?; + .expect(&format!("no space found in {:?}:{}", bi_file, i)); let score_split = ln[word_split + 1..] .find('\t') - .ok_or_else(|| format!("no tab found in {:?}:{}", bi_file, i))? + .expect(&format!("no tab found in {:?}:{}", bi_file, i)) + word_split + 1; let word1 = ln[..word_split].into(); let word2 = ln[word_split + 1..score_split].into(); let p = usize::from_str(&ln[score_split + 1..]) - .map_err(|e| format!("error at {:?}:{}: {}", bi_file, i, e))?; + .expect(&format!("error at {:?}:{}", bi_file, i)); - Ok(((word1, word2), p as f64)) + ((word1, word2), p as f64) }); - Segmenter::from_iters(unigrams, bigrams).unwrap() + Segmenter::from_iters(unigrams, bigrams) }