Skip to content

Commit 4cefe70

Browse files
committed
avoid extra .collect() on word store creation
1 parent 882fd08 commit 4cefe70

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

nlprule/src/tokenizer/tag.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,17 @@ impl From<Tagger> for TaggerFields {
248248
impl From<TaggerFields> for Tagger {
249249
fn from(data: TaggerFields) -> Self {
250250
let word_store_fst = Map::new(data.word_store_fst).unwrap();
251-
let word_store: BiMap<String, WordIdInt> = word_store_fst
252-
.into_stream()
253-
.into_str_vec()
254-
.unwrap()
255-
.into_iter()
256-
.map(|(key, value)| (key, WordIdInt(value as u32)))
257-
.collect();
251+
let mut word_store: BiMap<String, WordIdInt> = BiMap::with_capacity(word_store_fst.len());
252+
let mut stream = word_store_fst.into_stream();
253+
254+
while let Some((key, value)) = stream.next() {
255+
word_store.insert(
256+
std::str::from_utf8(key)
257+
.expect("word store keys are valid utf-8.")
258+
.to_owned(),
259+
WordIdInt(value as u32),
260+
);
261+
}
258262

259263
let mut tags = DefaultHashMap::new();
260264
let mut groups = DefaultHashMap::new();

0 commit comments

Comments
 (0)