Class SentimentAnalysis
- Namespace
- LMKit.TextAnalysis
- Assembly
- LM-Kit.NET.dll
Provides functionality for performing sentiment analysis on plain text, designed to assess and categorize emotional tone.
public sealed class SentimentAnalysis
- Inheritance
-
SentimentAnalysis
- Inherited Members
Examples
Example: Basic sentiment analysis
using LMKit.TextAnalysis;
using LMKit.Model;
using System;
// Initialize the language model (LM) using the specified model path
LM languageModel = LM.LoadFromModelID("lmkit-tasks:4b-preview");
// Create an instance of SentimentAnalysis
SentimentAnalysis sentimentAnalyzer = new SentimentAnalysis(languageModel);
// Analyze text sentiment
string text = "I absolutely love this product! It exceeded my expectations.";
SentimentCategory sentiment = sentimentAnalyzer.GetSentimentCategory(text);
Console.WriteLine($"Sentiment: {sentiment}");
// Output: "Sentiment: Positive"
Example: Batch analysis with confidence filtering
using LMKit.TextAnalysis;
using LMKit.Model;
LM model = LM.LoadFromModelID("lmkit-tasks:4b-preview");
SentimentAnalysis analyzer = new SentimentAnalysis(model);
string[] reviews = { "Great product!", "Terrible experience.", "It was okay." };
float confidenceThreshold = 0.8f;
foreach (var review in reviews)
{
var sentiment = analyzer.GetSentimentCategory(review);
if (analyzer.Confidence >= confidenceThreshold)
Console.WriteLine($"[{sentiment}] {review}");
else
Console.WriteLine($"[UNCERTAIN] {review}");
}
Example: Including neutral sentiment
using LMKit.TextAnalysis;
using LMKit.Model;
LM model = LM.LoadFromModelID("lmkit-tasks:4b-preview");
SentimentAnalysis analyzer = new SentimentAnalysis(model);
// Enable neutral category
analyzer.NeutralSupport = true;
// Now factual statements can be classified as neutral
var sentiment = analyzer.GetSentimentCategory("The meeting is at 3 PM.");
Console.WriteLine($"Sentiment: {sentiment}"); // Output: Neutral
Remarks
The SentimentAnalysis class uses a language model to analyze textual content and determine its sentiment category. It supports both synchronous and asynchronous operations and allows for fine-tuning with custom training data.
Key Features
- Classify text as positive, negative, or optionally neutral
- Get confidence scores to evaluate classification reliability
- Support for embedding-based classification for improved performance
- Built-in training datasets for model fine-tuning
- Multilingual support depending on the underlying model
Sentiment Categories
| Category | Meaning |
|---|---|
| Positive | Indicates approval, satisfaction, enthusiasm, or favorable opinion |
| Negative | Indicates criticism, dissatisfaction, frustration, or unfavorable opinion |
| Neutral | Indicates factual, objective content with no strong emotional tone (requires NeutralSupport = true) |
Classification Modes
By default, the analyzer uses completion-based classification. For certain models and high-volume
scenarios, embedding-based classification (UseEmbeddingClassifier) can provide
better performance.
Constructors
- SentimentAnalysis(LM)
Initializes a new instance of the SentimentAnalysis class.
Properties
- Confidence
Gets the confidence score of the last sentiment analysis process, ranging from 0 to 1. A score closer to 1 indicates higher confidence in the categorization accuracy.
- Model
Gets the LM instance associated with this SentimentAnalysis object.
- NeutralSupport
Gets or sets a value indicating whether the neutral category support is enabled.
- UseEmbeddingClassifier
Gets or sets a value indicating whether the classifier should utilize the embeddings strategy instead of completion.
Methods
- GetSentimentCategory(string, CancellationToken)
Analyzes the sentiment of the specified text and classifies it into a category defined in the SentimentAnalysis.SentimentCategory enumeration.
- GetSentimentCategoryAsync(string, CancellationToken)
Asynchronously analyzes the sentiment of the specified text and classifies it into a category defined in the SentimentAnalysis.SentimentCategory enumeration.
- GetTrainingData(TrainingDataset, int, bool, int?, bool)
Retrieves training data for fine-tuning a sentiment analysis model from the specified dataset.