SmartExtensions

Wisej.AI.SmartExtensions

Namespace: Wisej.AI

Assembly: Wisej.AI (3.5.0.0)

Provides extension methods for various operations such as calculating cosine similarity, computing clusters, converting strings to camel case, and managing services in a service provider.

public class SmartExtensions

Methods

AddOrReplaceService<TService, TImplementation>(services, lifetime)

Adds or replaces a service in the service provider with the specified implementation type.

Parameter
Type
Description

TService

The type of service to add or replace.

TImplementation

The type of the implementation to use.

services

The service provider to modify.

lifetime

The lifetime of the service.

Returns: ServiceProvider. The modified service provider.

AddOrReplaceService<TService>(services, implementation, lifetime)

Adds or replaces a service in the service provider with the specified implementation instance.

Parameter
Type
Description

TService

The type of service to add or replace.

services

The service provider to modify.

implementation

The implementation instance to use.

lifetime

The lifetime of the service.

Returns: ServiceProvider. The modified service provider.

AddOrReplaceService<TService>(services, factory, lifetime)

Adds or replaces a service in the service provider using a factory method.

Parameter
Type
Description

TService

The type of service to add or replace.

services

The service provider to modify.

factory

The factory method to create the service instance.

lifetime

The lifetime of the service.

Returns: ServiceProvider. The modified service provider.

ComputeClusters(embeddings, count, maxDivergence)

Computes clusters from the given embeddings using the K-Means clustering algorithm.

Parameter
Type
Description

embeddings

The embeddings to cluster.

count

The number of clusters to create.

maxDivergence

The maximum divergence allowed for convergence.

Returns: ValueTuple`2[]. An array of tuples containing the centroid and vectors of each cluster.

K-Means clustering is a method of vector quantization, originally from signal processing, that is popular for cluster analysis in data mining. This method partitions the embeddings into count clusters.

CosineSimilarity(vectorA, vectorB)

Calculates the cosine similarity between two vectors.

Parameter
Type
Description

vectorA

The first vector.

vectorB

The second vector.

Returns: Single. The cosine similarity between the two vectors.

Cosine similarity is a measure of similarity between two non-zero vectors of an inner product space. It is defined as the cosine of the angle between the two vectors.

CosineSimilarity(vectorA, vectorB)

Calculates the cosine similarity between a vector and an array of vectors.

Parameter
Type
Description

vectorA

The vector to compare.

vectorB

The array of vectors to compare against.

Returns: Single[]. An array of cosine similarity values for each vector in the array.

This method extends the single vector cosine similarity calculation to handle multiple vectors, returning an array of similarity scores.

ToCamelCase(text)

Converts the first character of the string to lowercase, making it camel case.

Parameter
Type
Description

text

The string to convert.

Returns: String. The camel case version of the string.

This method is useful for converting PascalCase strings to camelCase, which is often used in JSON serialization and other contexts.

Last updated