Refactor to make test segmenter more accessible

This commit is contained in:
Dirkjan Ochtman 2021-06-02 15:29:10 +02:00
parent f329176ed9
commit 14b00e6417
2 changed files with 11 additions and 5 deletions

View File

@ -2,12 +2,15 @@
use bencher::{benchmark_group, benchmark_main, Bencher};
use instant_segment::test_data::{crate_data_dir, segmenter};
use instant_segment::Search;
benchmark_group!(benches, short);
benchmark_main!(benches);
fn short(bench: &mut Bencher) {
let segmenter = instant_segment::test_data::segmenter();
let mut search = instant_segment::Search::default();
let segmenter = segmenter(crate_data_dir());
let mut search = Search::default();
bench.iter(|| {
let _ = segmenter.segment("thisisatest", &mut search);
});

View File

@ -9,11 +9,10 @@ use super::{HashMap, Segmenter};
#[test]
fn test_data() {
crate::test_cases::run(&segmenter());
crate::test_cases::run(&segmenter(crate_data_dir()));
}
pub fn segmenter() -> Segmenter {
let dir = PathBuf::from(format!("{}/../data", env!("CARGO_MANIFEST_DIR")));
pub fn segmenter(dir: PathBuf) -> Segmenter {
let mut ln = String::new();
let uni_file = dir.join("en-unigrams.txt");
@ -59,3 +58,7 @@ pub fn segmenter() -> Segmenter {
Segmenter::from_maps(unigrams, bigrams)
}
pub fn crate_data_dir() -> PathBuf {
PathBuf::from(format!("{}/../data", env!("CARGO_MANIFEST_DIR")))
}