/// <summary> /// Scoring with Tree Model /// </summary> /// <param name="root">Parent node</param> /// <param name="missingvalueStr">Missing value strategy to evaluate this node.</param> /// <param name="noTrueChildStr">Strategy to evaluate this node if no child are true</param> /// <param name="dict">Values</param> /// <param name="res" >Result to return</param> /// <returns></returns> public static ScoreResult Evaluate(Node root, MissingValueStrategy missingvalueStr, NoTrueChildStrategy noTrueChildStr, Dictionary <string, object> dict, ScoreResult res) { // Test childs foreach (Node child in root.Nodes) { PredicateResult childPredicate = child.Predicate.Evaluate(dict); if (childPredicate == PredicateResult.True) { res.Nodes.Add(child); res.Value = child.Score; foreach (ScoreDistribution sco in child.ScoreDistributions) { if (sco.Value.Equals(child.Score)) { if (sco.Confidence != null) { res.Confidence = Decimal.Parse(sco.Confidence, NumberStyles.Any, CultureInfo.InvariantCulture); } } } return(Evaluate(child, missingvalueStr, noTrueChildStr, dict, res)); } else if (childPredicate == PredicateResult.Unknown) { // Unknow value lead to act with missingvalueStr switch (missingvalueStr) { case MissingValueStrategy.LastPrediction: return(res); case MissingValueStrategy.NullPrediction: res.Value = null; return(res); case MissingValueStrategy.WeightedConfidence: Dictionary <string, decimal> conf = CalculateConfidence(root, dict); string max_conf = null; foreach (string key in conf.Keys) { if (max_conf == null) { max_conf = key; } if (conf[key] > conf[max_conf]) { max_conf = key; } } res.Value = max_conf; res.Confidence = conf[max_conf]; return(res); case MissingValueStrategy.AggregateNodes: return(res); default: throw new NotImplementedException(); } } } // All child nodes are false if (root.Nodes.Count > 0) { if (noTrueChildStr == NoTrueChildStrategy.ReturnNullPrediction) { res.Value = null; } } return(res); }
/// <summary> /// Scoring with Tree Model /// </summary> /// <param name="root">Parent node</param> /// <param name="missingvalueStr">Missing value strategy to evaluate this node.</param> /// <param name="noTrueChildStr">Strategy to evaluate this node if no child are true</param> /// <param name="dict">Values</param> /// <param name="res" >Result to return</param> /// <returns></returns> public static ScoreResult Evaluate(Node root, MissingValueStrategy missingvalueStr, NoTrueChildStrategy noTrueChildStr, Dictionary<string, object> dict, ScoreResult res) { // Test childs foreach(Node child in root.Nodes) { PredicateResult childPredicate = child.Predicate.Evaluate(dict); if (childPredicate == PredicateResult.True) { res.Nodes.Add(child); res.Value = child.Score; foreach(ScoreDistribution sco in child.ScoreDistributions) { if (sco.Value.Equals(child.Score)) { if (sco.Confidence != null) res.Confidence = Decimal.Parse(sco.Confidence, NumberStyles.Any, CultureInfo.InvariantCulture); } } return Evaluate(child, missingvalueStr, noTrueChildStr, dict, res); } else if (childPredicate == PredicateResult.Unknown) { // Unknow value lead to act with missingvalueStr switch(missingvalueStr) { case MissingValueStrategy.LastPrediction: return res; case MissingValueStrategy.NullPrediction: res.Value = null; return res; case MissingValueStrategy.WeightedConfidence: Dictionary<string, decimal> conf = CalculateConfidence(root, dict); string max_conf = null; foreach(string key in conf.Keys) { if (max_conf == null) max_conf = key; if (conf[key] > conf[max_conf]) max_conf = key; } res.Value = max_conf; res.Confidence = conf[max_conf]; return res; case MissingValueStrategy.AggregateNodes: return res; default: throw new NotImplementedException(); } } } // All child nodes are false if (root.Nodes.Count > 0) if (noTrueChildStr == NoTrueChildStrategy.ReturnNullPrediction) { res.Value = null; } return res; }