| | | 1 | | using Intentum.Analytics.Models; |
| | | 2 | | |
| | | 3 | | namespace Intentum.Analytics; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Builds an intent-based profile for an entity from timeline data (aggregate intent names, confidence distribution → l |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class IntentProfileService : IIntentProfileService |
| | | 9 | | { |
| | | 10 | | private readonly IIntentAnalytics _analytics; |
| | | 11 | | |
| | 2 | 12 | | public IntentProfileService(IIntentAnalytics analytics) |
| | 2 | 13 | | { |
| | 2 | 14 | | _analytics = analytics ?? throw new ArgumentNullException(nameof(analytics)); |
| | 2 | 15 | | } |
| | | 16 | | |
| | | 17 | | /// <inheritdoc /> |
| | | 18 | | public async Task<IntentProfile> GetProfileAsync( |
| | | 19 | | string entityId, |
| | | 20 | | DateTimeOffset start, |
| | | 21 | | DateTimeOffset end, |
| | | 22 | | CancellationToken cancellationToken = default) |
| | 2 | 23 | | { |
| | 2 | 24 | | var timeline = await _analytics.GetIntentTimelineAsync(entityId, start, end, cancellationToken); |
| | 2 | 25 | | var points = timeline.Points; |
| | | 26 | | |
| | 2 | 27 | | if (points.Count == 0) |
| | 1 | 28 | | { |
| | 1 | 29 | | return new IntentProfile( |
| | 1 | 30 | | entityId, |
| | 1 | 31 | | start, |
| | 1 | 32 | | end, |
| | 1 | 33 | | [], |
| | 1 | 34 | | new Dictionary<string, int>(), |
| | 1 | 35 | | 0, |
| | 1 | 36 | | 0, |
| | 1 | 37 | | 0); |
| | | 38 | | } |
| | | 39 | | |
| | 1 | 40 | | var intentCounts = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); |
| | 1 | 41 | | var totalScore = 0.0; |
| | 1 | 42 | | var highCount = 0; |
| | | 43 | | |
| | 7 | 44 | | foreach (var p in points) |
| | 2 | 45 | | { |
| | 2 | 46 | | intentCounts[p.IntentName] = intentCounts.GetValueOrDefault(p.IntentName, 0) + 1; |
| | 2 | 47 | | totalScore += p.ConfidenceScore; |
| | 2 | 48 | | if (p.ConfidenceLevel is "High" or "Certain") |
| | 1 | 49 | | highCount++; |
| | 2 | 50 | | } |
| | | 51 | | |
| | 1 | 52 | | var avgScore = totalScore / points.Count; |
| | 1 | 53 | | var highPct = 100.0 * highCount / points.Count; |
| | | 54 | | |
| | 1 | 55 | | var topIntents = intentCounts |
| | 1 | 56 | | .OrderByDescending(kv => kv.Value) |
| | 1 | 57 | | .Take(10) |
| | 3 | 58 | | .ToDictionary(kv => kv.Key, kv => kv.Value, StringComparer.OrdinalIgnoreCase); |
| | | 59 | | |
| | 1 | 60 | | var labels = new List<string>(); |
| | | 61 | | |
| | 1 | 62 | | if (points.Count >= 5) |
| | 0 | 63 | | labels.Add("active"); |
| | 1 | 64 | | if (avgScore >= 0.7) |
| | 1 | 65 | | labels.Add("high_confidence"); |
| | 0 | 66 | | else if (avgScore < 0.4) |
| | 0 | 67 | | labels.Add("low_confidence"); |
| | 1 | 68 | | if (highPct >= 70) |
| | 0 | 69 | | labels.Add("quick_decider"); |
| | 1 | 70 | | if (intentCounts.Count >= 3 && points.Count >= 5) |
| | 0 | 71 | | labels.Add("diverse_intents"); |
| | | 72 | | |
| | 2 | 73 | | var topIntentName = intentCounts.OrderByDescending(kv => kv.Value).FirstOrDefault().Key; |
| | 1 | 74 | | if (!string.IsNullOrEmpty(topIntentName) && intentCounts[topIntentName] >= Math.Max(2, points.Count / 2)) |
| | 1 | 75 | | labels.Add($"frequent_{topIntentName.ToLowerInvariant().Replace(" ", "_")}"); |
| | | 76 | | |
| | 1 | 77 | | return new IntentProfile( |
| | 1 | 78 | | entityId, |
| | 1 | 79 | | start, |
| | 1 | 80 | | end, |
| | 1 | 81 | | labels, |
| | 1 | 82 | | topIntents, |
| | 1 | 83 | | avgScore, |
| | 1 | 84 | | highPct, |
| | 1 | 85 | | points.Count); |
| | 2 | 86 | | } |
| | | 87 | | } |