Skip to content

Examples

Real-world sentiment scenarios. Every example is runnable Rust; copy any block into a binary, or wire the same calls into the BeLikeNative writing assistant for a live in-editor tone readout.

1. Score a sentence

Get the polarity, normalized score, and label in one call.

use sentiment_basic::analyze;

let r = analyze("This is wonderful and clean!");
println!("polarity: {}", r.polarity);
println!("score:    {:.2}", r.score);
println!("label:    {:?}", r.label);

2. Branch on the label

Route feedback by tone — different UI for praise vs. complaints.

use sentiment_basic::{analyze, Sentiment};

match analyze("The service was terrible and slow.").label {
    Sentiment::Positive => println!("thanks!"),
    Sentiment::Negative => println!("flag for follow-up"),
    Sentiment::Neutral  => println!("no strong signal"),
}

3. Compare two drafts

Pick the warmer phrasing by comparing normalized scores.

use sentiment_basic::analyze;

let drafts = [
    "The report is acceptable.",
    "The report is excellent and insightful!",
];

let warmest = drafts
    .iter()
    .max_by(|a, b| {
        analyze(a).score.partial_cmp(&analyze(b).score).unwrap()
    })
    .unwrap();
println!("warmest: {warmest}");

4. Empty or neutral input

No sentiment words means a 0.0 score and the Neutral label — never a panic, never a divide-by-zero.

use sentiment_basic::{analyze, Sentiment};

let r = analyze("the the the");
assert_eq!(r.score, 0.0);
assert_eq!(r.label, Sentiment::Neutral);

5. Inspect the lexicons

Audit which words drive a score, or check membership before analysis.

use sentiment_basic::{positive_words, negative_words};

println!("{} positive, {} negative words built in",
         positive_words().len(), negative_words().len());
assert!(positive_words().contains(&"happy"));
assert!(negative_words().contains(&"awful"));

Next steps

For a live tone readout that flags harsh or lukewarm phrasing as you type, with rewrite suggestions for negative passages, use the BeLikeNative AI writing assistant.