public void RecordWatsonResults(ActionToAnalyze actionToAnalyze) { // insert ActionSentiment ActionSentiment sentiment = InsertActionSentiment(_db, actionToAnalyze); _db.SubmitChanges(); // get the DB generated ID int actionSentimentID = sentiment.ActionSentimentID; // insert child records - ActionSentimentScore(s) List <Tones> tones = actionToAnalyze.GetTones(); List <ActionSentimentScore> scores = InsertSentimentScores(tones, _db, actionSentimentID); // update the corresponding ticket sentiment if (!actionToAnalyze.IsAgent) { ActionSentimentScore maxScore = scores.Where(s => s.SentimentScore == scores.Max(a => a.SentimentScore)).First(); TicketSentiment.TicketSentimentStrategy(_db, actionToAnalyze, maxScore); } // Delete ActionToAnalyze actionToAnalyze.DeleteOnSubmit(_db); _db.SubmitChanges(); }
/// <summary> /// Insert the detected sentiment tones, or a default if not found /// </summary> /// <param name="tones">tones from IBM Watson - may be empty</param> /// <param name="db">data context</param> /// <param name="actionSentimentID">Parent record ID</param> private static List <ActionSentimentScore> InsertSentimentScores(List <Tones> tones, DataContext db, int actionSentimentID) { List <ActionSentimentScore> result = new List <ActionSentimentScore>(); // if no tones detected - insert default score of 0.0 Table <ActionSentimentScore> actionSentimentScoreTable = db.GetTable <ActionSentimentScore>(); if ((tones == null) || !tones.Any()) { // no tones detected ActionSentimentScore score = new ActionSentimentScore { ActionSentimentID = actionSentimentID, SentimentID = 0, // no sentiment detected SentimentScore = 0 }; actionSentimentScoreTable.InsertOnSubmit(score); result.Add(score); return(result); } // insert tone sentiment scores foreach (Tones tone in tones) { ActionSentimentScore score = new ActionSentimentScore { ActionSentimentID = actionSentimentID, SentimentID = (int)FindSentimentID(tone.tone_id), SentimentScore = Convert.ToDecimal(tone.score) }; actionSentimentScoreTable.InsertOnSubmit(score); result.Add(score); } return(result); }
/// <summary> /// Callback on insert of Watson results - using the transaction submitted but NOT committed /// /// For each action, use the most likely sentiment (highest SentimentScore) /// Use Net Promoter score (promoters - detractors), normalized to [0, 1000] where 500 is neutral /// </summary> /// <param name="transaction">data associated with the watson transaction</param> public static void TicketSentimentStrategy(DataContext db, ActionToAnalyze actionToAnalyze, ActionSentimentScore maxScore) { if (actionToAnalyze.IsAgent) { return; } try { // normalize to [0, 1000] double actionScore = Convert.ToDouble(ToneSentiment.ToneSentiments[maxScore.SentimentID].SentimentMultiplier.Value) * Convert.ToDouble(maxScore.SentimentScore); actionScore = 500 * actionScore + 500; // submit TicketSentiment TicketSentiment score = null; CreateTicketSentiment(db, actionToAnalyze, actionScore, out score); score.TicketSentimentScore = (int)Math.Round(score.AverageActionSentiment); score.SetSentimentID(maxScore.SentimentID); db.SubmitChanges(); } catch (Exception e2) { WatsonEventLog.WriteEntry("Exception caught at select from ACtionsToAnalyze or HttpPOST:", e2); Console.WriteLine(e2.ToString()); } }