tenflowers-dataset 0.1.1

Data pipeline and dataset utilities for TenfloweRS
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
//! Text dataset format support for NLP tasks
//!
//! This module provides support for loading text datasets commonly used in
//! natural language processing tasks such as classification, sentiment analysis,
//! and language modeling.

use crate::Dataset;
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use tenflowers_core::{Result, Tensor, TensorError};

#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};

/// Configuration for text dataset loading
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct TextConfig {
    /// Path to the text file
    pub file_path: PathBuf,
    /// Maximum sequence length (truncate or pad to this length)
    pub max_sequence_length: usize,
    /// Character to use for padding sequences
    pub pad_token: String,
    /// Character to use for unknown tokens
    pub unk_token: String,
    /// Character to use for beginning of sequence
    pub bos_token: Option<String>,
    /// Character to use for end of sequence
    pub eos_token: Option<String>,
    /// Whether to convert text to lowercase
    pub lowercase: bool,
    /// Minimum token frequency (tokens below this are replaced with unk_token)
    pub min_frequency: usize,
    /// Maximum vocabulary size (keep most frequent tokens)
    pub max_vocab_size: Option<usize>,
    /// Whether to split on whitespace or use character-level tokenization
    pub tokenization_strategy: TokenizationStrategy,
    /// Label extraction strategy
    pub label_strategy: LabelStrategy,
}

impl Default for TextConfig {
    fn default() -> Self {
        Self {
            file_path: PathBuf::new(),
            max_sequence_length: 512,
            pad_token: "<pad>".to_string(),
            unk_token: "<unk>".to_string(),
            bos_token: Some("<bos>".to_string()),
            eos_token: Some("<eos>".to_string()),
            lowercase: true,
            min_frequency: 2,
            max_vocab_size: Some(10000),
            tokenization_strategy: TokenizationStrategy::WordLevel,
            label_strategy: LabelStrategy::FromFilename,
        }
    }
}

/// Tokenization strategy for text processing
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub enum TokenizationStrategy {
    /// Split on whitespace
    WordLevel,
    /// Character-level tokenization
    CharLevel,
    /// Subword tokenization (simple byte-pair encoding)
    Subword,
}

/// Strategy for extracting labels from text data
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub enum LabelStrategy {
    /// Extract label from filename (e.g., "pos_review1.txt" -> "pos")
    FromFilename,
    /// Extract label from first line of file
    FromFirstLine,
    /// Extract label from directory name
    FromDirectory,
    /// No labels (unsupervised learning)
    NoLabels,
    /// Labels provided separately
    External(Vec<String>),
}

/// Vocabulary for text tokenization
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct Vocabulary {
    /// Token to ID mapping
    pub token_to_id: HashMap<String, usize>,
    /// ID to token mapping
    pub id_to_token: HashMap<usize, String>,
    /// Token frequencies
    pub token_counts: HashMap<String, usize>,
    /// Special tokens
    pub pad_token_id: usize,
    pub unk_token_id: usize,
    pub bos_token_id: Option<usize>,
    pub eos_token_id: Option<usize>,
}

impl Vocabulary {
    /// Create a new vocabulary from text data
    pub fn from_texts(texts: &[String], config: &TextConfig) -> Self {
        let mut token_counts = HashMap::new();

        // Count token frequencies
        for text in texts {
            let tokens = Self::tokenize_text(text, &config.tokenization_strategy, config.lowercase);
            for token in tokens {
                *token_counts.entry(token).or_insert(0) += 1;
            }
        }

        // Filter by minimum frequency
        token_counts.retain(|_, &mut count| count >= config.min_frequency);

        // Sort by frequency and limit vocabulary size
        let mut sorted_tokens: Vec<_> = token_counts.iter().collect();
        sorted_tokens.sort_by(|a, b| b.1.cmp(a.1));

        if let Some(max_size) = config.max_vocab_size {
            sorted_tokens.truncate(max_size.saturating_sub(4)); // Reserve space for special tokens
        }

        // Build vocabulary mappings
        let mut token_to_id = HashMap::new();
        let mut id_to_token = HashMap::new();
        let mut next_id = 0;

        // Add special tokens first
        let pad_token_id = next_id;
        token_to_id.insert(config.pad_token.clone(), next_id);
        id_to_token.insert(next_id, config.pad_token.clone());
        next_id += 1;

        let unk_token_id = next_id;
        token_to_id.insert(config.unk_token.clone(), next_id);
        id_to_token.insert(next_id, config.unk_token.clone());
        next_id += 1;

        let bos_token_id = if let Some(ref bos_token) = config.bos_token {
            let id = next_id;
            token_to_id.insert(bos_token.clone(), next_id);
            id_to_token.insert(next_id, bos_token.clone());
            next_id += 1;
            Some(id)
        } else {
            None
        };

        let eos_token_id = if let Some(ref eos_token) = config.eos_token {
            let id = next_id;
            token_to_id.insert(eos_token.clone(), next_id);
            id_to_token.insert(next_id, eos_token.clone());
            next_id += 1;
            Some(id)
        } else {
            None
        };

        // Add regular tokens
        for (token, _) in sorted_tokens {
            if !token_to_id.contains_key(token) {
                token_to_id.insert(token.clone(), next_id);
                id_to_token.insert(next_id, token.clone());
                next_id += 1;
            }
        }

        Self {
            token_to_id,
            id_to_token,
            token_counts,
            pad_token_id,
            unk_token_id,
            bos_token_id,
            eos_token_id,
        }
    }

    /// Tokenize text according to strategy
    fn tokenize_text(text: &str, strategy: &TokenizationStrategy, lowercase: bool) -> Vec<String> {
        let processed_text = if lowercase {
            text.to_lowercase()
        } else {
            text.to_string()
        };

        match strategy {
            TokenizationStrategy::WordLevel => processed_text
                .split_whitespace()
                .map(|s| s.to_string())
                .collect(),
            TokenizationStrategy::CharLevel => {
                processed_text.chars().map(|c| c.to_string()).collect()
            }
            TokenizationStrategy::Subword => {
                // Simple subword tokenization (could be enhanced with BPE)
                Self::simple_subword_tokenize(&processed_text)
            }
        }
    }

    /// Simple subword tokenization (splits on punctuation and whitespace)
    fn simple_subword_tokenize(text: &str) -> Vec<String> {
        let mut tokens = Vec::new();
        let mut current_token = String::new();

        for ch in text.chars() {
            if ch.is_whitespace() || ch.is_ascii_punctuation() {
                if !current_token.is_empty() {
                    tokens.push(current_token.clone());
                    current_token.clear();
                }
                if !ch.is_whitespace() {
                    tokens.push(ch.to_string());
                }
            } else {
                current_token.push(ch);
            }
        }

        if !current_token.is_empty() {
            tokens.push(current_token);
        }

        tokens
    }

    /// Encode text to token IDs
    pub fn encode(&self, text: &str, config: &TextConfig) -> Vec<usize> {
        let tokens = Self::tokenize_text(text, &config.tokenization_strategy, config.lowercase);
        let mut token_ids = Vec::new();

        // Add BOS token if configured
        if let Some(bos_id) = self.bos_token_id {
            token_ids.push(bos_id);
        }

        // Convert tokens to IDs
        for token in tokens {
            let id = self.token_to_id.get(&token).unwrap_or(&self.unk_token_id);
            token_ids.push(*id);

            // Stop if we're approaching max length (save space for EOS)
            if token_ids.len() >= config.max_sequence_length - 1 {
                break;
            }
        }

        // Add EOS token if configured
        if let Some(eos_id) = self.eos_token_id {
            if token_ids.len() < config.max_sequence_length {
                token_ids.push(eos_id);
            }
        }

        // Pad or truncate to max length
        if token_ids.len() < config.max_sequence_length {
            token_ids.resize(config.max_sequence_length, self.pad_token_id);
        } else {
            token_ids.truncate(config.max_sequence_length);
        }

        token_ids
    }

    /// Decode token IDs back to text
    pub fn decode(&self, token_ids: &[usize]) -> String {
        let tokens: Vec<String> = token_ids
            .iter()
            .filter_map(|&id| self.id_to_token.get(&id))
            .filter(|token| {
                // Skip special tokens in output
                *token != &self.id_to_token[&self.pad_token_id]
                    && Some(*token)
                        != self
                            .bos_token_id
                            .as_ref()
                            .and_then(|id| self.id_to_token.get(id))
                    && Some(*token)
                        != self
                            .eos_token_id
                            .as_ref()
                            .and_then(|id| self.id_to_token.get(id))
            })
            .cloned()
            .collect();

        tokens.join(" ")
    }

    /// Get vocabulary size
    pub fn len(&self) -> usize {
        self.token_to_id.len()
    }

    /// Check if vocabulary is empty
    pub fn is_empty(&self) -> bool {
        self.token_to_id.is_empty()
    }
}

/// Text dataset for NLP tasks
#[derive(Debug, Clone)]
pub struct TextDataset<T> {
    samples: Vec<(Tensor<T>, Tensor<T>)>,
    vocabulary: Vocabulary,
    label_vocab: Option<HashMap<String, usize>>,
    config: TextConfig,
}

impl<T> TextDataset<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Zero
        + scirs2_core::numeric::ToPrimitive
        + scirs2_core::numeric::NumCast
        + Send
        + Sync
        + 'static,
{
    /// Create a new text dataset from configuration
    pub fn from_config(config: TextConfig) -> Result<Self> {
        let (texts, labels) = Self::load_text_data(&config)?;

        // Build vocabulary from texts
        let vocabulary = Vocabulary::from_texts(&texts, &config);

        // Build label vocabulary if needed
        let label_vocab = if !labels.is_empty() {
            let mut unique_labels: Vec<_> = labels.to_vec();
            unique_labels.sort();
            unique_labels.dedup();

            let label_vocab: HashMap<String, usize> = unique_labels
                .into_iter()
                .enumerate()
                .map(|(i, label)| (label, i))
                .collect();
            Some(label_vocab)
        } else {
            None
        };

        // Convert texts and labels to tensors
        let mut samples = Vec::new();
        for (text, label) in texts.iter().zip(labels.iter()) {
            let token_ids = vocabulary.encode(text, &config);
            let feature_tensor = Self::ids_to_tensor(&token_ids)?;

            let label_tensor = if let Some(ref label_vocab) = label_vocab {
                let label_id = label_vocab.get(label).unwrap_or(&0);
                Self::scalar_to_tensor(*label_id)?
            } else {
                // No labels case - use dummy label
                Self::scalar_to_tensor(0)?
            };

            samples.push((feature_tensor, label_tensor));
        }

        Ok(Self {
            samples,
            vocabulary,
            label_vocab,
            config,
        })
    }

    /// Create a new text dataset from file path
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let config = TextConfig {
            file_path: path.as_ref().to_path_buf(),
            ..Default::default()
        };
        Self::from_config(config)
    }

    /// Load text data and labels from file
    fn load_text_data(config: &TextConfig) -> Result<(Vec<String>, Vec<String>)> {
        let file = File::open(&config.file_path)
            .map_err(|e| TensorError::invalid_argument(format!("Cannot open text file: {e}")))?;

        let reader = BufReader::new(file);
        let mut texts = Vec::new();
        let mut labels = Vec::new();

        for (line_num, line) in reader.lines().enumerate() {
            let line = line.map_err(|e| {
                TensorError::invalid_argument(format!("Cannot read line {}: {}", line_num + 1, e))
            })?;

            if line.trim().is_empty() {
                continue;
            }

            match &config.label_strategy {
                LabelStrategy::FromFirstLine => {
                    if line_num == 0 {
                        // First line contains labels
                        continue;
                    }
                    texts.push(line);
                    labels.push("unlabeled".to_string());
                }
                LabelStrategy::FromFilename => {
                    texts.push(line);
                    let filename = config
                        .file_path
                        .file_stem()
                        .and_then(|s| s.to_str())
                        .unwrap_or("unknown");

                    // Try to extract label from filename (e.g., "pos_review.txt" -> "pos")
                    let label = if let Some(pos) = filename.find('_') {
                        filename[..pos].to_string()
                    } else {
                        filename.to_string()
                    };
                    labels.push(label);
                }
                LabelStrategy::FromDirectory => {
                    texts.push(line);
                    let dir_name = config
                        .file_path
                        .parent()
                        .and_then(|p| p.file_name())
                        .and_then(|s| s.to_str())
                        .unwrap_or("unknown");
                    labels.push(dir_name.to_string());
                }
                LabelStrategy::NoLabels => {
                    texts.push(line);
                    // No labels needed
                }
                LabelStrategy::External(external_labels) => {
                    texts.push(line);
                    if line_num < external_labels.len() {
                        labels.push(external_labels[line_num].clone());
                    } else {
                        labels.push("unknown".to_string());
                    }
                }
            }
        }

        if texts.is_empty() {
            return Err(TensorError::invalid_argument(
                "No text data found in file".to_string(),
            ));
        }

        Ok((texts, labels))
    }

    /// Convert token IDs to tensor
    fn ids_to_tensor(ids: &[usize]) -> Result<Tensor<T>> {
        let data: Vec<T> = ids
            .iter()
            .map(|&id| scirs2_core::num_traits::NumCast::from(id).unwrap_or_else(T::default))
            .collect();
        Tensor::from_vec(data, &[ids.len()])
    }

    /// Convert scalar to tensor
    fn scalar_to_tensor(value: usize) -> Result<Tensor<T>> {
        let tensor_val = scirs2_core::num_traits::NumCast::from(value).unwrap_or_else(T::default);
        Ok(Tensor::from_scalar(tensor_val))
    }

    /// Get the vocabulary used by this dataset
    pub fn vocabulary(&self) -> &Vocabulary {
        &self.vocabulary
    }

    /// Get the label vocabulary if available
    pub fn label_vocabulary(&self) -> Option<&HashMap<String, usize>> {
        self.label_vocab.as_ref()
    }

    /// Get the configuration used for this dataset
    pub fn config(&self) -> &TextConfig {
        &self.config
    }

    /// Get statistics about the loaded dataset
    pub fn info(&self) -> TextDatasetInfo {
        TextDatasetInfo {
            sample_count: self.samples.len(),
            vocabulary_size: self.vocabulary.len(),
            max_sequence_length: self.config.max_sequence_length,
            label_count: self.label_vocab.as_ref().map(|lv| lv.len()),
            file_path: self.config.file_path.clone(),
        }
    }
}

impl<T> Dataset<T> for TextDataset<T>
where
    T: Clone + Default + scirs2_core::numeric::Zero + Send + Sync + 'static,
{
    fn len(&self) -> usize {
        self.samples.len()
    }

    fn get(&self, index: usize) -> Result<(Tensor<T>, Tensor<T>)> {
        if index >= self.samples.len() {
            return Err(TensorError::invalid_argument(format!(
                "Index {} out of bounds for dataset of length {}",
                index,
                self.samples.len()
            )));
        }
        Ok(self.samples[index].clone())
    }
}

/// Tokenized text dataset that preprocesses all text into token sequences
pub type TokenizedDataset<T> = TextDataset<T>;

/// Information about a text dataset
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct TextDatasetInfo {
    pub sample_count: usize,
    pub vocabulary_size: usize,
    pub max_sequence_length: usize,
    pub label_count: Option<usize>,
    pub file_path: PathBuf,
}

/// Builder for text datasets
#[derive(Debug, Default)]
pub struct TextDatasetBuilder {
    config: TextConfig,
}

impl TextDatasetBuilder {
    /// Create a new text dataset builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the file path
    pub fn file_path<P: AsRef<Path>>(mut self, path: P) -> Self {
        self.config.file_path = path.as_ref().to_path_buf();
        self
    }

    /// Set maximum sequence length
    pub fn max_sequence_length(mut self, length: usize) -> Self {
        self.config.max_sequence_length = length;
        self
    }

    /// Set tokenization strategy
    pub fn tokenization_strategy(mut self, strategy: TokenizationStrategy) -> Self {
        self.config.tokenization_strategy = strategy;
        self
    }

    /// Set label extraction strategy
    pub fn label_strategy(mut self, strategy: LabelStrategy) -> Self {
        self.config.label_strategy = strategy;
        self
    }

    /// Enable or disable lowercase conversion
    pub fn lowercase(mut self, lowercase: bool) -> Self {
        self.config.lowercase = lowercase;
        self
    }

    /// Set minimum token frequency
    pub fn min_frequency(mut self, freq: usize) -> Self {
        self.config.min_frequency = freq;
        self
    }

    /// Set maximum vocabulary size
    pub fn max_vocab_size(mut self, size: usize) -> Self {
        self.config.max_vocab_size = Some(size);
        self
    }

    /// Set pad token
    pub fn pad_token<S: Into<String>>(mut self, token: S) -> Self {
        self.config.pad_token = token.into();
        self
    }

    /// Set unknown token
    pub fn unk_token<S: Into<String>>(mut self, token: S) -> Self {
        self.config.unk_token = token.into();
        self
    }

    /// Build the text dataset
    pub fn build<T>(self) -> Result<TextDataset<T>>
    where
        T: Clone
            + Default
            + scirs2_core::numeric::Zero
            + scirs2_core::numeric::ToPrimitive
            + scirs2_core::numeric::NumCast
            + Send
            + Sync
            + 'static,
    {
        TextDataset::from_config(self.config)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_text_config_default() {
        let config = TextConfig::default();
        assert_eq!(config.max_sequence_length, 512);
        assert_eq!(config.pad_token, "<pad>");
        assert_eq!(config.unk_token, "<unk>");
        assert!(config.lowercase);
        assert_eq!(config.min_frequency, 2);
    }

    #[test]
    fn test_vocabulary_creation() {
        let texts = vec![
            "hello world".to_string(),
            "world peace".to_string(),
            "hello peace".to_string(),
        ];

        let config = TextConfig::default();
        let vocab = Vocabulary::from_texts(&texts, &config);

        assert!(vocab.len() > 0);
        assert!(vocab.token_to_id.contains_key("hello"));
        assert!(vocab.token_to_id.contains_key("world"));
        assert!(vocab.token_to_id.contains_key("peace"));
        assert!(vocab.token_to_id.contains_key(&config.pad_token));
        assert!(vocab.token_to_id.contains_key(&config.unk_token));
    }

    #[test]
    fn test_tokenization_strategies() {
        let text = "Hello, world!";

        let word_tokens = Vocabulary::tokenize_text(text, &TokenizationStrategy::WordLevel, true);
        assert_eq!(word_tokens, vec!["hello,", "world!"]);

        let char_tokens = Vocabulary::tokenize_text(text, &TokenizationStrategy::CharLevel, true);
        assert!(char_tokens.len() > word_tokens.len());
        assert!(char_tokens.contains(&"h".to_string()));
        assert!(char_tokens.contains(&"!".to_string()));

        let subword_tokens = Vocabulary::tokenize_text(text, &TokenizationStrategy::Subword, true);
        assert!(subword_tokens.contains(&"hello".to_string()));
        assert!(subword_tokens.contains(&",".to_string()));
        assert!(subword_tokens.contains(&"world".to_string()));
        assert!(subword_tokens.contains(&"!".to_string()));
    }

    #[test]
    fn test_encode_decode() {
        let texts = vec!["hello world".to_string(), "world peace".to_string()];

        let config = TextConfig {
            min_frequency: 1, // Allow single occurrence tokens
            ..Default::default()
        };
        let vocab = Vocabulary::from_texts(&texts, &config);

        let encoded = vocab.encode("hello world", &config);
        assert_eq!(encoded.len(), config.max_sequence_length);

        let decoded = vocab.decode(&encoded);

        // The decoded text should contain the original words
        // Note: case might be different due to lowercase processing
        assert!(decoded.contains("hello") || decoded.contains("Hello"));
        assert!(decoded.contains("world") || decoded.contains("World"));
    }

    #[test]
    fn test_text_dataset_from_file() {
        // Create a temporary text file
        let mut temp_file = NamedTempFile::new().expect("test: temp file creation should succeed");
        let text_content =
            "This is a positive review\nThis is another positive review\nGreat product!";
        temp_file
            .write_all(text_content.as_bytes())
            .expect("test: write should succeed");
        temp_file.flush().expect("test: flush should succeed");

        let dataset = TextDataset::<f32>::from_file(temp_file.path())
            .expect("test: loading from file should succeed");

        assert_eq!(dataset.len(), 3);
        assert!(dataset.vocabulary().len() > 0);

        let (features, label) = dataset.get(0).expect("index should be in bounds");
        assert_eq!(
            features.shape().dims()[0],
            dataset.config().max_sequence_length
        );
    }

    #[test]
    fn test_text_dataset_builder() {
        let mut temp_file = NamedTempFile::new().expect("test: temp file creation should succeed");
        let text_content = "Short text\nAnother text";
        temp_file
            .write_all(text_content.as_bytes())
            .expect("test: write should succeed");
        temp_file.flush().expect("test: flush should succeed");

        let dataset = TextDatasetBuilder::new()
            .file_path(temp_file.path())
            .max_sequence_length(10)
            .tokenization_strategy(TokenizationStrategy::WordLevel)
            .lowercase(true)
            .min_frequency(1)
            .build::<f32>()
            .expect("test: operation should succeed");

        assert_eq!(dataset.len(), 2);
        assert_eq!(dataset.config().max_sequence_length, 10);

        let info = dataset.info();
        assert_eq!(info.sample_count, 2);
        assert_eq!(info.max_sequence_length, 10);
    }

    #[test]
    fn test_label_strategies() {
        // Test filename-based labeling
        let mut temp_file = NamedTempFile::with_prefix("positive_")
            .expect("test: temp file creation should succeed");
        let text_content = "This is positive text";
        temp_file
            .write_all(text_content.as_bytes())
            .expect("test: write should succeed");
        temp_file.flush().expect("test: flush should succeed");

        let config = TextConfig {
            file_path: temp_file.path().to_path_buf(),
            label_strategy: LabelStrategy::FromFilename,
            ..Default::default()
        };

        let dataset = TextDataset::<f32>::from_config(config)
            .expect("test: loading from config should succeed");
        assert!(dataset.label_vocabulary().is_some());
        assert!(dataset
            .label_vocabulary()
            .expect("test: label vocabulary should be present")
            .contains_key("positive"));
    }

    #[test]
    fn test_vocabulary_size_limit() {
        let texts = vec![
            "a b c d e".to_string(),
            "f g h i j".to_string(),
            "k l m n o".to_string(),
        ];

        let config = TextConfig {
            max_vocab_size: Some(8), // Very small vocabulary
            min_frequency: 1,
            ..Default::default()
        };

        let vocab = Vocabulary::from_texts(&texts, &config);

        // Should have special tokens + limited regular tokens
        assert!(vocab.len() <= 8);
        assert!(vocab.token_to_id.contains_key(&config.pad_token));
        assert!(vocab.token_to_id.contains_key(&config.unk_token));
    }

    #[test]
    fn test_empty_text_file() {
        let temp_file = NamedTempFile::new().expect("test: temp file creation should succeed");
        // Empty file

        let result = TextDataset::<f32>::from_file(temp_file.path());
        assert!(result.is_err());
    }
}