is-it-slop-preprocessing 0.5.0

Preprocessing step for is-it-slop package, providing fast TF-IDF vectorization.
Documentation
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! Python bindings for the preprocessing pipeline.
//!
//! This module exposes Rust preprocessing components to Python via `PyO3`.
//! All Rust types are prefixed with `Rust*` (e.g., `RustTfidfVectorizer`), and
//! Python wrappers remove the prefix (e.g., `TfidfVectorizer` in Python).
//!
//! # Exposed Components
//!
//! - **`RustTfidfVectorizer`**: TF-IDF vectorization with token n-grams
//! - **`RustTokenChunker`**: Token-based text chunking
//! - **`RustTextCleaner`**: Two-stage text cleaning (universal + dataset artifacts)
//! - **`RustVectorizerParams`**: Configuration for vectorizers
//!
//! # Python Usage
//!
//! ```python
//! from is_it_slop_preprocessing import TfidfVectorizer, VectorizerParams
//!
//! # Create vectorizer
//! params = VectorizerParams(min_df=10, max_df=0.9, sublinear_tf=True)
//! vectorizer, X = TfidfVectorizer.fit_transform(texts, params)
//!
//! # Save to file
//! vectorizer.save("vectorizer.rkyv")
//! ```
//!
//! # CSR Matrix Format
//!
//! Sparse matrices are returned as tuples `(data, indices, indptr, shape)` compatible
//! with `scipy.sparse.csr_matrix`:
//! ```python
//! from scipy.sparse import csr_matrix
//! data, indices, indptr, shape = vectorizer.transform(texts)
//! X = csr_matrix((data, indices, indptr), shape=shape)
//! ```

#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

use std::{fs, path::Path};

use ahash::HashMap;
use numpy::ToPyArray;
use pyo3::{prelude::*, types::PyTuple};

use crate::pre_processor::{
    DEFAULT_MAX_NGRAM, DEFAULT_MIN_NGRAM, TextCleaner, TfidfVectorizer, TokenChunker,
    VectorizerParams, reverse_tokenize as reverse_tokenize_, text_cleaner_for_inference,
    text_cleaner_for_training, tokenize as tokenize_,
};

#[allow(clippy::unsafe_derive_deserialize)]
/// Python wrapper for [`VectorizerParams`].
///
/// Configuration parameters for TF-IDF vectorization exposed to Python.
#[cfg_attr(
    feature = "rkyv",
    derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy)]
#[pyclass(from_py_object)]
struct RustVectorizerParams {
    #[pyo3(get)]
    ngram_range: (usize, usize),
    #[pyo3(get)]
    min_df: f32,
    #[pyo3(get)]
    max_df: f32,
    #[pyo3(get)]
    sublinear_tf: bool,
}

#[pymethods]
impl RustVectorizerParams {
    /// Creates a new `RustVectorizerParams` instance.
    #[new]
    #[pyo3(signature = (min_df, max_df, sublinear_tf))]
    fn new(min_df: f32, max_df: f32, sublinear_tf: bool) -> Self {
        Self {
            ngram_range: (DEFAULT_MIN_NGRAM, DEFAULT_MAX_NGRAM),
            min_df,
            max_df,
            sublinear_tf,
        }
    }

    /// Returns a string representation of the `RustVectorizerParams`.
    fn __repr__(&self) -> String {
        format!(
            "RustVectorizerParams(ngram_range=({}, {}), min_df={}, max_df={}, sublinear_tf={})",
            self.ngram_range.0, self.ngram_range.1, self.min_df, self.max_df, self.sublinear_tf
        )
    }

    /// Returns a detailed string representation of the `RustVectorizerParams`.
    fn __str__(&self) -> String {
        format!("{self:#?}")
    }
}

impl Default for RustVectorizerParams {
    fn default() -> Self {
        Self {
            ngram_range: (3, 5),
            min_df: 10.0,
            max_df: 0.9,
            sublinear_tf: true,
        }
    }
}

impl RustVectorizerParams {
    fn to_inner(self) -> VectorizerParams {
        VectorizerParams::new(
            // self.ngram_range.0..=self.ngram_range.1,
            self.min_df,
            self.max_df,
            self.sublinear_tf,
        )
    }
}

impl From<&VectorizerParams> for RustVectorizerParams {
    fn from(params: &VectorizerParams) -> Self {
        Self {
            ngram_range: params.ngram_range(),
            min_df: params.min_df(),
            max_df: params.max_df(),
            sublinear_tf: params.sublinear_tf(),
        }
    }
}
#[allow(clippy::unsafe_derive_deserialize)]
/// A wrapper function around `TfidfVectorizer` to expose it to Python.
#[cfg_attr(
    feature = "rkyv",
    derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
#[pyclass(from_py_object)]
struct RustTfidfVectorizer {
    #[serde(flatten)]
    inner: TfidfVectorizer,
}

#[pymethods]
impl RustTfidfVectorizer {
    /// Fits the `TfidfVectorizer` to the provided texts with the given parameters.
    /// Returns a new instance of `RustTfidfVectorizer`.
    #[new]
    pub fn fit(py: Python<'_>, texts: Vec<String>, params: RustVectorizerParams) -> Self {
        py.detach(move || {
            let vectorizer = TfidfVectorizer::fit(texts.as_slice(), params.to_inner());
            Self { inner: vectorizer }
        })
    }

    /// Transforms the input texts and returns the TF-IDF matrix components.
    /// The returned tuple contains:
    /// - shape: (usize, usize) | (number of rows, number of columns)
    /// - data: np.ndarray of f32 | values of the non-zero entries
    /// - indices: np.ndarray of usize | column indices of the non-zero entries
    /// - indptr: np.ndarray of usize | index pointers to the start of each row
    #[allow(clippy::needless_pass_by_value)]
    pub fn transform<'py>(
        &self,
        py: Python<'py>,
        texts: Vec<String>,
    ) -> PyResult<Bound<'py, PyTuple>> {
        let tfidf_matrix: sprs::CsMatBase<f32, usize, Vec<usize>, Vec<usize>, Vec<f32>> =
            py.detach(|| self.inner.transform(texts.as_slice()));
        let data = tfidf_matrix.data().to_pyarray(py);
        let indices = tfidf_matrix.indices().to_pyarray(py);
        let indptr = tfidf_matrix
            .indptr()
            .to_owned()
            .into_raw_storage()
            .to_pyarray(py);
        let shape = (tfidf_matrix.rows(), tfidf_matrix.cols());

        (shape, data, indices, indptr).into_pyobject(py)
    }

    /// Fits the vectorizer and transforms the input texts in one step.
    /// Returns a tuple of (vectorizer, `tfidf_matrix_components`).
    /// The `tfidf_matrix_components` is the same as returned by `transform`.
    #[allow(clippy::needless_pass_by_value)]
    #[staticmethod]
    pub fn fit_transform(
        py: Python<'_>,
        texts: Vec<String>,
        params: RustVectorizerParams,
    ) -> PyResult<(Self, Bound<'_, PyTuple>)> {
        let (vectorizer, transform_result) =
            TfidfVectorizer::fit_transform(texts.as_slice(), params.to_inner());
        let vectorizer = Self { inner: vectorizer };
        let data = transform_result.data().to_pyarray(py);
        let indices = transform_result.indices().to_pyarray(py);
        let indptr = transform_result
            .indptr()
            .to_owned()
            .into_raw_storage()
            .to_pyarray(py);
        let shape = (transform_result.rows(), transform_result.cols());
        let transform_result = (shape, data, indices, indptr).into_pyobject(py)?;
        Ok((vectorizer, transform_result))
    }

    /// Transform pre-tokenized sequences to TF-IDF matrix
    #[allow(clippy::needless_pass_by_value)]
    pub fn vectorize_from_tokens<'py>(
        &self,
        py: Python<'py>,
        token_sequences: Vec<Vec<u32>>,
    ) -> PyResult<Bound<'py, PyTuple>> {
        let tfidf_matrix = py.detach(|| self.inner.vectorize_from_tokens(&token_sequences));

        let data = tfidf_matrix.data().to_pyarray(py);
        let indices = tfidf_matrix.indices().to_pyarray(py);
        let indptr = tfidf_matrix
            .indptr()
            .to_owned()
            .into_raw_storage()
            .to_pyarray(py);
        let shape = (tfidf_matrix.rows(), tfidf_matrix.cols());

        (shape, data, indices, indptr).into_pyobject(py)
    }

    /// Getter for the number of features (vocabulary size).
    #[getter]
    pub fn num_features(&self) -> usize {
        self.inner.num_features()
    }

    /// Getter for the vocabulary mapping (token to index).
    #[getter]
    pub fn vocabulary(&self) -> HashMap<String, usize> {
        self.inner.vocabulary()
    }

    /// Getter for the vectorizer parameters.
    #[getter]
    pub fn params(&self) -> RustVectorizerParams {
        self.inner.params().into()
    }

    /// Return a string representation of the `RustTfidfVectorizer`.
    fn __repr__(&self) -> String {
        format!(
            "RustTfidfVectorizer(vocabulary_size={})",
            self.num_features(),
        )
    }

    /// Return a detailed string representation of the `RustTfidfVectorizer`.
    fn __str__(&self) -> String {
        format!("{self:#?}")
    }

    /// Serialize the vectorizer to bytes using rkyv format.
    /// Returns a bytes object that can be saved to disk or passed to `from_bytes`.
    /// Return the inner vectorizer serialized as bytes so it is compatible with Rust side.
    #[cfg(feature = "rkyv")]
    fn to_bytes(&self, py: Python<'_>) -> PyResult<Vec<u8>> {
        py.detach(|| {
            self.inner
                .to_bytes()
                // Converting the archived bytes to a Vec<u8> for Python compatibility, it allocates
                // and you should use `save` method for large models to avoid this allocation.
                .map(rkyv::util::AlignedVec::into_vec)
                .map_err(|e| {
                    PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                        "Failed to serialize vectorizer: {e}"
                    ))
                })
        })
    }

    /// Serialize the vectorizer to JSON string.
    /// Returns a JSON string that can be saved to disk or passed to `from_json`.
    /// Return the inner vectorizer serialized as JSON so it is compatible with Rust side.
    #[cfg(feature = "serde")]
    fn to_json(&self, py: Python<'_>) -> PyResult<String> {
        py.detach(|| {
            self.inner.to_json().map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                    "Failed to serialize vectorizer to JSON: {e}"
                ))
            })
        })
    }

    /// Save the vectorizer to a path based on the file extension.
    /// Supports .rkyv (rkyv) and .json (serde).
    #[cfg(any(feature = "rkyv", feature = "serde"))]
    fn save(&self, py: Python<'_>, path: &str) -> PyResult<()> {
        let path = Path::new(path);
        let extension = path.extension().and_then(|ext| ext.to_str());

        py.detach(|| match extension {
            Some("rkyv") => {
                #[cfg(feature = "rkyv")]
                {
                    let bytes = self.inner.to_bytes().map_err(|e| {
                        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                            "Failed to serialize vectorizer: {e}"
                        ))
                    })?;
                    fs::write(path, bytes.as_slice()).map_err(|e| {
                        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                            "Failed to write vectorizer to {}: {e}",
                            path.display()
                        ))
                    })
                }
                #[cfg(not(feature = "rkyv"))]
                {
                    let _ = path;
                    Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
                        "rkyv serialization is not enabled",
                    ))
                }
            }
            Some("json") => {
                #[cfg(feature = "serde")]
                {
                    let json = self.inner.to_json().map_err(|e| {
                        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                            "Failed to serialize vectorizer to JSON: {e}"
                        ))
                    })?;
                    fs::write(path, json).map_err(|e| {
                        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                            "Failed to write vectorizer to {}: {e}",
                            path.display()
                        ))
                    })
                }
                #[cfg(not(feature = "serde"))]
                {
                    let _ = path;
                    Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
                        "JSON serialization is not enabled",
                    ))
                }
            }
            _ => Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
                "File extension must be .rkyv or .json",
            )),
        })
    }

    /// Deserialize the vectorizer from bytes (rkyv format).
    ///
    /// Args:
    ///     bytes: A bytes object containing the serialized vectorizer
    ///
    /// Returns:
    ///     A new `RustTfidfVectorizer` instance
    #[staticmethod]
    #[cfg(feature = "rkyv")]
    fn from_bytes(py: Python<'_>, bytes: &[u8]) -> PyResult<Self> {
        py.detach(|| {
            let inner = TfidfVectorizer::from_bytes(bytes).map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                    "Failed to deserialize vectorizer from bytes: {e}"
                ))
            })?;
            Ok(Self { inner })
        })
    }

    /// Load a vectorizer from a path based on the file extension.
    /// Supports .rkyv (rkyv) and .json (serde).
    #[staticmethod]
    #[cfg(any(feature = "rkyv", feature = "bincode", feature = "serde"))]
    fn load(py: Python<'_>, path: &str) -> PyResult<Self> {
        let path = Path::new(path);
        let extension = path.extension().and_then(|ext| ext.to_str());

        py.detach(|| match extension {
            Some("rkyv") => {
                #[cfg(feature = "rkyv")]
                {
                    let bytes = fs::read(path).map_err(|e| {
                        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                            "Failed to read vectorizer from {}: {e}",
                            path.display()
                        ))
                    })?;
                    let inner = TfidfVectorizer::from_bytes(&bytes).map_err(|e| {
                        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                            "Failed to deserialize vectorizer from bytes: {e}"
                        ))
                    })?;
                    Ok(Self { inner })
                }
                #[cfg(not(feature = "rkyv"))]
                {
                    let _ = path;
                    Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
                        "rkyv serialization is not enabled",
                    ))
                }
            }
            Some("bin") => {
                #[cfg(feature = "bincode")]
                {
                    let bytes = fs::read(path).map_err(|e| {
                        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                            "Failed to read vectorizer from {}: {e}",
                            path.display()
                        ))
                    })?;
                    let inner = TfidfVectorizer::from_bincode_bytes(&bytes).map_err(|e| {
                        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                            "Failed to deserialize vectorizer from bincode bytes: {e}"
                        ))
                    })?;
                    Ok(Self { inner })
                }
                #[cfg(not(feature = "bincode"))]
                {
                    let _ = path;
                    Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
                        "bincode serialization is not enabled",
                    ))
                }
            }
            Some("json") => {
                #[cfg(feature = "serde")]
                {
                    let json = fs::read_to_string(path).map_err(|e| {
                        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                            "Failed to read vectorizer from {}: {e}",
                            path.display()
                        ))
                    })?;
                    let inner = TfidfVectorizer::from_json(&json).map_err(|e| {
                        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                            "Failed to deserialize vectorizer from JSON: {e}"
                        ))
                    })?;
                    Ok(Self { inner })
                }
                #[cfg(not(feature = "serde"))]
                {
                    let _ = path;
                    Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
                        "JSON serialization is not enabled",
                    ))
                }
            }
            _ => Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
                "File extension must be .rkyv, .bin, or .json",
            )),
        })
    }

    /// Deserialize the vectorizer from JSON string.
    ///
    /// Args:
    ///     json: A JSON string containing the serialized vectorizer
    ///
    /// Returns:
    ///     A new `RustTfidfVectorizer` instance
    #[staticmethod]
    #[cfg(feature = "serde")]
    fn from_json(py: Python<'_>, json: &str) -> PyResult<Self> {
        py.detach(|| {
            let vectorizer = TfidfVectorizer::from_json(json).map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                    "Failed to deserialize vectorizer from JSON: {e}"
                ))
            })?;
            Ok(Self { inner: vectorizer })
        })
    }
}

#[pyclass(from_py_object)]
#[derive(Clone)]
pub struct RustTokenChunker {
    inner: TokenChunker,
}

#[pymethods]
impl RustTokenChunker {
    #[new]
    #[pyo3(signature = (chunk_size=150, overlap=15, min_chunk_size=30))]
    fn new(chunk_size: usize, overlap: usize, min_chunk_size: usize) -> Self {
        Self {
            inner: TokenChunker {
                chunk_size,
                overlap,
                min_chunk_size,
            },
        }
    }

    /// Chunk a single token sequence
    #[allow(clippy::needless_pass_by_value)]
    fn chunk(&self, tokens: Vec<u32>) -> Vec<Vec<u32>> {
        self.inner.chunk(&tokens)
    }

    /// Chunk multiple token sequences in parallel
    #[allow(clippy::needless_pass_by_value)]
    fn chunk_batch(&self, py: Python<'_>, token_sequences: Vec<Vec<u32>>) -> Vec<Vec<Vec<u32>>> {
        py.detach(|| self.inner.chunk_batch(&token_sequences))
    }

    fn to_dict(&self) -> std::collections::HashMap<String, usize> {
        std::collections::HashMap::from([
            ("chunk_size".to_string(), self.inner.chunk_size),
            ("overlap".to_string(), self.inner.overlap),
            ("min_chunk_size".to_string(), self.inner.min_chunk_size),
        ])
    }
}

#[pyclass(from_py_object)]
#[derive(Clone)]
pub enum RustCleaningMode {
    Training,
    Inference,
}

#[pyclass(from_py_object)]
#[derive(Clone)]
pub struct RustTextCleaner {
    inner: TextCleaner,
    mode: RustCleaningMode,
}

#[pymethods]
impl RustTextCleaner {
    #[new]
    #[allow(clippy::needless_pass_by_value)]
    fn new(mode: RustCleaningMode) -> Self {
        let inner = match mode {
            RustCleaningMode::Training => text_cleaner_for_training().clone(),
            RustCleaningMode::Inference => text_cleaner_for_inference().clone(),
        };

        Self { inner, mode }
    }

    /// Clean a single text string
    fn clean(&self, text: &str) -> String {
        self.inner.clean(text).to_string()
    }

    /// Clean multiple texts in parallel
    #[allow(clippy::needless_pass_by_value)]
    fn clean_batch(&self, python: Python<'_>, texts: Vec<String>) -> Vec<String> {
        python.detach(|| {
            self.inner.clean_batch(&texts)
            // texts
            //     .into_par_iter()
            //     .map(|text| self.inner.clean(text))
            //     .collect()
        })
    }

    pub fn is_training_mode(&self) -> bool {
        matches!(self.mode, RustCleaningMode::Training)
    }
}

/// Encode text into BPE token IDs
/// This is a utility function to expose tokenization to Python.
#[pyfunction]
#[allow(clippy::needless_pass_by_value)]
fn tokenize(py: Python<'_>, text: Vec<String>) -> Vec<Vec<u32>> {
    py.detach(|| tokenize_(&text))
}

/// Decode BPE token IDs back to text
#[pyfunction]
#[allow(clippy::needless_pass_by_value)]
fn reverse_tokenize(py: Python<'_>, tokens: Vec<u32>) -> String {
    py.detach(|| reverse_tokenize_(&tokens))
}

#[pymodule]
#[pyo3(name = "_is_it_slop_preprocessing_rust_bindings")]
fn is_it_slop_preprocessing(m: &Bound<'_, PyModule>) -> PyResult<()> {
    // pyo3_log::init();

    m.add("__version__", env!("CARGO_PKG_VERSION"))?;
    m.add_class::<RustVectorizerParams>()?;
    m.add_class::<RustTfidfVectorizer>()?;

    m.add_class::<RustTokenChunker>()?;

    m.add_class::<RustCleaningMode>()?;
    m.add_class::<RustTextCleaner>()?;

    m.add_function(wrap_pyfunction!(tokenize, m)?)?;
    m.add_function(wrap_pyfunction!(reverse_tokenize, m)?)?;

    Ok(())
}