use Risan\Sentiment\Sentiment;
final class Sentiment is the shortest way to score a text. It keeps one default Analyzer per language, built lazily the first time you ask for it and reused afterwards.
The class has a private constructor and cannot be instantiated. For custom words or thresholds, use Analyzer directly.
Methods
analyze()
public static function analyze(
string $text,
Language|string $language = Language::English,
): Result
Scores a text with the default analyzer of the given language.
Parameters
| Name | Type | Description |
|---|---|---|
$text |
string |
The text to analyze. Any length, including empty. |
$language |
Language|string |
A Language case, or its code: 'en' or 'id'. Defaults to English. |
Returns a Result.
Throws
| Exception | When |
|---|---|
ValueError |
$language is a string that is not a known code. |
Invalid UTF-8 input never throws: invalid bytes are dropped and the rest of the text is scored. Empty or whitespace-only text returns all zeros and a neutral label.
Example
use Risan\Sentiment\Language;
use Risan\Sentiment\Sentiment;
$result = Sentiment::analyze('This package is awesome!');
$result->label; // Label::Positive
$result = Sentiment::analyze('Filmnya bagus banget!', Language::Indonesian);
$result = Sentiment::analyze('Filmnya bagus banget!', 'id'); // same thing
Sentiment::analyze('Hello', 'xx'); // throws ValueError