Skip to main content

SmartExtensions

Namespace: Wisej.AI

Assembly: Wisej.AI

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.

ParameterTypeDescription
TServiceThe type of service to add or replace.
TImplementationThe type of the implementation to use.
servicesServiceProviderThe service provider to modify.
lifetime ServiceLifetimeThe 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.

ParameterTypeDescription
TServiceThe type of service to add or replace.
servicesServiceProviderThe service provider to modify.
implementationTServiceThe implementation instance to use.
lifetime ServiceLifetimeThe 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.

ParameterTypeDescription
TServiceThe type of service to add or replace.
servicesServiceProviderThe service provider to modify.
factoryFunc<Type, Object>The factory method to create the service instance.
lifetime ServiceLifetimeThe 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.

ParameterTypeDescription
embeddingsSingle[][]The embeddings to cluster.
countInt32The number of clusters to create.
maxDivergence DoubleThe 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.

ParameterTypeDescription
vectorASingle[]The first vector.
vectorBSingle[]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.

ParameterTypeDescription
vectorASingle[]The vector to compare.
vectorBSingle[][]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.

ParameterTypeDescription
textStringThe 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.