From 0d2930c408bf567883fd0b288e94800e4b424cb9 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 4 Feb 2021 10:19:52 +0100 Subject: [PATCH] Add API to create segmenter from hashmaps directly --- src/lib.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 43161a3..6295c0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,9 +26,22 @@ impl Segmenter { U: Iterator, B: Iterator, { + Self::from_maps(unigrams.collect(), bigrams.collect()) + } + + /// Create `Segmenter` from the given hashmaps (using ahash) + /// + /// 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. + /// The `HashMap` type here refers to `std::collections::HashMap` parametrized with the + /// `ahash::RandomState`. + pub fn from_maps( + unigrams: HashMap, + bigrams: HashMap<(String, String), f64>, + ) -> Self { Self { - unigrams: unigrams.collect::>(), - bigrams: bigrams.collect::>(), + unigrams, + bigrams, limit: DEFAULT_LIMIT, total: DEFAULT_TOTAL, }