MLSentimentAnalyzer

Struct MLSentimentAnalyzer 

Source
pub struct MLSentimentAnalyzer { /* private fields */ }
Expand description

ML-based sentiment analyzer

Implementations§

Source§

impl MLSentimentAnalyzer

Source

pub fn new() -> Self

Create a new ML sentiment analyzer

Examples found in repository?
examples/ml_sentiment_demo.rs (line 33)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    println!("ML-based Sentiment Analysis Demo");
8    println!("================================\n");
9
10    // Create a sample dataset
11    let (train_dataset, test_dataset) = create_sentiment_dataset()?;
12
13    println!("1. Dataset Information");
14    println!("--------------------");
15    println!("Training examples: {}", train_dataset.texts.len());
16    println!("Test examples: {}", test_dataset.texts.len());
17
18    let labels_train: std::collections::HashSet<_> = train_dataset.labels.iter().cloned().collect();
19    println!("Labels: {labels_train:?}\n");
20
21    // Configure and train ML sentiment analyzer
22    println!("2. Training ML Sentiment Analyzer");
23    println!("------------------------------");
24
25    let config = MLSentimentConfig {
26        learning_rate: 0.05,
27        epochs: 200,
28        regularization: 0.01,
29        batch_size: 32,
30        random_seed: Some(42),
31    };
32
33    let mut analyzer = MLSentimentAnalyzer::new().with_config(config);
34
35    println!("Training...");
36    let training_metrics = analyzer.train(&train_dataset)?;
37
38    println!("Training complete!");
39    println!("Final accuracy: {:.4}", training_metrics.accuracy);
40    println!(
41        "Final loss: {:.4}",
42        training_metrics.loss_history.last().unwrap()
43    );
44
45    // Plot loss history
46    println!("\nLoss history (first 10 epochs):");
47    print!("  ");
48    for i in 0..10 {
49        print!("{:.2} ", training_metrics.loss_history[i]);
50    }
51    println!("...");
52
53    // Evaluate on test data
54    println!("\n3. Evaluation");
55    println!("-----------");
56
57    let eval_metrics = analyzer.evaluate(&test_dataset)?;
58
59    println!("Accuracy: {:.4}", eval_metrics.accuracy);
60    println!("Precision: {:.4}", eval_metrics.precision);
61    println!("Recall: {:.4}", eval_metrics.recall);
62    println!("F1 Score: {:.4}", eval_metrics.f1_score);
63
64    println!("\nClass metrics:");
65    for (label, metrics) in &eval_metrics.class_metrics {
66        println!(
67            "  {}: Precision={:.4}, Recall={:.4}, F1={:.4}",
68            label, metrics.precision, metrics.recall, metrics.f1_score
69        );
70    }
71
72    // Display confusion matrix
73    println!("\nConfusion Matrix:");
74    for i in 0..eval_metrics.confusion_matrix.nrows() {
75        print!("  ");
76        for j in 0..eval_metrics.confusion_matrix.ncols() {
77            print!("{:4} ", eval_metrics.confusion_matrix[[i, j]]);
78        }
79        println!();
80    }
81
82    // Use the model for predictions
83    println!("\n4. Sentiment Predictions");
84    println!("----------------------");
85
86    let testtexts = vec![
87        "This product is amazing! I absolutely love it and would recommend it to everyone.",
88        "Terrible experience. The customer service was awful and the product doesn't work.",
89        "It's okay. Not great, not terrible, just average.",
90        "Good value for money, but there are some issues with the packaging.",
91        "Worst purchase ever. Complete waste of money.",
92    ];
93
94    println!("Sample text predictions:");
95    for text in testtexts {
96        let result = analyzer.predict(text)?;
97        println!(
98            "\"{}...\"\n  → {} (Score: {:.2}, Confidence: {:.2})\n",
99            text.chars().take(40).collect::<String>(),
100            result.sentiment,
101            result.score,
102            result.confidence
103        );
104    }
105
106    // Batch prediction
107    println!("5. Batch Prediction");
108    println!("----------------");
109
110    let batchtexts = vec![
111        "Excellent quality product",
112        "Poor performance for the price",
113        "Somewhat satisfied with purchase",
114    ];
115
116    let batch_results = analyzer.predict_batch(&batchtexts)?;
117
118    for (i, result) in batch_results.iter().enumerate() {
119        println!(
120            "Text {}: {} (Confidence: {:.2})",
121            i + 1,
122            result.sentiment,
123            result.confidence
124        );
125    }
126
127    // Compare with different configurations
128    println!("\n6. Hyperparameter Comparison");
129    println!("--------------------------");
130
131    let configs = vec![
132        (0.01, 100, "Low learning rate"),
133        (0.1, 100, "High learning rate"),
134        (0.05, 50, "Medium rate, fewer epochs"),
135        (0.05, 200, "Medium rate, more epochs"),
136    ];
137
138    for (lr, epochs, desc) in configs {
139        let config = MLSentimentConfig {
140            learning_rate: lr,
141            epochs,
142            regularization: 0.01,
143            batch_size: 32,
144            random_seed: Some(42),
145        };
146
147        let mut temp_analyzer = MLSentimentAnalyzer::new().with_config(config);
148        let _metrics = temp_analyzer.train(&train_dataset)?;
149        let eval = temp_analyzer.evaluate(&test_dataset)?;
150
151        println!(
152            "{}: Accuracy={:.4}, F1={:.4}",
153            desc, eval.accuracy, eval.f1_score
154        );
155    }
156
157    Ok(())
158}
Source

pub fn with_config(self, config: MLSentimentConfig) -> Self

Configure the analyzer

Examples found in repository?
examples/ml_sentiment_demo.rs (line 33)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    println!("ML-based Sentiment Analysis Demo");
8    println!("================================\n");
9
10    // Create a sample dataset
11    let (train_dataset, test_dataset) = create_sentiment_dataset()?;
12
13    println!("1. Dataset Information");
14    println!("--------------------");
15    println!("Training examples: {}", train_dataset.texts.len());
16    println!("Test examples: {}", test_dataset.texts.len());
17
18    let labels_train: std::collections::HashSet<_> = train_dataset.labels.iter().cloned().collect();
19    println!("Labels: {labels_train:?}\n");
20
21    // Configure and train ML sentiment analyzer
22    println!("2. Training ML Sentiment Analyzer");
23    println!("------------------------------");
24
25    let config = MLSentimentConfig {
26        learning_rate: 0.05,
27        epochs: 200,
28        regularization: 0.01,
29        batch_size: 32,
30        random_seed: Some(42),
31    };
32
33    let mut analyzer = MLSentimentAnalyzer::new().with_config(config);
34
35    println!("Training...");
36    let training_metrics = analyzer.train(&train_dataset)?;
37
38    println!("Training complete!");
39    println!("Final accuracy: {:.4}", training_metrics.accuracy);
40    println!(
41        "Final loss: {:.4}",
42        training_metrics.loss_history.last().unwrap()
43    );
44
45    // Plot loss history
46    println!("\nLoss history (first 10 epochs):");
47    print!("  ");
48    for i in 0..10 {
49        print!("{:.2} ", training_metrics.loss_history[i]);
50    }
51    println!("...");
52
53    // Evaluate on test data
54    println!("\n3. Evaluation");
55    println!("-----------");
56
57    let eval_metrics = analyzer.evaluate(&test_dataset)?;
58
59    println!("Accuracy: {:.4}", eval_metrics.accuracy);
60    println!("Precision: {:.4}", eval_metrics.precision);
61    println!("Recall: {:.4}", eval_metrics.recall);
62    println!("F1 Score: {:.4}", eval_metrics.f1_score);
63
64    println!("\nClass metrics:");
65    for (label, metrics) in &eval_metrics.class_metrics {
66        println!(
67            "  {}: Precision={:.4}, Recall={:.4}, F1={:.4}",
68            label, metrics.precision, metrics.recall, metrics.f1_score
69        );
70    }
71
72    // Display confusion matrix
73    println!("\nConfusion Matrix:");
74    for i in 0..eval_metrics.confusion_matrix.nrows() {
75        print!("  ");
76        for j in 0..eval_metrics.confusion_matrix.ncols() {
77            print!("{:4} ", eval_metrics.confusion_matrix[[i, j]]);
78        }
79        println!();
80    }
81
82    // Use the model for predictions
83    println!("\n4. Sentiment Predictions");
84    println!("----------------------");
85
86    let testtexts = vec![
87        "This product is amazing! I absolutely love it and would recommend it to everyone.",
88        "Terrible experience. The customer service was awful and the product doesn't work.",
89        "It's okay. Not great, not terrible, just average.",
90        "Good value for money, but there are some issues with the packaging.",
91        "Worst purchase ever. Complete waste of money.",
92    ];
93
94    println!("Sample text predictions:");
95    for text in testtexts {
96        let result = analyzer.predict(text)?;
97        println!(
98            "\"{}...\"\n  → {} (Score: {:.2}, Confidence: {:.2})\n",
99            text.chars().take(40).collect::<String>(),
100            result.sentiment,
101            result.score,
102            result.confidence
103        );
104    }
105
106    // Batch prediction
107    println!("5. Batch Prediction");
108    println!("----------------");
109
110    let batchtexts = vec![
111        "Excellent quality product",
112        "Poor performance for the price",
113        "Somewhat satisfied with purchase",
114    ];
115
116    let batch_results = analyzer.predict_batch(&batchtexts)?;
117
118    for (i, result) in batch_results.iter().enumerate() {
119        println!(
120            "Text {}: {} (Confidence: {:.2})",
121            i + 1,
122            result.sentiment,
123            result.confidence
124        );
125    }
126
127    // Compare with different configurations
128    println!("\n6. Hyperparameter Comparison");
129    println!("--------------------------");
130
131    let configs = vec![
132        (0.01, 100, "Low learning rate"),
133        (0.1, 100, "High learning rate"),
134        (0.05, 50, "Medium rate, fewer epochs"),
135        (0.05, 200, "Medium rate, more epochs"),
136    ];
137
138    for (lr, epochs, desc) in configs {
139        let config = MLSentimentConfig {
140            learning_rate: lr,
141            epochs,
142            regularization: 0.01,
143            batch_size: 32,
144            random_seed: Some(42),
145        };
146
147        let mut temp_analyzer = MLSentimentAnalyzer::new().with_config(config);
148        let _metrics = temp_analyzer.train(&train_dataset)?;
149        let eval = temp_analyzer.evaluate(&test_dataset)?;
150
151        println!(
152            "{}: Accuracy={:.4}, F1={:.4}",
153            desc, eval.accuracy, eval.f1_score
154        );
155    }
156
157    Ok(())
158}
Source

pub fn train(&mut self, dataset: &TextDataset) -> Result<TrainingMetrics>

Train the sentiment analyzer

Examples found in repository?
examples/ml_sentiment_demo.rs (line 36)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    println!("ML-based Sentiment Analysis Demo");
8    println!("================================\n");
9
10    // Create a sample dataset
11    let (train_dataset, test_dataset) = create_sentiment_dataset()?;
12
13    println!("1. Dataset Information");
14    println!("--------------------");
15    println!("Training examples: {}", train_dataset.texts.len());
16    println!("Test examples: {}", test_dataset.texts.len());
17
18    let labels_train: std::collections::HashSet<_> = train_dataset.labels.iter().cloned().collect();
19    println!("Labels: {labels_train:?}\n");
20
21    // Configure and train ML sentiment analyzer
22    println!("2. Training ML Sentiment Analyzer");
23    println!("------------------------------");
24
25    let config = MLSentimentConfig {
26        learning_rate: 0.05,
27        epochs: 200,
28        regularization: 0.01,
29        batch_size: 32,
30        random_seed: Some(42),
31    };
32
33    let mut analyzer = MLSentimentAnalyzer::new().with_config(config);
34
35    println!("Training...");
36    let training_metrics = analyzer.train(&train_dataset)?;
37
38    println!("Training complete!");
39    println!("Final accuracy: {:.4}", training_metrics.accuracy);
40    println!(
41        "Final loss: {:.4}",
42        training_metrics.loss_history.last().unwrap()
43    );
44
45    // Plot loss history
46    println!("\nLoss history (first 10 epochs):");
47    print!("  ");
48    for i in 0..10 {
49        print!("{:.2} ", training_metrics.loss_history[i]);
50    }
51    println!("...");
52
53    // Evaluate on test data
54    println!("\n3. Evaluation");
55    println!("-----------");
56
57    let eval_metrics = analyzer.evaluate(&test_dataset)?;
58
59    println!("Accuracy: {:.4}", eval_metrics.accuracy);
60    println!("Precision: {:.4}", eval_metrics.precision);
61    println!("Recall: {:.4}", eval_metrics.recall);
62    println!("F1 Score: {:.4}", eval_metrics.f1_score);
63
64    println!("\nClass metrics:");
65    for (label, metrics) in &eval_metrics.class_metrics {
66        println!(
67            "  {}: Precision={:.4}, Recall={:.4}, F1={:.4}",
68            label, metrics.precision, metrics.recall, metrics.f1_score
69        );
70    }
71
72    // Display confusion matrix
73    println!("\nConfusion Matrix:");
74    for i in 0..eval_metrics.confusion_matrix.nrows() {
75        print!("  ");
76        for j in 0..eval_metrics.confusion_matrix.ncols() {
77            print!("{:4} ", eval_metrics.confusion_matrix[[i, j]]);
78        }
79        println!();
80    }
81
82    // Use the model for predictions
83    println!("\n4. Sentiment Predictions");
84    println!("----------------------");
85
86    let testtexts = vec![
87        "This product is amazing! I absolutely love it and would recommend it to everyone.",
88        "Terrible experience. The customer service was awful and the product doesn't work.",
89        "It's okay. Not great, not terrible, just average.",
90        "Good value for money, but there are some issues with the packaging.",
91        "Worst purchase ever. Complete waste of money.",
92    ];
93
94    println!("Sample text predictions:");
95    for text in testtexts {
96        let result = analyzer.predict(text)?;
97        println!(
98            "\"{}...\"\n  → {} (Score: {:.2}, Confidence: {:.2})\n",
99            text.chars().take(40).collect::<String>(),
100            result.sentiment,
101            result.score,
102            result.confidence
103        );
104    }
105
106    // Batch prediction
107    println!("5. Batch Prediction");
108    println!("----------------");
109
110    let batchtexts = vec![
111        "Excellent quality product",
112        "Poor performance for the price",
113        "Somewhat satisfied with purchase",
114    ];
115
116    let batch_results = analyzer.predict_batch(&batchtexts)?;
117
118    for (i, result) in batch_results.iter().enumerate() {
119        println!(
120            "Text {}: {} (Confidence: {:.2})",
121            i + 1,
122            result.sentiment,
123            result.confidence
124        );
125    }
126
127    // Compare with different configurations
128    println!("\n6. Hyperparameter Comparison");
129    println!("--------------------------");
130
131    let configs = vec![
132        (0.01, 100, "Low learning rate"),
133        (0.1, 100, "High learning rate"),
134        (0.05, 50, "Medium rate, fewer epochs"),
135        (0.05, 200, "Medium rate, more epochs"),
136    ];
137
138    for (lr, epochs, desc) in configs {
139        let config = MLSentimentConfig {
140            learning_rate: lr,
141            epochs,
142            regularization: 0.01,
143            batch_size: 32,
144            random_seed: Some(42),
145        };
146
147        let mut temp_analyzer = MLSentimentAnalyzer::new().with_config(config);
148        let _metrics = temp_analyzer.train(&train_dataset)?;
149        let eval = temp_analyzer.evaluate(&test_dataset)?;
150
151        println!(
152            "{}: Accuracy={:.4}, F1={:.4}",
153            desc, eval.accuracy, eval.f1_score
154        );
155    }
156
157    Ok(())
158}
Source

pub fn predict(&self, text: &str) -> Result<SentimentResult>

Predict sentiment for a single text

Examples found in repository?
examples/ml_sentiment_demo.rs (line 96)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    println!("ML-based Sentiment Analysis Demo");
8    println!("================================\n");
9
10    // Create a sample dataset
11    let (train_dataset, test_dataset) = create_sentiment_dataset()?;
12
13    println!("1. Dataset Information");
14    println!("--------------------");
15    println!("Training examples: {}", train_dataset.texts.len());
16    println!("Test examples: {}", test_dataset.texts.len());
17
18    let labels_train: std::collections::HashSet<_> = train_dataset.labels.iter().cloned().collect();
19    println!("Labels: {labels_train:?}\n");
20
21    // Configure and train ML sentiment analyzer
22    println!("2. Training ML Sentiment Analyzer");
23    println!("------------------------------");
24
25    let config = MLSentimentConfig {
26        learning_rate: 0.05,
27        epochs: 200,
28        regularization: 0.01,
29        batch_size: 32,
30        random_seed: Some(42),
31    };
32
33    let mut analyzer = MLSentimentAnalyzer::new().with_config(config);
34
35    println!("Training...");
36    let training_metrics = analyzer.train(&train_dataset)?;
37
38    println!("Training complete!");
39    println!("Final accuracy: {:.4}", training_metrics.accuracy);
40    println!(
41        "Final loss: {:.4}",
42        training_metrics.loss_history.last().unwrap()
43    );
44
45    // Plot loss history
46    println!("\nLoss history (first 10 epochs):");
47    print!("  ");
48    for i in 0..10 {
49        print!("{:.2} ", training_metrics.loss_history[i]);
50    }
51    println!("...");
52
53    // Evaluate on test data
54    println!("\n3. Evaluation");
55    println!("-----------");
56
57    let eval_metrics = analyzer.evaluate(&test_dataset)?;
58
59    println!("Accuracy: {:.4}", eval_metrics.accuracy);
60    println!("Precision: {:.4}", eval_metrics.precision);
61    println!("Recall: {:.4}", eval_metrics.recall);
62    println!("F1 Score: {:.4}", eval_metrics.f1_score);
63
64    println!("\nClass metrics:");
65    for (label, metrics) in &eval_metrics.class_metrics {
66        println!(
67            "  {}: Precision={:.4}, Recall={:.4}, F1={:.4}",
68            label, metrics.precision, metrics.recall, metrics.f1_score
69        );
70    }
71
72    // Display confusion matrix
73    println!("\nConfusion Matrix:");
74    for i in 0..eval_metrics.confusion_matrix.nrows() {
75        print!("  ");
76        for j in 0..eval_metrics.confusion_matrix.ncols() {
77            print!("{:4} ", eval_metrics.confusion_matrix[[i, j]]);
78        }
79        println!();
80    }
81
82    // Use the model for predictions
83    println!("\n4. Sentiment Predictions");
84    println!("----------------------");
85
86    let testtexts = vec![
87        "This product is amazing! I absolutely love it and would recommend it to everyone.",
88        "Terrible experience. The customer service was awful and the product doesn't work.",
89        "It's okay. Not great, not terrible, just average.",
90        "Good value for money, but there are some issues with the packaging.",
91        "Worst purchase ever. Complete waste of money.",
92    ];
93
94    println!("Sample text predictions:");
95    for text in testtexts {
96        let result = analyzer.predict(text)?;
97        println!(
98            "\"{}...\"\n  → {} (Score: {:.2}, Confidence: {:.2})\n",
99            text.chars().take(40).collect::<String>(),
100            result.sentiment,
101            result.score,
102            result.confidence
103        );
104    }
105
106    // Batch prediction
107    println!("5. Batch Prediction");
108    println!("----------------");
109
110    let batchtexts = vec![
111        "Excellent quality product",
112        "Poor performance for the price",
113        "Somewhat satisfied with purchase",
114    ];
115
116    let batch_results = analyzer.predict_batch(&batchtexts)?;
117
118    for (i, result) in batch_results.iter().enumerate() {
119        println!(
120            "Text {}: {} (Confidence: {:.2})",
121            i + 1,
122            result.sentiment,
123            result.confidence
124        );
125    }
126
127    // Compare with different configurations
128    println!("\n6. Hyperparameter Comparison");
129    println!("--------------------------");
130
131    let configs = vec![
132        (0.01, 100, "Low learning rate"),
133        (0.1, 100, "High learning rate"),
134        (0.05, 50, "Medium rate, fewer epochs"),
135        (0.05, 200, "Medium rate, more epochs"),
136    ];
137
138    for (lr, epochs, desc) in configs {
139        let config = MLSentimentConfig {
140            learning_rate: lr,
141            epochs,
142            regularization: 0.01,
143            batch_size: 32,
144            random_seed: Some(42),
145        };
146
147        let mut temp_analyzer = MLSentimentAnalyzer::new().with_config(config);
148        let _metrics = temp_analyzer.train(&train_dataset)?;
149        let eval = temp_analyzer.evaluate(&test_dataset)?;
150
151        println!(
152            "{}: Accuracy={:.4}, F1={:.4}",
153            desc, eval.accuracy, eval.f1_score
154        );
155    }
156
157    Ok(())
158}
Source

pub fn predict_batch(&self, texts: &[&str]) -> Result<Vec<SentimentResult>>

Batch predict sentiment

Examples found in repository?
examples/ml_sentiment_demo.rs (line 116)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    println!("ML-based Sentiment Analysis Demo");
8    println!("================================\n");
9
10    // Create a sample dataset
11    let (train_dataset, test_dataset) = create_sentiment_dataset()?;
12
13    println!("1. Dataset Information");
14    println!("--------------------");
15    println!("Training examples: {}", train_dataset.texts.len());
16    println!("Test examples: {}", test_dataset.texts.len());
17
18    let labels_train: std::collections::HashSet<_> = train_dataset.labels.iter().cloned().collect();
19    println!("Labels: {labels_train:?}\n");
20
21    // Configure and train ML sentiment analyzer
22    println!("2. Training ML Sentiment Analyzer");
23    println!("------------------------------");
24
25    let config = MLSentimentConfig {
26        learning_rate: 0.05,
27        epochs: 200,
28        regularization: 0.01,
29        batch_size: 32,
30        random_seed: Some(42),
31    };
32
33    let mut analyzer = MLSentimentAnalyzer::new().with_config(config);
34
35    println!("Training...");
36    let training_metrics = analyzer.train(&train_dataset)?;
37
38    println!("Training complete!");
39    println!("Final accuracy: {:.4}", training_metrics.accuracy);
40    println!(
41        "Final loss: {:.4}",
42        training_metrics.loss_history.last().unwrap()
43    );
44
45    // Plot loss history
46    println!("\nLoss history (first 10 epochs):");
47    print!("  ");
48    for i in 0..10 {
49        print!("{:.2} ", training_metrics.loss_history[i]);
50    }
51    println!("...");
52
53    // Evaluate on test data
54    println!("\n3. Evaluation");
55    println!("-----------");
56
57    let eval_metrics = analyzer.evaluate(&test_dataset)?;
58
59    println!("Accuracy: {:.4}", eval_metrics.accuracy);
60    println!("Precision: {:.4}", eval_metrics.precision);
61    println!("Recall: {:.4}", eval_metrics.recall);
62    println!("F1 Score: {:.4}", eval_metrics.f1_score);
63
64    println!("\nClass metrics:");
65    for (label, metrics) in &eval_metrics.class_metrics {
66        println!(
67            "  {}: Precision={:.4}, Recall={:.4}, F1={:.4}",
68            label, metrics.precision, metrics.recall, metrics.f1_score
69        );
70    }
71
72    // Display confusion matrix
73    println!("\nConfusion Matrix:");
74    for i in 0..eval_metrics.confusion_matrix.nrows() {
75        print!("  ");
76        for j in 0..eval_metrics.confusion_matrix.ncols() {
77            print!("{:4} ", eval_metrics.confusion_matrix[[i, j]]);
78        }
79        println!();
80    }
81
82    // Use the model for predictions
83    println!("\n4. Sentiment Predictions");
84    println!("----------------------");
85
86    let testtexts = vec![
87        "This product is amazing! I absolutely love it and would recommend it to everyone.",
88        "Terrible experience. The customer service was awful and the product doesn't work.",
89        "It's okay. Not great, not terrible, just average.",
90        "Good value for money, but there are some issues with the packaging.",
91        "Worst purchase ever. Complete waste of money.",
92    ];
93
94    println!("Sample text predictions:");
95    for text in testtexts {
96        let result = analyzer.predict(text)?;
97        println!(
98            "\"{}...\"\n  → {} (Score: {:.2}, Confidence: {:.2})\n",
99            text.chars().take(40).collect::<String>(),
100            result.sentiment,
101            result.score,
102            result.confidence
103        );
104    }
105
106    // Batch prediction
107    println!("5. Batch Prediction");
108    println!("----------------");
109
110    let batchtexts = vec![
111        "Excellent quality product",
112        "Poor performance for the price",
113        "Somewhat satisfied with purchase",
114    ];
115
116    let batch_results = analyzer.predict_batch(&batchtexts)?;
117
118    for (i, result) in batch_results.iter().enumerate() {
119        println!(
120            "Text {}: {} (Confidence: {:.2})",
121            i + 1,
122            result.sentiment,
123            result.confidence
124        );
125    }
126
127    // Compare with different configurations
128    println!("\n6. Hyperparameter Comparison");
129    println!("--------------------------");
130
131    let configs = vec![
132        (0.01, 100, "Low learning rate"),
133        (0.1, 100, "High learning rate"),
134        (0.05, 50, "Medium rate, fewer epochs"),
135        (0.05, 200, "Medium rate, more epochs"),
136    ];
137
138    for (lr, epochs, desc) in configs {
139        let config = MLSentimentConfig {
140            learning_rate: lr,
141            epochs,
142            regularization: 0.01,
143            batch_size: 32,
144            random_seed: Some(42),
145        };
146
147        let mut temp_analyzer = MLSentimentAnalyzer::new().with_config(config);
148        let _metrics = temp_analyzer.train(&train_dataset)?;
149        let eval = temp_analyzer.evaluate(&test_dataset)?;
150
151        println!(
152            "{}: Accuracy={:.4}, F1={:.4}",
153            desc, eval.accuracy, eval.f1_score
154        );
155    }
156
157    Ok(())
158}
Source

pub fn evaluate(&self, testdataset: &TextDataset) -> Result<EvaluationMetrics>

Evaluate on test dataset

Examples found in repository?
examples/ml_sentiment_demo.rs (line 57)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    println!("ML-based Sentiment Analysis Demo");
8    println!("================================\n");
9
10    // Create a sample dataset
11    let (train_dataset, test_dataset) = create_sentiment_dataset()?;
12
13    println!("1. Dataset Information");
14    println!("--------------------");
15    println!("Training examples: {}", train_dataset.texts.len());
16    println!("Test examples: {}", test_dataset.texts.len());
17
18    let labels_train: std::collections::HashSet<_> = train_dataset.labels.iter().cloned().collect();
19    println!("Labels: {labels_train:?}\n");
20
21    // Configure and train ML sentiment analyzer
22    println!("2. Training ML Sentiment Analyzer");
23    println!("------------------------------");
24
25    let config = MLSentimentConfig {
26        learning_rate: 0.05,
27        epochs: 200,
28        regularization: 0.01,
29        batch_size: 32,
30        random_seed: Some(42),
31    };
32
33    let mut analyzer = MLSentimentAnalyzer::new().with_config(config);
34
35    println!("Training...");
36    let training_metrics = analyzer.train(&train_dataset)?;
37
38    println!("Training complete!");
39    println!("Final accuracy: {:.4}", training_metrics.accuracy);
40    println!(
41        "Final loss: {:.4}",
42        training_metrics.loss_history.last().unwrap()
43    );
44
45    // Plot loss history
46    println!("\nLoss history (first 10 epochs):");
47    print!("  ");
48    for i in 0..10 {
49        print!("{:.2} ", training_metrics.loss_history[i]);
50    }
51    println!("...");
52
53    // Evaluate on test data
54    println!("\n3. Evaluation");
55    println!("-----------");
56
57    let eval_metrics = analyzer.evaluate(&test_dataset)?;
58
59    println!("Accuracy: {:.4}", eval_metrics.accuracy);
60    println!("Precision: {:.4}", eval_metrics.precision);
61    println!("Recall: {:.4}", eval_metrics.recall);
62    println!("F1 Score: {:.4}", eval_metrics.f1_score);
63
64    println!("\nClass metrics:");
65    for (label, metrics) in &eval_metrics.class_metrics {
66        println!(
67            "  {}: Precision={:.4}, Recall={:.4}, F1={:.4}",
68            label, metrics.precision, metrics.recall, metrics.f1_score
69        );
70    }
71
72    // Display confusion matrix
73    println!("\nConfusion Matrix:");
74    for i in 0..eval_metrics.confusion_matrix.nrows() {
75        print!("  ");
76        for j in 0..eval_metrics.confusion_matrix.ncols() {
77            print!("{:4} ", eval_metrics.confusion_matrix[[i, j]]);
78        }
79        println!();
80    }
81
82    // Use the model for predictions
83    println!("\n4. Sentiment Predictions");
84    println!("----------------------");
85
86    let testtexts = vec![
87        "This product is amazing! I absolutely love it and would recommend it to everyone.",
88        "Terrible experience. The customer service was awful and the product doesn't work.",
89        "It's okay. Not great, not terrible, just average.",
90        "Good value for money, but there are some issues with the packaging.",
91        "Worst purchase ever. Complete waste of money.",
92    ];
93
94    println!("Sample text predictions:");
95    for text in testtexts {
96        let result = analyzer.predict(text)?;
97        println!(
98            "\"{}...\"\n  → {} (Score: {:.2}, Confidence: {:.2})\n",
99            text.chars().take(40).collect::<String>(),
100            result.sentiment,
101            result.score,
102            result.confidence
103        );
104    }
105
106    // Batch prediction
107    println!("5. Batch Prediction");
108    println!("----------------");
109
110    let batchtexts = vec![
111        "Excellent quality product",
112        "Poor performance for the price",
113        "Somewhat satisfied with purchase",
114    ];
115
116    let batch_results = analyzer.predict_batch(&batchtexts)?;
117
118    for (i, result) in batch_results.iter().enumerate() {
119        println!(
120            "Text {}: {} (Confidence: {:.2})",
121            i + 1,
122            result.sentiment,
123            result.confidence
124        );
125    }
126
127    // Compare with different configurations
128    println!("\n6. Hyperparameter Comparison");
129    println!("--------------------------");
130
131    let configs = vec![
132        (0.01, 100, "Low learning rate"),
133        (0.1, 100, "High learning rate"),
134        (0.05, 50, "Medium rate, fewer epochs"),
135        (0.05, 200, "Medium rate, more epochs"),
136    ];
137
138    for (lr, epochs, desc) in configs {
139        let config = MLSentimentConfig {
140            learning_rate: lr,
141            epochs,
142            regularization: 0.01,
143            batch_size: 32,
144            random_seed: Some(42),
145        };
146
147        let mut temp_analyzer = MLSentimentAnalyzer::new().with_config(config);
148        let _metrics = temp_analyzer.train(&train_dataset)?;
149        let eval = temp_analyzer.evaluate(&test_dataset)?;
150
151        println!(
152            "{}: Accuracy={:.4}, F1={:.4}",
153            desc, eval.accuracy, eval.f1_score
154        );
155    }
156
157    Ok(())
158}

Trait Implementations§

Source§

impl Default for MLSentimentAnalyzer

Source§

fn default() -> MLSentimentAnalyzer

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V