sentiment_analysis_demo/
sentiment_analysis_demo.rs

1//! Sentiment analysis example
2
3use scirs2_text::{LexiconSentimentAnalyzer, RuleBasedSentimentAnalyzer, SentimentLexicon};
4
5#[allow(dead_code)]
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    println!("Sentiment Analysis Demo");
8    println!("======================\n");
9
10    // Create sentiment analyzers
11    let basic_analyzer = LexiconSentimentAnalyzer::with_basiclexicon();
12    let rule_based_analyzer = RuleBasedSentimentAnalyzer::with_basiclexicon();
13
14    // Example texts for analysis
15    let texts = vec![
16        "I absolutely love this product! It's amazing!",
17        "This is terrible, I hate it.",
18        "It's okay, nothing special.",
19        "I'm not happy with this purchase.",
20        "Extremely disappointed with the quality.",
21        "This is very good, I'm really satisfied.",
22        "The worst experience ever!",
23        "Somewhat decent, but could be better.",
24    ];
25
26    println!("Basic Lexicon-based Sentiment Analysis:");
27    println!("======================================");
28
29    for text in &texts {
30        let result = basic_analyzer.analyze(text)?;
31        println!("\nText: \"{text}\"");
32        println!("  Sentiment: {:?}", result.sentiment);
33        println!("  Score: {:.2}", result.score);
34        println!("  Confidence: {:.2}%", result.confidence * 100.0);
35        println!(
36            "  Word counts: +{} -{} ={} (total: {})",
37            result.word_counts.positive_words,
38            result.word_counts.negative_words,
39            result.word_counts.neutral_words,
40            result.word_counts.total_words
41        );
42    }
43
44    println!("\n\nRule-based Sentiment Analysis:");
45    println!("=============================");
46
47    // Examples with intensifiers
48    let intensifiedtexts = vec![
49        "This is good",
50        "This is very good",
51        "This is extremely good",
52        "This is somewhat good",
53        "This is really bad",
54        "This is slightly bad",
55    ];
56
57    for text in &intensifiedtexts {
58        let basic_result = basic_analyzer.analyze(text)?;
59        let rule_result = rule_based_analyzer.analyze(text)?;
60
61        println!("\nText: \"{text}\"");
62        println!("  Basic score: {:.2}", basic_result.score);
63        println!("  Rule-based score: {:.2}", rule_result.score);
64        println!(
65            "  Difference: {:.2}",
66            rule_result.score - basic_result.score
67        );
68    }
69
70    // Batch analysis example
71    println!("\n\nBatch Analysis:");
72    println!("==============");
73
74    let batchtexts = vec![
75        "Great product!",
76        "Terrible service.",
77        "Average quality.",
78        "Highly recommended!",
79        "Would not buy again.",
80    ];
81
82    let batch_results = basic_analyzer.analyze_batch(&batchtexts)?;
83
84    for (text, result) in batchtexts.iter().zip(batch_results.iter()) {
85        println!(
86            "{}: {:?} (score: {:.2})",
87            text, result.sentiment, result.score
88        );
89    }
90
91    // Creating a custom lexicon
92    println!("\n\nCustom Lexicon Example:");
93    println!("======================");
94
95    let mut custom_lexicon = SentimentLexicon::new();
96    custom_lexicon.add_word("awesome".to_string(), 3.0);
97    custom_lexicon.add_word("terrible".to_string(), -3.0);
98    custom_lexicon.add_word("meh".to_string(), -0.5);
99
100    let custom_analyzer = LexiconSentimentAnalyzer::new(custom_lexicon);
101
102    let customtexts = vec![
103        "This is awesome!",
104        "Meh, not impressed",
105        "Terrible experience",
106    ];
107
108    for text in &customtexts {
109        let result = custom_analyzer.analyze(text)?;
110        println!(
111            "\n\"{}\" -> {:?} (score: {:.2})",
112            text, result.sentiment, result.score
113        );
114    }
115
116    Ok(())
117}