sentiment-basic¶
Is this sentence positive, negative, or neutral? Split it into words, look each one up in a small polarity lexicon, sum the hits, and divide by the word count — a fast, transparent sentiment score with no model, no network, and no nondeterminism.
sentiment-basic is a pure-Rust, zero-dependency crate that does exactly
that: a compact built-in word list of positive and negative terms, lowercase
tokenization with basic stemming, and a single analyze call returning a
polarity score, a label, and the counts behind both. It is the tone checker
behind the BeLikeNative AI writing assistant,
packaged as a small library for editors, CLIs, and feedback widgets.
How scoring works¶
- Tokenize — split on non-letter characters, lowercase each token.
- Score each word —
+1if in the positive lexicon,-1if in the negative lexicon,0otherwise. - Sum — total polarity across all tokens.
- Normalize —
score = polarity / word_count, clamped to-1.0..=1.0. - Label —
score > 0→Positive,< 0→Negative,== 0→Neutral.
| field | range | meaning |
|---|---|---|
positive |
usize |
number of positive words matched |
negative |
usize |
number of negative words matched |
polarity |
i64 |
positive - negative |
score |
-1.0 .. 1.0 |
normalized polarity (0.0 if no words) |
label |
enum | Positive / Negative / Neutral |
Install¶
Quick start¶
use sentiment_basic::analyze;
let pos = analyze("Great job, this is wonderful and clean!");
let neg = analyze("This is terrible, awful, and broken.");
println!("pos score: {:.2} ({:?})", pos.score, pos.label); // Positive
println!("neg score: {:.2} ({:?})", neg.score, neg.label); // Negative
assert!(pos.score > 0.0);
assert!(neg.score < 0.0);
See it live
The crate gives you the score. For a live in-editor tone readout that flags harsh or lukewarm phrasing as you type, use the BeLikeNative writing assistant.
Features¶
- Pure Rust, no deps — a single file, no features, no network.
- Built-in lexicon — small curated positive/negative word lists; no data files to ship.
- Deterministic — same input always yields identical output.
- Normalized score —
-1.0..=1.0so results are comparable across inputs. - Three-way label —
Sentimentenum for quick branching. - O(1) per word — hash-set lookups; linear in token count.
- MIT licensed — embed anywhere.
When to use it (and when not)¶
sentiment-basic is a lexicon analyzer, not a machine-learning model. It
shines for short, clearly opinionated text — reviews, status updates,
headlines, and writing-tone feedback. It will not catch sarcasm, negation
scope ("not bad"), or domain-specific idiom. For those you need a model; this
crate is the fast, transparent baseline.
Links¶
- Source crate:
sentiment-basic - Interactive tool: BeLikeNative