| | | 1 | | using Intentum.AI.Embeddings; |
| | | 2 | | |
| | | 3 | | namespace Intentum.AI.Similarity; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Similarity engine that averages embedding scores; optionally weighted by source (e.g. event counts). |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class SimpleAverageSimilarityEngine : IIntentSimilarityEngine |
| | | 9 | | { |
| | | 10 | | /// <inheritdoc /> |
| | | 11 | | public double CalculateIntentScore(IReadOnlyCollection<IntentEmbedding> embeddings) |
| | 9 | 12 | | => CalculateIntentScore(embeddings, null); |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public double CalculateIntentScore(IReadOnlyCollection<IntentEmbedding> embeddings, IReadOnlyDictionary<string, doub |
| | 103 | 16 | | { |
| | 103 | 17 | | if (embeddings.Count == 0) |
| | 1 | 18 | | return 0; |
| | | 19 | | |
| | 102 | 20 | | if (sourceWeights == null || sourceWeights.Count == 0) |
| | 25 | 21 | | return embeddings.Average(e => e.Score); |
| | | 22 | | |
| | 93 | 23 | | var totalWeighted = 0.0; |
| | 93 | 24 | | var totalWeight = 0.0; |
| | 723 | 25 | | foreach (var e in embeddings) |
| | 222 | 26 | | { |
| | 222 | 27 | | var w = sourceWeights.GetValueOrDefault(e.Source, 1.0); |
| | 222 | 28 | | totalWeighted += e.Score * w; |
| | 222 | 29 | | totalWeight += w; |
| | 222 | 30 | | } |
| | 93 | 31 | | return totalWeight > 0 ? totalWeighted / totalWeight : embeddings.Average(x => x.Score); |
| | 103 | 32 | | } |
| | | 33 | | } |