sklears-feature-extraction 0.2.0

Feature extraction from raw data (text, images)
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
//! Text processing and vectorization tests
//!
//! This module contains tests for text feature extraction functionality,
//! including count vectorizers, TF-IDF, and text preprocessing.

use crate::text;
use sklears_core::traits::Fit;
use std::collections::HashSet;

#[test]
fn test_count_vectorizer() {
    let documents = vec![
        "the quick brown fox".to_string(),
        "the quick brown dog".to_string(),
        "the lazy dog".to_string(),
    ];

    let vectorizer = text::CountVectorizer::new();
    let fitted = vectorizer
        .fit(&documents, &())
        .expect("operation should succeed");
    let features = fitted
        .transform(&documents)
        .expect("operation should succeed");

    assert_eq!(features.nrows(), 3);
    assert!(features.ncols() > 0);

    // Check that vocabulary contains expected words
    let vocab = fitted.get_vocabulary();
    if let Some(vocab_map) = vocab {
        assert!(vocab_map.contains_key("the"));
        assert!(vocab_map.contains_key("quick"));
        assert!(vocab_map.contains_key("brown"));
    }
}

#[test]
fn test_tfidf_vectorizer() {
    let documents = vec![
        "the quick brown fox".to_string(),
        "the quick brown dog".to_string(),
        "the lazy dog".to_string(),
    ];

    let vectorizer = text::TfidfVectorizer::new();
    let fitted = vectorizer
        .fit(&documents, &())
        .expect("operation should succeed");
    let features = fitted
        .transform(&documents)
        .expect("operation should succeed");

    assert_eq!(features.nrows(), 3);
    assert!(features.ncols() > 0);

    // Check that IDF weights are computed
    let idf_weights = fitted.get_idf_weights();
    assert_eq!(idf_weights.len(), features.ncols());
}

#[test]
fn test_count_vectorizer_binary() {
    let documents = vec![
        "the the quick brown fox".to_string(),
        "the quick brown dog".to_string(),
    ];

    let vectorizer = text::CountVectorizer::new().binary(true);
    let fitted = vectorizer
        .fit(&documents, &())
        .expect("operation should succeed");
    let features = fitted
        .transform(&documents)
        .expect("operation should succeed");

    // All non-zero entries should be 1.0
    for &value in features.iter() {
        if value > 0.0 {
            assert!((value - 1.0).abs() < 1e-10);
        }
    }
}

#[test]
fn test_count_vectorizer_stop_words() {
    let documents = vec![
        "the quick brown fox".to_string(),
        "the quick brown dog".to_string(),
    ];

    let mut stop_words = HashSet::new();
    stop_words.insert("the".to_string());
    let stop_words_vec: Vec<String> = stop_words.into_iter().collect();

    let vectorizer = text::CountVectorizer::new().stop_words(Some(stop_words_vec));
    let fitted = vectorizer
        .fit(&documents, &())
        .expect("operation should succeed");

    let vocab = fitted.get_vocabulary();
    if let Some(vocab_map) = vocab {
        assert!(!vocab_map.contains_key("the")); // Stop word should be excluded
        assert!(vocab_map.contains_key("quick")); // Regular word should be included
    }
}

#[test]
fn test_count_vectorizer_ngram_range() {
    let documents = vec![
        "the quick brown fox".to_string(),
        "the quick brown dog".to_string(),
    ];

    let vectorizer = text::CountVectorizer::new().ngram_range((1, 2)); // Unigrams and bigrams
    let fitted = vectorizer
        .fit(&documents, &())
        .expect("operation should succeed");
    let features = fitted
        .transform(&documents)
        .expect("operation should succeed");
    assert_eq!(features.nrows(), documents.len());

    let vocab = fitted.get_vocabulary();

    if let Some(vocab_map) = vocab {
        // Should contain unigrams
        assert!(vocab_map.contains_key("quick"));
        assert!(vocab_map.contains_key("brown"));

        // Should contain bigrams
        assert!(vocab_map.contains_key("the quick") || vocab_map.contains_key("quick brown"));
    }
}

#[test]
fn test_count_vectorizer_min_max_df() {
    let documents = vec![
        "common word rare".to_string(),
        "common word unusual".to_string(),
        "common word unique".to_string(),
        "common word distinct".to_string(),
    ];

    let vectorizer = text::CountVectorizer::new()
        .min_df(2) // Word must appear in at least 2 documents
        .max_df(3.0); // Word must appear in at most 3 documents

    let fitted = vectorizer
        .fit(&documents, &())
        .expect("operation should succeed");
    let _vocab = fitted.get_vocabulary();

    // "common" and "word" appear in all 4 documents, so should be excluded by max_df=3
    // Individual unique words appear only once, so should be excluded by min_df=2
    // This particular configuration might result in no terms, which is valid

    let features = fitted
        .transform(&documents)
        .expect("operation should succeed");
    assert_eq!(features.nrows(), 4);
}

#[test]
fn test_tfidf_vectorizer_sublinear_tf() {
    let documents = vec!["cat cat cat dog".to_string(), "cat dog dog dog".to_string()];

    let vectorizer_regular = text::TfidfVectorizer::new().sublinear_tf(false);
    let vectorizer_sublinear = text::TfidfVectorizer::new().sublinear_tf(true);

    let fitted_regular = vectorizer_regular
        .fit(&documents, &())
        .expect("operation should succeed");
    let fitted_sublinear = vectorizer_sublinear
        .fit(&documents, &())
        .expect("operation should succeed");

    let features_regular = fitted_regular
        .transform(&documents)
        .expect("operation should succeed");
    let features_sublinear = fitted_sublinear
        .transform(&documents)
        .expect("operation should succeed");

    assert_eq!(features_regular.shape(), features_sublinear.shape());

    // Features should be different due to sublinear scaling
    let mut features_differ = false;
    for (reg, sub) in features_regular.iter().zip(features_sublinear.iter()) {
        if (reg - sub).abs() > 1e-10 {
            features_differ = true;
            break;
        }
    }
    assert!(features_differ || features_regular == features_sublinear);

    // In most cases they should differ, but might be identical for some edge cases
    // We just ensure both produce valid finite results
    for &val in features_regular.iter() {
        assert!(val.is_finite());
    }
    for &val in features_sublinear.iter() {
        assert!(val.is_finite());
    }
}

#[test]
fn test_tfidf_vectorizer_normalization() {
    let documents = vec![
        "the quick brown fox jumps".to_string(),
        "the lazy dog sleeps".to_string(),
    ];

    let vectorizer_l1 = text::TfidfVectorizer::new().norm(Some("l1".to_string()));
    let vectorizer_l2 = text::TfidfVectorizer::new().norm(Some("l2".to_string()));
    let vectorizer_none = text::TfidfVectorizer::new().norm(None);

    let fitted_l1 = vectorizer_l1
        .fit(&documents, &())
        .expect("operation should succeed");
    let fitted_l2 = vectorizer_l2
        .fit(&documents, &())
        .expect("operation should succeed");
    let fitted_none = vectorizer_none
        .fit(&documents, &())
        .expect("operation should succeed");

    let features_l1 = fitted_l1
        .transform(&documents)
        .expect("operation should succeed");
    let features_l2 = fitted_l2
        .transform(&documents)
        .expect("operation should succeed");
    let features_none = fitted_none
        .transform(&documents)
        .expect("operation should succeed");

    // Check L1 normalization - each row should sum to 1
    for i in 0..features_l1.nrows() {
        let row_sum: f64 = features_l1.row(i).iter().map(|&x| x.abs()).sum();
        if row_sum > 0.0 {
            assert!(
                (row_sum - 1.0).abs() < 1e-10,
                "L1 normalization failed for row {}",
                i
            );
        }
    }

    // Check L2 normalization - each row should have norm 1
    for i in 0..features_l2.nrows() {
        let row_norm: f64 = features_l2
            .row(i)
            .iter()
            .map(|&x| x * x)
            .sum::<f64>()
            .sqrt();
        if row_norm > 0.0 {
            assert!(
                (row_norm - 1.0).abs() < 1e-10,
                "L2 normalization failed for row {}",
                i
            );
        }
    }

    // All features should be finite
    for &val in features_l1.iter() {
        assert!(val.is_finite());
    }
    for &val in features_l2.iter() {
        assert!(val.is_finite());
    }
    for &val in features_none.iter() {
        assert!(val.is_finite());
    }
}

#[test]
fn test_count_vectorizer_max_features() {
    let documents = vec![
        "apple banana cherry date elderberry".to_string(),
        "fig grape honeydew kiwi lemon".to_string(),
        "mango nectarine orange papaya quince".to_string(),
    ];

    let vectorizer = text::CountVectorizer::new().max_features(Some(5));
    let fitted = vectorizer
        .fit(&documents, &())
        .expect("operation should succeed");
    let features = fitted
        .transform(&documents)
        .expect("operation should succeed");

    let vocab = fitted.get_vocabulary();

    // Should have at most 5 features
    if let Some(vocab_map) = &vocab {
        assert!(vocab_map.len() <= 5);
    }
    assert!(features.ncols() <= 5);

    if let Some(vocab_map) = &vocab {
        if !vocab_map.is_empty() {
            assert_eq!(vocab_map.len(), features.ncols());
        }
    }
}

#[test]
fn test_text_vectorizer_empty_documents() {
    let documents = vec!["".to_string(), "word".to_string(), "".to_string()];

    let vectorizer = text::CountVectorizer::new();
    let fitted = vectorizer
        .fit(&documents, &())
        .expect("operation should succeed");
    let features = fitted
        .transform(&documents)
        .expect("operation should succeed");

    assert_eq!(features.nrows(), 3);

    // Empty documents should result in zero rows
    let empty_row_sum: f64 = features.row(0).iter().sum();
    let word_row_sum: f64 = features.row(1).iter().sum();
    let empty_row2_sum: f64 = features.row(2).iter().sum();

    assert_eq!(empty_row_sum, 0.0);
    assert!(word_row_sum > 0.0);
    assert_eq!(empty_row2_sum, 0.0);
}

#[test]
fn test_tfidf_vectorizer_single_document() {
    let documents = vec!["single document with multiple words".to_string()];

    let vectorizer = text::TfidfVectorizer::new();
    let fitted = vectorizer
        .fit(&documents, &())
        .expect("operation should succeed");
    let features = fitted
        .transform(&documents)
        .expect("operation should succeed");

    assert_eq!(features.nrows(), 1);
    assert!(features.ncols() > 0);

    // With only one document, IDF should be 1.0 for all terms
    let idf_weights = fitted.get_idf_weights();
    for &idf in idf_weights.iter() {
        assert!((idf - 1.0).abs() < 1e-10 || idf.is_finite());
    }

    // All features should be finite
    for &val in features.iter() {
        assert!(val.is_finite());
    }
}

#[test]
fn test_count_vectorizer_case_sensitivity() {
    let documents = vec![
        "The Quick Brown Fox".to_string(),
        "the quick brown fox".to_string(),
    ];

    let vectorizer_case_sensitive = text::CountVectorizer::new().lowercase(false);
    let vectorizer_case_insensitive = text::CountVectorizer::new().lowercase(true);

    let fitted_sensitive = vectorizer_case_sensitive
        .fit(&documents, &())
        .expect("operation should succeed");
    let fitted_insensitive = vectorizer_case_insensitive
        .fit(&documents, &())
        .expect("operation should succeed");

    let vocab_sensitive = fitted_sensitive.get_vocabulary();
    let vocab_insensitive = fitted_insensitive.get_vocabulary();

    // Compare vocabularies if both are available
    if let (Some(vocab_s), Some(vocab_i)) = (vocab_sensitive, vocab_insensitive) {
        // Case sensitive should have both "The" and "the"
        if vocab_s.len() > vocab_i.len() {
            // This suggests case sensitivity is working
            assert!(vocab_s.len() >= vocab_i.len());
        }

        // Case insensitive should only have "the"
        if vocab_i.contains_key("the") {
            assert!(!vocab_i.contains_key("The") || vocab_i.len() == vocab_s.len());
        }
    }
}

#[test]
fn test_text_tokenization_patterns() {
    let documents = vec![
        "word1 word2, word3! word4?".to_string(),
        "email@domain.com and http://website.org".to_string(),
    ];

    let vectorizer = text::CountVectorizer::new();
    let fitted = vectorizer
        .fit(&documents, &())
        .expect("operation should succeed");
    let vocab = fitted.get_vocabulary();

    // Check that tokenization handles punctuation appropriately
    // The exact behavior depends on the tokenizer implementation

    if let Some(vocab_map) = vocab {
        // Should tokenize basic words
        let has_basic_words = vocab_map.contains_key("word1")
            || vocab_map.contains_key("word2")
            || vocab_map.contains_key("word3")
            || vocab_map.contains_key("word4");

        if !vocab_map.is_empty() {
            assert!(has_basic_words, "Should tokenize basic words");
        }
    }

    let features = fitted
        .transform(&documents)
        .expect("operation should succeed");
    assert_eq!(features.nrows(), 2);

    // All features should be finite
    for &val in features.iter() {
        assert!(val.is_finite());
    }
}

#[test]
fn test_vectorizer_transform_unseen_documents() {
    let train_documents = vec![
        "train document one".to_string(),
        "train document two".to_string(),
    ];

    let test_documents = vec![
        "test document with new words".to_string(),
        "document one".to_string(), // Partially overlaps
    ];

    let vectorizer = text::CountVectorizer::new();
    let fitted = vectorizer
        .fit(&train_documents, &())
        .expect("operation should succeed");

    // Transform unseen test documents
    let test_features = fitted
        .transform(&test_documents)
        .expect("operation should succeed");

    assert_eq!(test_features.nrows(), 2);
    assert_eq!(
        test_features.ncols(),
        fitted.get_vocabulary().map_or(0, |v| v.len())
    );

    // Should handle unseen words gracefully (ignore them)
    for &val in test_features.iter() {
        assert!(val.is_finite());
        assert!(val >= 0.0); // Count should be non-negative
    }
}

#[test]
fn test_tfidf_consistency() {
    let documents = vec![
        "cat dog bird".to_string(),
        "dog bird fish".to_string(),
        "bird fish cat".to_string(),
    ];

    let vectorizer = text::TfidfVectorizer::new();
    let fitted = vectorizer
        .fit(&documents, &())
        .expect("operation should succeed");

    // Transform same documents multiple times - should be consistent
    let features1 = fitted
        .transform(&documents)
        .expect("operation should succeed");
    let features2 = fitted
        .transform(&documents)
        .expect("operation should succeed");

    assert_eq!(features1.shape(), features2.shape());

    for (f1, f2) in features1.iter().zip(features2.iter()) {
        assert!((f1 - f2).abs() < 1e-12, "TF-IDF should be consistent");
    }
}

// ============================================================================
// Emotion Detection Tests
// ============================================================================

#[test]
fn test_emotion_detector_basic_joy() {
    let detector = text::EmotionDetector::new();
    let result = detector.detect_emotion("I am so happy and excited! This is wonderful!");

    assert_eq!(result.primary_emotion, text::EmotionType::Joy);
    assert!(result.confidence > 0.0);
    assert!(result.total_emotion_words > 0);
}

#[test]
fn test_emotion_detector_basic_sadness() {
    let detector = text::EmotionDetector::new();
    let result =
        detector.detect_emotion("I am so sad and depressed. I feel miserable and hopeless.");

    assert_eq!(result.primary_emotion, text::EmotionType::Sadness);
    assert!(result.confidence > 0.0);
}

#[test]
fn test_emotion_detector_basic_anger() {
    let detector = text::EmotionDetector::new();
    let result =
        detector.detect_emotion("I am furious and angry! This is outrageous and infuriating!");

    assert_eq!(result.primary_emotion, text::EmotionType::Anger);
    assert!(result.confidence > 0.0);
}

#[test]
fn test_emotion_detector_basic_fear() {
    let detector = text::EmotionDetector::new();
    let result =
        detector.detect_emotion("I am so scared and frightened. I feel terrified and anxious.");

    assert_eq!(result.primary_emotion, text::EmotionType::Fear);
    assert!(result.confidence > 0.0);
}

#[test]
fn test_emotion_detector_basic_surprise() {
    let detector = text::EmotionDetector::new();
    let result = detector.detect_emotion("Wow! I am so surprised and amazed! This is incredible!");

    assert_eq!(result.primary_emotion, text::EmotionType::Surprise);
    assert!(result.confidence > 0.0);
}

#[test]
fn test_emotion_detector_basic_disgust() {
    let detector = text::EmotionDetector::new();
    let result = detector
        .detect_emotion("This is disgusting and revolting! It's absolutely gross and nasty!");

    assert_eq!(result.primary_emotion, text::EmotionType::Disgust);
    assert!(result.confidence > 0.0);
}

#[test]
fn test_emotion_detector_neutral() {
    let detector = text::EmotionDetector::new();
    let result = detector.detect_emotion("The meeting is scheduled for tomorrow at 3 PM.");

    assert_eq!(result.primary_emotion, text::EmotionType::Neutral);
    assert_eq!(result.total_emotion_words, 0);
}

#[test]
fn test_emotion_detector_empty_text() {
    let detector = text::EmotionDetector::new();
    let result = detector.detect_emotion("");

    assert_eq!(result.primary_emotion, text::EmotionType::Neutral);
    assert_eq!(result.total_words, 0);
    assert_eq!(result.confidence, 0.0);
}

#[test]
fn test_emotion_detector_custom_words() {
    let detector = text::EmotionDetector::new().add_custom_emotion_words(
        text::EmotionType::Joy,
        vec!["awesome".to_string(), "epic".to_string()],
    );

    let result = detector.detect_emotion("This is totally epic and awesome!");

    assert_eq!(result.primary_emotion, text::EmotionType::Joy);
    assert!(result.confidence > 0.0);
}

#[test]
fn test_emotion_detector_mixed_emotions() {
    let detector = text::EmotionDetector::new();
    let result = detector.detect_emotion("I'm happy but also worried and anxious.");

    // Should detect primary emotion even with mixed emotions
    assert!(result.primary_emotion != text::EmotionType::Neutral);
    assert!(result.total_emotion_words >= 3);

    // Check that multiple emotions have scores
    let joy_score = result
        .emotion_scores
        .get(&text::EmotionType::Joy)
        .unwrap_or(&0.0);
    let fear_score = result
        .emotion_scores
        .get(&text::EmotionType::Fear)
        .unwrap_or(&0.0);

    assert!(joy_score > &0.0 || fear_score > &0.0);
}

#[test]
fn test_emotion_detector_secondary_emotion() {
    let detector = text::EmotionDetector::new();
    let result = detector.detect_emotion("I'm mostly happy and joyful but slightly worried.");

    let secondary = result.secondary_emotion();
    assert!(secondary.is_some());

    if let Some((emotion, score)) = secondary {
        assert!(score > 0.0);
        assert!(emotion != result.primary_emotion);
    }
}

#[test]
fn test_emotion_detector_intensity() {
    let detector = text::EmotionDetector::new();

    let high_intensity = detector.detect_emotion("Happy happy happy joy joy joy!");
    let low_intensity = detector.detect_emotion("This is a happy day with some other words.");

    assert!(high_intensity.intensity() > low_intensity.intensity());
}

#[test]
fn test_emotion_detector_case_insensitive() {
    let detector = text::EmotionDetector::new().case_sensitive(false);

    let result1 = detector.detect_emotion("HAPPY EXCITED WONDERFUL");
    let result2 = detector.detect_emotion("happy excited wonderful");

    assert_eq!(result1.primary_emotion, text::EmotionType::Joy);
    assert_eq!(result2.primary_emotion, text::EmotionType::Joy);
}

#[test]
fn test_emotion_detector_extract_features() {
    let detector = text::EmotionDetector::new();

    let documents = vec![
        "I am so happy and excited!".to_string(),
        "I feel sad and depressed.".to_string(),
        "I am angry and frustrated.".to_string(),
    ];

    let features = detector
        .extract_features(&documents)
        .expect("operation should succeed");

    // Should have correct shape: (n_documents, 14)
    assert_eq!(features.nrows(), 3);
    assert_eq!(features.ncols(), 14);

    // All features should be finite
    for &val in features.iter() {
        assert!(val.is_finite());
    }

    // Check that different emotions have different feature patterns
    let joy_features = features.row(0);
    let sadness_features = features.row(1);

    // Joy score should be higher for first document
    assert!(joy_features[0] > sadness_features[0]);

    // Sadness score should be higher for second document
    assert!(sadness_features[1] > joy_features[1]);
}

#[test]
fn test_emotion_detector_extract_features_empty() {
    let detector = text::EmotionDetector::new();
    let documents: Vec<String> = vec![];

    let result = detector.extract_features(&documents);
    assert!(result.is_err());
}

#[test]
fn test_emotion_detector_confidence_threshold() {
    let detector = text::EmotionDetector::new().min_confidence(0.5);

    // Low emotion word density should result in neutral
    let result = detector.detect_emotion("I'm slightly happy in this long sentence with many other words that are not emotional at all.");

    // With high confidence threshold, weak emotions should be classified as neutral
    assert!(result.confidence < 0.5 || result.primary_emotion != text::EmotionType::Neutral);
}

#[test]
fn test_emotion_detector_distribution() {
    let detector = text::EmotionDetector::new();

    let documents = vec![
        "I am happy!".to_string(),
        "I am joyful!".to_string(),
        "I am sad.".to_string(),
        "I am angry!".to_string(),
        "Neutral text here.".to_string(),
    ];

    let distribution = detector.analyze_distribution(&documents);

    // Should have multiple emotion types in distribution
    assert!(distribution.len() > 1);

    // Joy should appear twice
    assert_eq!(*distribution.get(&text::EmotionType::Joy).unwrap_or(&0), 2);

    // Sadness should appear once
    assert_eq!(
        *distribution.get(&text::EmotionType::Sadness).unwrap_or(&0),
        1
    );

    // Anger should appear once
    assert_eq!(
        *distribution.get(&text::EmotionType::Anger).unwrap_or(&0),
        1
    );
}

#[test]
fn test_emotion_detector_all_emotions() {
    let all_emotions = text::EmotionType::all_emotions();
    assert_eq!(all_emotions.len(), 6);

    // Should not include Neutral
    assert!(!all_emotions.contains(&text::EmotionType::Neutral));
}

#[test]
fn test_emotion_type_as_str() {
    assert_eq!(text::EmotionType::Joy.as_str(), "joy");
    assert_eq!(text::EmotionType::Sadness.as_str(), "sadness");
    assert_eq!(text::EmotionType::Anger.as_str(), "anger");
    assert_eq!(text::EmotionType::Fear.as_str(), "fear");
    assert_eq!(text::EmotionType::Surprise.as_str(), "surprise");
    assert_eq!(text::EmotionType::Disgust.as_str(), "disgust");
    assert_eq!(text::EmotionType::Neutral.as_str(), "neutral");
}

// ============================================================================
// Aspect-Based Sentiment Analysis Tests
// ============================================================================

#[test]
fn test_aspect_sentiment_basic() {
    let analyzer = text::AspectBasedSentimentAnalyzer::new()
        .add_aspects(vec!["food".to_string(), "service".to_string()]);

    let result = analyzer.analyze("The food was great but the service was terrible.");

    // Should find both aspects
    assert!(!result.is_empty());

    let food_aspect = result.iter().find(|a| a.aspect == "food");
    let service_aspect = result.iter().find(|a| a.aspect == "service");

    assert!(food_aspect.is_some());
    assert!(service_aspect.is_some());

    // Food should be positive
    if let Some(aspect) = food_aspect {
        assert_eq!(aspect.sentiment, text::SentimentPolarity::Positive);
    }

    // Service should be negative (check score is negative or neutral at minimum)
    if let Some(aspect) = service_aspect {
        // The sentiment should be detected (not necessarily always negative due to context)
        assert!(aspect.score <= 0.0 || aspect.sentiment != text::SentimentPolarity::Positive);
    }
}

#[test]
fn test_aspect_sentiment_auto_extract() {
    let analyzer = text::AspectBasedSentimentAnalyzer::new().auto_extract_aspects(true);

    let result = analyzer.analyze("The food was excellent and the atmosphere was wonderful.");

    // Should automatically extract aspects
    assert!(!result.is_empty());

    let has_food = result.iter().any(|a| a.aspect == "food");
    let has_atmosphere = result.iter().any(|a| a.aspect == "atmosphere");

    assert!(has_food || has_atmosphere);
}

#[test]
fn test_aspect_sentiment_context_window() {
    let analyzer = text::AspectBasedSentimentAnalyzer::new()
        .add_aspects(vec!["product".to_string()])
        .context_window(2);

    let result = analyzer.analyze("The product is great.");

    assert!(!result.is_empty());
    if let Some(aspect) = result.first() {
        assert_eq!(aspect.sentiment, text::SentimentPolarity::Positive);
    }
}

#[test]
fn test_aspect_sentiment_opinion_words() {
    let analyzer = text::AspectBasedSentimentAnalyzer::new().add_aspects(vec!["food".to_string()]);

    let result = analyzer.analyze("The food was absolutely wonderful and amazing!");

    assert!(!result.is_empty());
    if let Some(aspect) = result.first() {
        // Should have captured opinion words
        assert!(!aspect.opinion_words.is_empty());
    }
}

#[test]
fn test_aspect_sentiment_min_confidence() {
    let analyzer = text::AspectBasedSentimentAnalyzer::new()
        .add_aspects(vec!["product".to_string()])
        .min_confidence(0.5);

    // Low sentiment word density
    let _result1 = analyzer.analyze("The product is here in this place with other things.");

    // High sentiment word density
    let result2 = analyzer.analyze("The product is great and wonderful!");

    // Second result should have higher confidence
    if !result2.is_empty() {
        assert!(result2[0].confidence >= 0.0);
    }
}

#[test]
fn test_aspect_sentiment_multi_word() {
    let analyzer =
        text::AspectBasedSentimentAnalyzer::new().add_aspects(vec!["customer service".to_string()]);

    let result = analyzer.analyze("The customer service was excellent and helpful.");

    let has_customer_service = result.iter().any(|a| a.aspect == "customer service");
    assert!(has_customer_service || !result.is_empty()); // Should handle multi-word aspects
}

#[test]
fn test_aspect_sentiment_extract_features() {
    let analyzer = text::AspectBasedSentimentAnalyzer::new()
        .add_aspects(vec!["food".to_string(), "service".to_string()]);

    let documents = vec![
        "The food was great but the service was terrible.".to_string(),
        "Both food and service were excellent!".to_string(),
        "The service was okay, food was amazing.".to_string(),
    ];

    let features = analyzer
        .extract_features(&documents)
        .expect("operation should succeed");

    // Should have correct shape: (n_documents, 6)
    assert_eq!(features.nrows(), 3);
    assert_eq!(features.ncols(), 6);

    // All features should be finite
    for &val in features.iter() {
        assert!(val.is_finite());
    }
}

#[test]
fn test_aspect_sentiment_extract_features_empty() {
    let analyzer = text::AspectBasedSentimentAnalyzer::new();
    let documents: Vec<String> = vec![];

    let result = analyzer.extract_features(&documents);
    assert!(result.is_err());
}

#[test]
fn test_aspect_sentiment_aggregate() {
    let analyzer = text::AspectBasedSentimentAnalyzer::new()
        .add_aspects(vec!["food".to_string(), "service".to_string()]);

    let documents = vec![
        "The food was great!".to_string(),
        "The food was okay.".to_string(),
        "The service was terrible.".to_string(),
    ];

    let aggregated = analyzer.aggregate_aspects(&documents);

    // Should have food and service aspects
    assert!(aggregated.contains_key("food") || aggregated.contains_key("service"));

    // Food should appear twice
    if let Some(food_sentiments) = aggregated.get("food") {
        assert!(!food_sentiments.is_empty());
    }
}

#[test]
fn test_aspect_sentiment_summary() {
    let analyzer = text::AspectBasedSentimentAnalyzer::new()
        .add_aspects(vec!["food".to_string(), "service".to_string()]);

    let documents = vec![
        "The food was great!".to_string(),
        "The food was excellent!".to_string(),
        "The service was okay.".to_string(),
    ];

    let summary = analyzer.aspect_summary(&documents);

    // Should have summary statistics
    assert!(!summary.is_empty());

    // Should be sorted by frequency
    if summary.len() > 1 {
        assert!(summary[0].2 >= summary[1].2);
    }
}

#[test]
fn test_aspect_sentiment_empty_text() {
    let analyzer = text::AspectBasedSentimentAnalyzer::new().add_aspects(vec!["food".to_string()]);

    let result = analyzer.analyze("");

    assert!(result.is_empty());
}

#[test]
fn test_aspect_sentiment_no_aspects_found() {
    let analyzer = text::AspectBasedSentimentAnalyzer::new()
        .add_aspects(vec!["food".to_string()])
        .auto_extract_aspects(false);

    let result = analyzer.analyze("This is a great day!");

    // No predefined aspects found
    assert!(result.is_empty());
}

#[test]
fn test_aspect_sentiment_case_sensitivity() {
    let analyzer_sensitive = text::AspectBasedSentimentAnalyzer::new()
        .add_aspects(vec!["Food".to_string()])
        .case_sensitive(true);

    let analyzer_insensitive = text::AspectBasedSentimentAnalyzer::new()
        .add_aspects(vec!["food".to_string()])
        .case_sensitive(false);

    let text = "The food was great!";

    let _result_sensitive = analyzer_sensitive.analyze(text);
    let result_insensitive = analyzer_insensitive.analyze(text);

    // Insensitive should find the aspect
    assert!(!result_insensitive.is_empty());
}

#[test]
fn test_aspect_sentiment_multiple_occurrences() {
    let analyzer =
        text::AspectBasedSentimentAnalyzer::new().add_aspects(vec!["product".to_string()]);

    let result = analyzer.analyze("The product is great. Another product is terrible.");

    // Should find both occurrences
    assert!(!result.is_empty());
}