< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
.ctor(...)75%44100%
CalculateIntentScore(...)83.33%66100%

File(s)

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

#LineLine coverage
 1using Intentum.AI.Embeddings;
 2
 3namespace Intentum.AI.Similarity;
 4
 5/// <summary>
 6/// Similarity engine that combines multiple similarity engines using weighted combination.
 7/// Useful for A/B testing or combining different similarity strategies.
 8/// </summary>
 9public sealed class CompositeSimilarityEngine : IIntentSimilarityEngine
 10{
 11    private readonly List<(IIntentSimilarityEngine Engine, double Weight)> _engines;
 12
 13    /// <summary>
 14    /// Creates a composite similarity engine with equal weights for all engines.
 15    /// </summary>
 16    public CompositeSimilarityEngine(IEnumerable<IIntentSimilarityEngine> engines)
 917        : this(engines.Select(e => (e, 1.0)).ToArray())
 318    {
 319    }
 20
 21    /// <summary>
 22    /// Creates a composite similarity engine with custom weights.
 23    /// </summary>
 624    public CompositeSimilarityEngine(params (IIntentSimilarityEngine Engine, double Weight)[] engines)
 625    {
 626        if (engines == null || engines.Length == 0)
 127            throw new ArgumentException("At least one engine is required", nameof(engines));
 28
 529        _engines = engines.ToList();
 530    }
 31
 32    public double CalculateIntentScore(IReadOnlyCollection<IntentEmbedding> embeddings)
 533    {
 534        if (embeddings.Count == 0)
 135            return 0;
 36
 437        var totalWeightedScore = 0.0;
 438        var totalWeight = 0.0;
 39
 3040        foreach (var (engine, weight) in _engines)
 941        {
 942            var score = engine.CalculateIntentScore(embeddings);
 943            totalWeightedScore += score * weight;
 944            totalWeight += weight;
 945        }
 46
 447        return totalWeight > 0 ? totalWeightedScore / totalWeight : 0;
 548    }
 49}