Skip to content

API Reference

sentiment-basic exposes one entry point — analyze — plus the SentimentReport struct and the Sentiment label enum. No traits to implement, no features to enable.

analyze

pub fn analyze(text: &str) -> SentimentReport

Score text for sentiment polarity. Tokenizes on non-letter characters, lowercases each token, looks each up in the built-in positive and negative lexicons, and returns the full SentimentReport. Deterministic and total: never panics.

use sentiment_basic::analyze;

let r = analyze("Happy, joyful, and delightful.");
assert!(r.score > 0.0);
assert_eq!(r.label, sentiment_basic::Sentiment::Positive);

SentimentReport

pub struct SentimentReport {
    pub positive: usize,
    pub negative: usize,
    pub polarity: i64,
    pub score: f64,
    pub label: Sentiment,
}
field meaning
positive count of tokens matched in the positive lexicon
negative count of tokens matched in the negative lexicon
polarity positive as i64 - negative as i64
score polarity / word_count, clamped to -1.0..=1.0 (0.0 if none)
label Positive / Negative / Neutral, derived from score

Lexicon access

The lexicons are exposed as functions so callers can audit or extend them:

/// Returns the positive lexicon as a sorted slice.
pub fn positive_words() -> &'static [&'static str]
/// Returns the negative lexicon as a sorted slice.
pub fn negative_words() -> &'static [&'static str]
use sentiment_basic::positive_words;

assert!(positive_words().contains(&"wonderful"));

Sentiment

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Sentiment {
    Positive,  // score > 0.0
    Negative,  // score < 0.0
    Neutral,   // score == 0.0 (including empty input)
}

The three-way label derived from SentimentReport::score. Derives Debug, Clone, Copy, PartialEq, and Eq so it compares and copies cheaply.