Method GetBestCategory
- Namespace
- LMKit.TextAnalysis
- Assembly
- LM-Kit.NET.dll
GetBestCategory(IList<string>, string, bool, CancellationToken)
Classifies a given text into one of the predefined categories.
public int GetBestCategory(IList<string> categories, string candidate, bool normalize = true, CancellationToken cancellationToken = default)
Parameters
categories
IList<string>A list of predefined categories. Each entry should be a description of its corresponding category.
candidate
stringThe plain text that needs to be classified.
normalize
boolOptional. Specifies whether input parameters normalization should be enabled. Default is true.
cancellationToken
CancellationTokenOptional. A CancellationToken for handling cancellation requests.
Returns
- int
The index within the
categories
list that best matches the category associated with thecandidate
text. Returns -1 if no category can be associated.
Examples
using LMKit.Model;
using LMKit.TextAnalysis;
using System;
using System.Collections.Generic;
class Example
{
static void Main()
{
Uri modelUri = new Uri("https://your-model-link-here");
LM model = new LM(modelUri);
Categorization categorization = new Categorization(model);
var categories = new List<string> { "finance", "travel", "entertainment" };
string text = "Stock prices soared on Wall Street.";
int bestCategoryIndex = categorization.GetBestCategory(categories, text);
if (bestCategoryIndex != -1)
{
Console.WriteLine($"Category: {categories[bestCategoryIndex]}");
}
else
{
Console.WriteLine("No suitable category found.");
}
Console.WriteLine($"Confidence: {categorization.Confidence}");
}
}
Exceptions
- OperationCanceledException
Thrown if the operation is cancelled via the
cancellationToken
.