Table of Contents

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 string

The plain text to be classified.

maxCategories int

The maximum number of top categories to return.

normalize bool

Optional. Specifies whether input parameters should be normalized. The default is true.

cancellationToken CancellationToken

Optional. A CancellationToken for canceling the operation.

Returns

List<int>

A list of indices representing the top categories for the given text.

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]);
}

GetTopCategories(IList<string>, 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, IList<string> categoryDescriptions, string content, int maxCategories, bool normalize = true, CancellationToken cancellationToken = default)

Parameters

categories IList<string>

A list of predefined category names.

categoryDescriptions IList<string>

Optional descriptions for each category.

content string

The plain text to be classified.

maxCategories int

The maximum number of top categories to return.

normalize bool

Specifies whether input parameters should be normalized. The default is true.

cancellationToken CancellationToken

A CancellationToken for canceling the operation.

Returns

List<int>

A list of indices representing the top categories for the given text.

GetTopCategories(IList<string>, ImageBuffer, int, bool, CancellationToken)

Classifies the given image into up to a specified maximum number of categories. The underlying language model must support vision input.

public List<int> GetTopCategories(IList<string> categories, ImageBuffer image, int maxCategories, bool normalize = true, CancellationToken cancellationToken = default)

Parameters

categories IList<string>

A list of predefined category names.

image ImageBuffer

The image to classify.

maxCategories int

The maximum number of top categories to return.

normalize bool

Specifies whether input parameters should be normalized. The default is true.

cancellationToken CancellationToken

A CancellationToken used to cancel the operation.

Returns

List<int>

A list of zero-based indices representing the top matching categories for the given image.

Exceptions

InvalidModelException

Thrown if the underlying model does not support vision when an image is provided.

GetTopCategories(IList<string>, Attachment, int, bool, CancellationToken)

Classifies the content provided as an attachment into up to a specified maximum number of categories.

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 Attachment

The 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.

maxCategories int

The maximum number of top categories to return.

normalize bool

Specifies whether input parameters should be normalized. The default is true.

cancellationToken CancellationToken

A CancellationToken for canceling the operation.

Returns

List<int>

A list of indices representing the top categories for the given attachment.

Exceptions

InvalidModelException

Thrown when the attachment contains visual content requiring vision input but the underlying language model does not support vision.

GetTopCategories(IList<string>, IList<string>, Attachment, int, bool, CancellationToken)

Classifies the content provided as an attachment into up to a specified maximum number of categories.

public List<int> GetTopCategories(IList<string> categories, IList<string> categoryDescriptions, Attachment content, int maxCategories, bool normalize = true, CancellationToken cancellationToken = default)

Parameters

categories IList<string>

A list of predefined category names.

categoryDescriptions IList<string>

Optional descriptions for each category.

content Attachment

The attachment to be classified. Supports various file formats including images, PDF documents, HTML files, and Microsoft Office formats.

maxCategories int

The maximum number of top categories to return.

normalize bool

Specifies whether input parameters should be normalized. The default is true.

cancellationToken CancellationToken

A CancellationToken for canceling the operation.

Returns

List<int>

A list of indices representing the top categories for the given attachment.

Exceptions

InvalidModelException

Thrown when the attachment contains visual content requiring vision input but the underlying language model does not support vision.