< Summary

Information
Class: Intentum.AI.Similarity.WeightedAverageSimilarityEngine
Assembly: Intentum.AI
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.AI/Similarity/WeightedAverageSimilarityEngine.cs
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 49
Line coverage: 100%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%22100%
CalculateIntentScore(...)100%11100%
CalculateIntentScore(...)87.5%88100%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.AI/Similarity/WeightedAverageSimilarityEngine.cs

#LineLine coverage
 1using Intentum.AI.Embeddings;
 2
 3namespace Intentum.AI.Similarity;
 4
 5/// <summary>
 6/// Similarity engine that applies weights to embeddings based on their source (actor:action).
 7/// Useful when certain behaviors should have more influence on intent inference.
 8/// </summary>
 9public sealed class WeightedAverageSimilarityEngine : IIntentSimilarityEngine
 10{
 11    private readonly IReadOnlyDictionary<string, double> _weights;
 12    private readonly double _defaultWeight;
 13
 14    /// <summary>
 15    /// Creates a weighted average similarity engine.
 16    /// </summary>
 17    /// <param name="weights">Dictionary mapping source keys (actor:action) to weights. If a source is not found, defaul
 18    /// <param name="defaultWeight">Default weight for sources not in the weights dictionary. Defaults to 1.0.</param>
 719    public WeightedAverageSimilarityEngine(
 720        IReadOnlyDictionary<string, double>? weights = null,
 721        double defaultWeight = 1.0)
 722    {
 723        _weights = weights ?? new Dictionary<string, double>();
 724        _defaultWeight = defaultWeight;
 725    }
 26
 27    public double CalculateIntentScore(IReadOnlyCollection<IntentEmbedding> embeddings)
 628        => CalculateIntentScore(embeddings, null);
 29
 30    /// <inheritdoc />
 31    public double CalculateIntentScore(IReadOnlyCollection<IntentEmbedding> embeddings, IReadOnlyDictionary<string, doub
 732    {
 733        if (embeddings.Count == 0)
 134            return 0;
 35
 636        var totalWeightedScore = 0.0;
 637        var totalWeight = 0.0;
 38
 4439        foreach (var embedding in embeddings)
 1340        {
 1341            var weight = sourceWeights?.GetValueOrDefault(embedding.Source, _defaultWeight)
 1342                ?? _weights.GetValueOrDefault(embedding.Source, _defaultWeight);
 1343            totalWeightedScore += embedding.Score * weight;
 1344            totalWeight += weight;
 1345        }
 46
 647        return totalWeight > 0 ? totalWeightedScore / totalWeight : 0;
 748    }
 49}