-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmod.rs
474 lines (408 loc) · 14.1 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Copyright 2019 vtext developers
//
// Licensed under the Apache License, Version 2.0,
// <http://apache.org/licenses/LICENSE-2.0>. This file may not be copied,
// modified, or distributed except according to those terms.
/*!
# Vectorization module
This module allows computing a sparse document term matrix from a text corpus.
```rust
extern crate vtext;
use vtext::tokenize::{VTextTokenizer,Tokenizer};
use vtext::vectorize::CountVectorizer;
let documents = vec![
String::from("Some text input"),
String::from("Another line"),
];
let mut vectorizer = CountVectorizer::<VTextTokenizer>::default();
let X = vectorizer.fit_transform(&documents);
// returns a sparse CSR matrix with document-terms counts
*/
use crate::errors::VTextError;
use crate::math::CSRArray;
use crate::tokenize::Tokenizer;
use hashbrown::{HashMap, HashSet};
use itertools::sorted;
use ndarray::Array;
#[cfg(feature = "rayon")]
use rayon::prelude::*;
use sprs::CsMat;
#[cfg(feature = "rayon")]
use std::cmp;
#[cfg(test)]
mod tests;
/// Sort features by name
///
/// Returns a reordered matrix and modifies the vocabulary in place
fn _sort_features(X: &mut CSRArray, vocabulary: &mut HashMap<String, i32>) {
let mut vocabulary_sorted: Vec<_> = vocabulary
.iter()
.map(|(key, val)| (key.clone(), *val))
.collect();
vocabulary_sorted.sort_unstable();
let mut idx_map: Array<usize, _> = Array::zeros(vocabulary_sorted.len());
for (idx_new, (_term, idx_old)) in vocabulary_sorted.iter().enumerate() {
idx_map[*idx_old as usize] = idx_new;
vocabulary
.entry(_term.to_string())
.and_modify(|e| *e = idx_new as i32);
}
for idx in 0..X.indices.len() {
X.indices[idx] = idx_map[X.indices[idx]];
}
}
/// Sum duplicates
#[inline]
fn _sum_duplicates(tf: &mut CSRArray, indices_local: &[i32], nnz: &mut usize) {
if !indices_local.is_empty() {
let mut bucket: i32 = 1;
let mut index_last = indices_local[0];
for index_current in indices_local.iter().skip(1) {
if *index_current != index_last {
tf.indices.push(index_last as usize);
tf.data.push(bucket);
*nnz += 1;
index_last = *index_current;
bucket = 1;
} else {
bucket += 1;
}
}
tf.indices
.push(indices_local[indices_local.len() - 1] as usize);
tf.data.push(bucket);
*nnz += 1;
}
tf.indptr.push(*nnz);
}
#[derive(Debug, Clone)]
pub struct CountVectorizerParams<T> {
lowercase: bool,
tokenizer: T,
n_jobs: usize,
}
impl<T: Tokenizer + Clone> CountVectorizerParams<T> {
pub fn lowercase(&mut self, value: bool) -> CountVectorizerParams<T> {
self.lowercase = value;
self.clone()
}
pub fn tokenizer(&mut self, value: T) -> CountVectorizerParams<T> {
self.tokenizer = value.clone();
self.clone()
}
pub fn n_jobs(&mut self, value: usize) -> CountVectorizerParams<T> {
self.n_jobs = value;
self.clone()
}
pub fn build(&mut self) -> Result<CountVectorizer<T>, VTextError> {
if self.n_jobs < 1 {
panic!("n_jobs={} must be > 0", self.n_jobs);
}
Ok(CountVectorizer {
params: self.clone(),
vocabulary: HashMap::with_capacity_and_hasher(1000, Default::default()),
})
}
}
impl<T: Tokenizer + Clone + Default> Default for CountVectorizerParams<T> {
/// Create a new instance
fn default() -> CountVectorizerParams<T> {
let tokenizer = T::default();
CountVectorizerParams {
lowercase: true,
tokenizer,
n_jobs: 1,
}
}
}
impl<T: Tokenizer + Clone + Default> Default for CountVectorizer<T> {
/// Create a new instance
fn default() -> CountVectorizer<T> {
CountVectorizerParams::default().build().unwrap()
}
}
#[derive(Debug)]
pub struct CountVectorizer<T> {
pub params: CountVectorizerParams<T>,
// vocabulary uses i32 indices, to avoid memory copies when converting
// to sparse CSR arrays in Python with scipy.sparse
pub vocabulary: HashMap<String, i32>,
}
pub enum Vectorizer {}
impl<T: Tokenizer + Sync> CountVectorizer<T> {
/// Initialize a CountVectorizer estimator
pub fn with_params_and_vocabulary(
params: CountVectorizerParams<T>,
vocabulary: HashMap<String, i32>,
) -> Self {
CountVectorizer { params, vocabulary }
}
/// Fit the estimator
///
/// This lists the vocabulary
pub fn fit(&mut self, X: &[String]) {
let tokenize = |X: &[String]| -> HashSet<String> {
let mut _vocab: HashSet<String> = HashSet::with_capacity(1000);
for doc in X {
let doc = doc.to_ascii_lowercase();
let tokens = self.params.tokenizer.tokenize(&doc);
for token in tokens {
if !_vocab.contains(token) {
_vocab.insert(token.to_string());
};
}
}
_vocab
};
let vocabulary: HashSet<String>;
if self.params.n_jobs == 1 {
vocabulary = tokenize(X);
} else if self.params.n_jobs > 1 {
#[cfg(not(feature = "rayon"))]
{
panic!("vtext not built with rayon support; got n_jobs > 1");
}
#[cfg(feature = "rayon")]
{
let chunk_size = cmp::max(X.len() / (self.params.n_jobs * 4), 1);
let pipe = X.par_chunks(chunk_size).flat_map(tokenize);
vocabulary = pipe.collect();
}
} else {
panic!("n_jobs={} must be > 0", self.params.n_jobs);
}
if !vocabulary.is_empty() {
self.vocabulary = sorted(vocabulary.iter())
.zip(0..vocabulary.len())
.map(|(tok, idx)| (tok.to_owned(), idx as i32))
.collect();
}
}
/// Transform
///
/// Converts a sequence of text documents to a CSR Matrix
pub fn transform(&mut self, X: &[String]) -> CsMat<i32> {
let mut tf = crate::math::CSRArray {
indices: Vec::new(),
indptr: Vec::new(),
data: Vec::new(),
};
tf.indptr.push(0);
let mut nnz: usize = 0;
let tokenize_map = |doc: &str| -> Vec<i32> {
// Closure to tokenize a document and returns hash indices for each token
let mut indices_local: Vec<i32> = Vec::with_capacity(10);
for token in self.params.tokenizer.tokenize(doc) {
if let Some(_id) = self.vocabulary.get(token) {
indices_local.push(*_id)
};
}
// this takes 10-15% of the compute time
indices_local.sort_unstable();
indices_local
};
let pipe: Box<dyn Iterator<Item = Vec<i32>>>;
if self.params.n_jobs == 1 {
pipe = Box::new(
X.iter()
.map(|doc| doc.to_ascii_lowercase())
.map(|doc| tokenize_map(&doc)),
);
} else if self.params.n_jobs > 1 {
#[cfg(not(feature = "rayon"))]
{
panic!("vtext not built with rayon support; got n_jobs > 1");
}
#[cfg(feature = "rayon")]
{
pipe = Box::new(
X.par_iter()
.map(|doc| doc.to_ascii_lowercase())
.map(|doc| tokenize_map(&doc))
.collect::<Vec<Vec<i32>>>()
.into_iter(),
);
}
} else {
panic!("n_jobs={} must be > 0", self.params.n_jobs);
}
for indices_local in pipe {
_sum_duplicates(&mut tf, indices_local.as_slice(), &mut nnz);
}
CsMat::new(
(tf.indptr.len() - 1, self.vocabulary.len()),
tf.indptr,
tf.indices,
tf.data,
)
}
/// Fit and transform
///
/// This is a single pass vectorization
pub fn fit_transform(&mut self, X: &[String]) -> CsMat<i32> {
let mut tf = crate::math::CSRArray {
indices: Vec::new(),
indptr: Vec::new(),
data: Vec::new(),
};
tf.indptr.push(0);
let mut nnz: usize = 0;
let mut indices_local: Vec<i32> = Vec::new();
let pipe = X.iter().map(|doc| doc.to_ascii_lowercase());
let mut vocabulary_size: i32 = 0;
for document in pipe {
let tokens = self.params.tokenizer.tokenize(&document);
indices_local.clear();
for token in tokens {
match self.vocabulary.get(token) {
Some(_id) => indices_local.push(*_id),
None => {
self.vocabulary.insert(token.to_string(), vocabulary_size);
indices_local.push(vocabulary_size);
vocabulary_size += 1;
}
};
}
// this takes 10-15% of the compute time
indices_local.sort_unstable();
_sum_duplicates(&mut tf, indices_local.as_slice(), &mut nnz);
}
_sort_features(&mut tf, &mut self.vocabulary);
CsMat::new(
(tf.indptr.len() - 1, self.vocabulary.len()),
tf.indptr,
tf.indices,
tf.data,
)
}
}
#[derive(Debug, Clone)]
pub struct HashingVectorizerParams<T> {
n_features: u64,
lowercase: bool,
tokenizer: T,
n_jobs: usize,
}
impl<T: Tokenizer + Clone> HashingVectorizerParams<T> {
pub fn lowercase(&mut self, value: bool) -> HashingVectorizerParams<T> {
self.lowercase = value;
self.clone()
}
pub fn tokenizer(&mut self, value: T) -> HashingVectorizerParams<T> {
self.tokenizer = value.clone();
self.clone()
}
pub fn n_jobs(&mut self, value: usize) -> HashingVectorizerParams<T> {
self.n_jobs = value;
self.clone()
}
pub fn build(&mut self) -> Result<HashingVectorizer<T>, VTextError> {
if self.n_jobs < 1 {
panic!("n_jobs={} must be > 0", self.n_jobs);
}
Ok(HashingVectorizer {
params: self.clone(),
})
}
}
impl<T: Tokenizer + Clone + Default> Default for HashingVectorizerParams<T> {
/// Create a new instance
fn default() -> HashingVectorizerParams<T> {
let tokenizer = T::default();
HashingVectorizerParams {
n_features: 1_048_576,
lowercase: true,
tokenizer,
n_jobs: 1,
}
}
}
impl<T: Tokenizer + Clone + Default> Default for HashingVectorizer<T> {
/// Create a new instance
fn default() -> HashingVectorizer<T> {
HashingVectorizerParams::default().build().unwrap()
}
}
#[derive(Debug)]
pub struct HashingVectorizer<T> {
params: HashingVectorizerParams<T>,
}
impl<T: Tokenizer + Sync> HashingVectorizer<T> {
/// Fit method
///
/// The vectorizer is stateless, this has no effect
pub fn fit(self, _X: &[String]) -> Self {
self
}
/// Transform method
pub fn transform(&self, X: &[String]) -> CsMat<i32> {
let mut tf = crate::math::CSRArray {
indices: Vec::new(),
indptr: Vec::new(),
data: Vec::new(),
};
tf.indptr.push(0);
let mut nnz: usize = 0;
let tokenize_hash = |doc: &str| -> Vec<i32> {
// Closure to tokenize a document and returns hash indices for each token
let mut indices_local: Vec<i32> = Vec::with_capacity(10);
for token in self.params.tokenizer.tokenize(doc) {
// set the RNG seeds to get reproducible hashing
let hash = seahash::hash_seeded(token.as_bytes(), 1, 1000, 200, 89);
let hash = (hash % self.params.n_features) as i32;
indices_local.push(hash);
}
// this takes 10-15% of the compute time
indices_local.sort_unstable();
indices_local
};
let pipe: Box<dyn Iterator<Item = Vec<i32>>>;
if self.params.n_jobs == 1 {
// Sequential (streaming) pipelines
pipe = Box::new(
X.iter()
// String.to_lowercase() is very slow
// https://www.reddit.com/r/rust/comments/6wbru2/performance_issue_can_i_avoid_of_using_the_slow/
// https://github.com/rust-lang/rust/issues/26244
// Possibly use: https://github.com/JuliaStrings/utf8proc
// http://www.unicode.org/faq/casemap_charprop.html
.map(|doc| doc.to_ascii_lowercase())
.map(|doc| tokenize_hash(&doc)),
);
} else if self.params.n_jobs > 1 {
#[cfg(not(feature = "rayon"))]
{
panic!("vtext not built with rayon support; got n_jobs > 1");
}
#[cfg(feature = "rayon")]
{
// Parallel pipeline. The scaling is reasonably good, however it uses more
// memory as all the tokens need to be collected into a Vec
// TODO: explicitly use self.thread_pool, currently the global thread pool is used
pipe = Box::new(
X.par_iter()
.map(|doc| doc.to_ascii_lowercase())
.map(|doc| tokenize_hash(&doc))
.collect::<Vec<Vec<i32>>>()
.into_iter(),
);
}
} else {
panic!("n_jobs={} must be > 0", self.params.n_jobs);
}
for indices_local in pipe {
_sum_duplicates(&mut tf, indices_local.as_slice(), &mut nnz);
}
CsMat::new(
(tf.indptr.len() - 1, self.params.n_features as usize),
tf.indptr,
tf.indices,
tf.data,
)
}
/// Fit and transform
///
pub fn fit_transform(&self, X: &[String]) -> CsMat<i32> {
self.transform(X)
}
}