Method GetSentimentCategory
- Namespace
- LMKit.TextAnalysis
- Assembly
- LM-Kit.NET.dll
GetSentimentCategory(string, CancellationToken)
Analyzes the sentiment of the specified text and classifies it into a category defined in the SentimentAnalysis.SentimentCategory enumeration.
public SentimentAnalysis.SentimentCategory GetSentimentCategory(string text, CancellationToken cancellationToken = default)
Parameters
text
stringThe text to be analyzed and classified.
cancellationToken
CancellationTokenOptional. A CancellationToken for handling cancellation requests.
Returns
- SentimentAnalysis.SentimentCategory
A member of the SentimentAnalysis.SentimentCategory enumeration that matches the sentiment category of the text.
Examples
using LMKit.TextAnalysis;
using LMKit.Model;
using System;
// Initialize the language model using the specified model path
LLM languageModel = new LLM("https://huggingface.co/lm-kit/lm-kit-sentiment-analysis-2.0-1b-gguf/resolve/main/lm-kit-sentiment-analysis-2.0-1b-q4.gguf?download=true");
// Create an instance of SentimentAnalysis
SentimentAnalysis sentimentAnalyzer = new SentimentAnalysis(languageModel);
// Analyze text sentiment
SentimentCategory sentiment = sentimentAnalyzer.GetSentimentCategory("This is the worst service I've ever experienced.");
Console.WriteLine($"Sentiment: {sentiment}");
// Output: "Sentiment: Negative"
Remarks
This method performs a synchronous analysis. For large texts or when running in UI applications, consider using the asynchronous version GetSentimentCategoryAsync(string, CancellationToken).
Exceptions
- ArgumentNullException
Thrown if the input
text
isnull
or whitespace.