Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions rust/rssparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ pub struct SparseMatrixBuilder {
num_cols: usize,
}

impl SparseMatrixBuilder {
fn _fit(&mut self, texts: &Vec<HashMap<String, usize>>) {
let mut col_index: usize = 0;
for doc in texts {
for token in doc.keys() {
if !self.vocab.contains_key(token) {
self.vocab.insert(token.clone(), col_index);
col_index += 1;
}
}
}
self.num_cols = col_index;
}
}

#[pymethods]
impl SparseMatrixBuilder {
#[new]
Expand Down Expand Up @@ -59,7 +74,7 @@ impl SparseMatrixBuilder {
self.normalize,
);

self._fit(texts.clone());
self._fit(&texts);

// Scipy csr_matrix are faster to build from numpy arrays.
let (vec1, vec2, vec3) = self._transform(texts);
Expand All @@ -80,20 +95,7 @@ impl SparseMatrixBuilder {
self.normalize,
);

self._fit(texts);
}

fn _fit(&mut self, texts: Vec<HashMap<String, usize>>) {
let mut col_index: usize = 0;
for doc in &texts {
for token in doc.keys() {
if !self.vocab.contains_key(token) {
self.vocab.insert(token.clone(), col_index);
col_index += 1;
}
}
}
self.num_cols = col_index;
self._fit(&texts);
}

pub fn transform(
Expand Down