| | | 1 | | using Intentum.Core.Behavior; |
| | | 2 | | using Intentum.Core.Intents; |
| | | 3 | | |
| | | 4 | | namespace Intentum.Core.Evaluation; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Infers intent and confidence from a behavior space. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class IntentEvaluator |
| | | 10 | | { |
| | | 11 | | /// <summary>Evaluates the given behavior space and returns an intent with confidence and signals.</summary> |
| | | 12 | | public static IntentEvaluationResult Evaluate( |
| | | 13 | | string intentName, |
| | | 14 | | BehaviorSpace behaviorSpace) |
| | 3 | 15 | | { |
| | 3 | 16 | | var vector = behaviorSpace.ToVector(); |
| | | 17 | | |
| | 3 | 18 | | var signals = vector.Dimensions.Select(d => |
| | 6 | 19 | | new IntentSignal( |
| | 6 | 20 | | Source: "behavior", |
| | 6 | 21 | | Description: d.Key, |
| | 6 | 22 | | Weight: Normalize(d.Value) |
| | 6 | 23 | | )).ToList(); |
| | | 24 | | |
| | 6 | 25 | | var score = signals.Sum(s => s.Weight) / Math.Max(1, signals.Count); |
| | | 26 | | |
| | 3 | 27 | | var confidence = IntentConfidence.FromScore(score); |
| | | 28 | | |
| | 3 | 29 | | var intent = new Intent( |
| | 3 | 30 | | intentName, |
| | 3 | 31 | | signals, |
| | 3 | 32 | | confidence |
| | 3 | 33 | | ); |
| | | 34 | | |
| | 3 | 35 | | return new IntentEvaluationResult(intent, vector); |
| | 3 | 36 | | } |
| | | 37 | | |
| | | 38 | | private static double Normalize(double value) |
| | 3 | 39 | | => Math.Min(1.0, value / 10.0); |
| | | 40 | | } |