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-1if 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>A list of predefined categories.
categoryDescriptionsIList<string>Optional descriptions for each category.
contentstringThe plain text to be classified.
normalizeboolSpecifies whether normalization should occur.
cancellationTokenCancellationTokenA CancellationToken for canceling the operation.
Returns
- int
The index of the best matching category, or
-1if none match.
GetBestCategory(IList<string>, Attachment, bool, CancellationToken)
Classifies the content provided as an attachment into one of the predefined categories.
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. Supports various file formats including images, PDF documents, HTML files, and Microsoft Office formats. See Attachment for the complete list of supported formats.
normalizeboolSpecifies whether input parameters should be normalized. The default is
true.cancellationTokenCancellationTokenA CancellationToken for canceling the operation.
Returns
- int
The index of the best matching category, or
-1if none match.
Examples
using LMKit.Model;
using LMKit.TextAnalysis;
using LMKit.Data;
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> { "invoice", "receipt", "contract", "letter" };
// Classify a PDF document
var attachment = new Attachment("path/to/document.pdf");
int bestCategoryIndex = categorization.GetBestCategory(categories, attachment);
if (bestCategoryIndex != -1)
{
Console.WriteLine($"Document type: {categories[bestCategoryIndex]}");
}
}
}
Exceptions
- InvalidModelException
Thrown when the attachment contains visual content 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. The underlying language model must support 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. The default is
true.cancellationTokenCancellationTokenA CancellationToken used 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. Supports various file formats including images, PDF documents, HTML files, and Microsoft Office formats.
normalizeboolSpecifies whether input parameters should be normalized. The default is
true.cancellationTokenCancellationTokenA CancellationToken for canceling the operation.
Returns
- int
The index of the best matching category, or
-1if none match.
Exceptions
- InvalidModelException
Thrown when the attachment contains visual content requiring vision input but the underlying language model does not support vision.