Method GetTopCategories
- Namespace
- LMKit.TextAnalysis
- Assembly
- LM-Kit.NET.dll
GetTopCategories(IList<string>, string, int, bool, CancellationToken)
Classifies the specified plain text into up to a specified maximum number of categories.
public List<int> GetTopCategories(IList<string> categories, string content, int maxCategories, bool normalize = true, CancellationToken cancellationToken = default)
Parameters
categories
IList<string>A list of predefined category names.
content
stringThe plain text to be classified.
maxCategories
intThe maximum number of top categories to return.
normalize
boolOptional. Specifies whether the input parameters should be normalized. The default is
true
.cancellationToken
CancellationTokenOptional. A CancellationToken to cancel the operation if needed.
Returns
- List<int>
A list of indices representing the top categories for the given text. The indices correspond to positions in the input
categories
list.
Examples
// Synchronous usage:
var categories = new List<string> { "finance", "travel", "entertainment", "politics", "technology" };
string text = "The government unveiled a new technology policy impacting the finance sector.";
// Returns the top 3 categories:
IList<int> topCategories = categorization.GetTopCategories(categories, text, 3);
Console.WriteLine("Top categories:");
foreach (var index in topCategories)
{
Console.WriteLine(categories[index]);
}
Exceptions
- OperationCanceledException
Thrown if the operation is canceled via the
cancellationToken
.
GetTopCategories(IList<string>, Attachment, int, bool, CancellationToken)
Classifies the content provided as an attachment into up to a specified maximum number of categories. The attachment can contain either plain text or image data. If the attachment is an image requiring vision processing, the underlying language model must support vision input.
public List<int> GetTopCategories(IList<string> categories, Attachment content, int maxCategories, bool normalize = true, CancellationToken cancellationToken = default)
Parameters
categories
IList<string>A list of predefined category names.
content
AttachmentThe attachment to be classified, representing either plain text or image content.
maxCategories
intThe maximum number of top categories to return.
normalize
boolOptional. Specifies whether the input parameters should be normalized. The default is
true
.cancellationToken
CancellationTokenOptional. A CancellationToken to cancel the operation if needed.
Returns
- List<int>
A list of indices representing the top categories for the given attachment. The indices correspond to positions in the input
categories
list.
Exceptions
- InvalidModelException
Thrown when the attachment is an image that requires vision input but the underlying language model does not support vision.