Method GetBestCategory
- Namespace
- LMKit.TextAnalysis
- Assembly
- LM-Kit.NET.dll
GetBestCategory(IList<string>, string, bool, CancellationToken)
Classifies the specified plain text into one of the predefined categories.
public int GetBestCategory(IList<string> categories, string content, bool normalize = true, CancellationToken cancellationToken = default)
Parameters
categoriesIList<string>A list of predefined category names. Each entry should describe the corresponding category.
contentstringThe plain text to be classified.
normalizeboolOptional. Specifies whether the input parameters should be normalized. The default is
true.cancellationTokenCancellationTokenOptional. A CancellationToken that can be used to cancel the operation.
Returns
- int
The index of the best matching category from the
categorieslist, or -1 if no suitable category is found.
Examples
using LMKit.Model;
using LMKit.TextAnalysis;
using System;
using System.Collections.Generic;
class Example
{
static void Main()
{
LM model = LM.LoadFromModelID("lmkit-tasks:4b-preview");
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 canceled via the
cancellationToken.
GetBestCategory(IList<string>, IList<string>, string, bool, CancellationToken)
Classifies the specified plain text into one of the predefined categories.
public int GetBestCategory(IList<string> categories, IList<string> categoryDescriptions, string content, bool normalize = true, CancellationToken cancellationToken = default)
Parameters
categoriesIList<string>List of predefined categories.
categoryDescriptionsIList<string>Optional descriptions for each category.
contentstringThe plain text to be classified.
normalizeboolSpecifies whether normalization should occur.
cancellationTokenCancellationTokenCancellationToken for cancelling the operation.
Returns
- int
The index of the best matching category or -1 if none match.
GetBestCategory(IList<string>, Attachment, bool, CancellationToken)
Classifies the content provided as an attachment into one of the predefined categories. The attachment can represent plain text or an image. If the attachment is an image requiring vision processing, the underlying language model must support vision input.
public int GetBestCategory(IList<string> categories, Attachment content, bool normalize = true, CancellationToken cancellationToken = default)
Parameters
categoriesIList<string>A list of predefined category names.
contentAttachmentThe attachment to be classified.
normalizeboolSpecifies whether input parameters should be normalized. Default is
true.cancellationTokenCancellationTokenCancellationToken for cancelling the operation.
Returns
- int
The index of the best matching category or -1 if none match.
Exceptions
- InvalidModelException
Thrown when the attachment is an image requiring vision input but the underlying language model does not support vision.
GetBestCategory(IList<string>, ImageBuffer, bool, CancellationToken)
Classifies the content provided in an image into one of the predefined categories, assuming the underlying language model supports vision input.
public int GetBestCategory(IList<string> categories, ImageBuffer image, bool normalize = true, CancellationToken cancellationToken = default)
Parameters
categoriesIList<string>A list of predefined category names.
imageImageBufferThe image to classify.
normalizeboolSpecifies whether input parameters should be normalized (default is
true).cancellationTokenCancellationTokenA
CancellationTokenused to cancel the operation.
Returns
- int
The zero-based index of the best-matching category, or
-1if none match.
Exceptions
- InvalidModelException
Thrown if the underlying model does not support vision.
GetBestCategory(IList<string>, IList<string>, Attachment, bool, CancellationToken)
Classifies the content provided as an attachment into one of the predefined categories.
public int GetBestCategory(IList<string> categories, IList<string> categoryDescriptions, Attachment content, bool normalize = true, CancellationToken cancellationToken = default)
Parameters
categoriesIList<string>A list of predefined category names.
categoryDescriptionsIList<string>Optional descriptions for each category.
contentAttachmentThe attachment to be classified.
normalizeboolSpecifies whether input parameters should be normalized. Default is
true.cancellationTokenCancellationTokenCancellationToken for cancelling the operation.
Returns
- int
The index of the best matching category or -1 if none match.