public bool TryAdd(ActionToAnalyze actionToAnalyze, Utterance utterance) { // Max 50 per call if (UtteranceCount + 1 > WatsonUtterancePerAPICall) { return(false); } _actions.Add(actionToAnalyze); _messageUtterances.Add(new MessageUtterance(actionToAnalyze, utterance)); return(true); }
/// <summary> Can we fit the action into a single utterance? </summary> public bool TryGetUtterance(out Utterance utterance) { string watsonText = WatsonText(); if (watsonText.Length > MaxUtteranceLength) { utterance = null; return(false); } utterance = new Utterance(IsAgent, watsonText); return(true); }
/// <summary> Pack Action to Utterances </summary> public static List <Utterance> PackActionToUtterances(bool isAgent, string text) { if (text.Length <= MaxUtteranceLength) { throw new Exception("Action would have fit into a single utterance"); } // pack sentences into utterances List <Utterance> results = new List <Utterance>(); StringBuilder utteranceBuilder = new StringBuilder(); string[] sentences = Regex.Split(text, @"(?<=[.?!,;:])"); foreach (string sentence in sentences) { // sentence fits in utterance if (utteranceBuilder.Length + sentence.Length <= MaxUtteranceLength) { utteranceBuilder.Append(sentence); continue; } // sentence too long to fit into current utterance if (sentence.Length < MaxUtteranceLength) { results.Add(new Utterance(isAgent, utteranceBuilder.ToString())); utteranceBuilder.Clear(); utteranceBuilder.Append(sentence); // start a new utterance continue; } Utterance.PackLongSentenceToUtterances(sentence, utteranceBuilder, isAgent, results); } // create an utterance with the remainder results.Add(new Utterance(isAgent, utteranceBuilder.ToString())); // does collection of utterances add up to text? string test = String.Empty; foreach (Utterance utterance in results) { test += utterance.text; } if (String.CompareOrdinal(text, test) != 0) { Debugger.Break(); } return(results); }
/// <summary> Write the message results back to the database </summary> public void PublishWatsonResponse(UtteranceToneList watsonResponse) { foreach (UtteranceResponse response in watsonResponse.utterances_tone) { MessageUtterance messageUtterance = _messageUtterances[response.utterance_id]; Utterance sent = messageUtterance._utterance; if (string.CompareOrdinal(sent.text, response.utterance_text) != 0) { Debugger.Break(); } ActionToAnalyze action = messageUtterance._action; action.AddSentiment(response); } // update the action with the accumulated results foreach (ActionToAnalyze action in _actions) { PublishActionSentiment(action); } }
public void Add(Utterance addition) { utterances.Add(addition); }
public MessageUtterance(ActionToAnalyze actionToAnalyze, Utterance utterance) { _action = actionToAnalyze; _utterance = utterance; }
/// <summary> /// Constructor to wrap the "using" of SqlConnection, SqlTransaction, and DataContext /// </summary> /// <param name="tones"></param> /// <param name="actionToAnalyze"></param> public WatsonResultsTransaction(Utterance utterance, ActionToAnalyze actionToAnalyze, Action <WatsonTransactionCallback> callback) { List <Tones> tones = utterance.tones; if (tones == null) { return; // ? } // open the connection try { _singleThreadedTransactions.WaitOne(); string connectionString = ConfigurationManager.AppSettings.Get("ConnectionString"); _connection = new SqlConnection(connectionString); // using _connection.Open(); // connection must be open to begin transaction // start the transaction _transaction = _connection.BeginTransaction(); // using // create a data context _db = new DataContext(_connection); // using _db.Transaction = _transaction; // insert ActionSentiment ActionSentiment sentiment = InsertActionSentiment(_db, actionToAnalyze); _db.SubmitChanges(); // get the DB generated ID int actionSentimentID = sentiment.ActionSentimentID; // insert child records - ActionSentimentScore(s) List <ActionSentimentScore> scores = InsertSentimentScores(tones, _db, actionSentimentID); // Delete ActionToAnalyze actionToAnalyze.DeleteOnSubmit(_db); _db.SubmitChanges(); if (callback != null) { WatsonTransactionCallback transaction = new WatsonTransactionCallback() { _db = _db, _sentiment = sentiment, _scores = scores }; callback(transaction); } // Success! _transaction.Commit(); } catch (Exception e) { if (_transaction != null) { _transaction.Rollback(); } EventLog.WriteEntry(EVENT_SOURCE, "********************PublishToTable SubmitTransaction: Exception " + e.Message + " ### " + e.Source + " ### " + e.StackTrace.ToString()); Console.WriteLine("Exception at insert into Action Sentiment:" + e.Message + "###" + e.Source + " ----- STACK: " + e.StackTrace.ToString()); } finally { _singleThreadedTransactions.ReleaseMutex(); } }