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.
- C#
- VB.NET
public class SmartExtensions
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 | ServiceProvider | The service provider to modify. |
| lifetime | ServiceLifetime | 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 | ServiceProvider | The service provider to modify. |
| implementation | TService | The implementation instance to use. |
| lifetime | ServiceLifetime | 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 | ServiceProvider | The service provider to modify. |
| factory | Func<Type, Object> | The factory method to create the service instance. |
| lifetime | ServiceLifetime | 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 | Single[][] | The embeddings to cluster. |
| count | Int32 | The number of clusters to create. |
| maxDivergence | Double | 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 | Single[] | The first vector. |
| vectorB | Single[] | 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 | Single[] | The vector to compare. |
| vectorB | Single[][] | 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 | String | 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.