private void ProcessOffenseResult(OffensivenessLogicResponse result)
 {
     if (ServerUtilities.msgIdToUserID[result.Msg_id] is NewAnswerOffenseCheck)
     {
         if (result.Offensive)
         {
             ProcessChatbotLogic.ProcessOffensiveAnswer((NewAnswerOffenseCheck)ServerUtilities.msgIdToUserID[result.Msg_id]);
         }
         else
         {
             //ProcessChatbotLogic.SaveQuestionToDatabase((NewQuestionNonsenseCheck)ServerUtilities.msgIdToUserID[result.Msg_id]);
             int    answerId            = ProcessChatbotLogic.SaveAnswerToOpenQuestion((NewAnswerOffenseCheck)ServerUtilities.msgIdToUserID[result.Msg_id]);
             String openAnswerModelUser = ProcessChatbotLogic.RetrieveOpenAnswer(((NewAnswerOffenseCheck)ServerUtilities.msgIdToUserID[result.Msg_id]).question_id);
             ChatbotWebSocketController.SendAnswerToQuestion(new ServerAnswerAfterQuestion(openAnswerModelUser, (NewAnswerOffenseCheck)ServerUtilities.msgIdToUserID[result.Msg_id], result, answerId, ProcessNLPResponse.getQuestionFromID(((NewAnswerOffenseCheck)ServerUtilities.msgIdToUserID[result.Msg_id]).question_id)));
         }
     }
     else if (ServerUtilities.msgIdToUserID[result.Msg_id] is NewQuestionNonsenseCheck)
     {
         if (result.Offensive)
         {
             ProcessChatbotLogic.ProcessOffensiveAnswer((NewQuestionNonsenseCheck)ServerUtilities.msgIdToUserID[result.Msg_id]);
             ChatbotWebSocketController.SendAnswerToQuestion(new ChatbotVariousServerResponses((NewQuestionNonsenseCheck)ServerUtilities.msgIdToUserID[result.Msg_id], false));
         }
         else
         {
             ChatbotWebSocketController.ProcessProperQuestion((NewQuestionNonsenseCheck)ServerUtilities.msgIdToUserID[result.Msg_id]);
         }
     }
 }
 private void Echo(OffensivenessLogicResponse result)
 {
     if (connections.ContainsKey("NLP") && connections["NLP"] != null && connections["NLP"].State == WebSocketState.Open)
     {
         connections["NLP"].SendAsync(new ArraySegment <byte>(usedEncoding.GetBytes("ECHO ECHO TO NLP"), 0, "ECHO ECHO TO NLP".Length), WebSocketMessageType.Text, true, CancellationToken.None);
     }
 }
        /// <summary>
        /// Process an NLP OffensivenessModelResponse and turn it into an OffensivenessLogicResponse.
        /// </summary>
        /// <param name="offensivenessModel">The NLP model to process.</param>
        /// <returns>An OffensivenessLogicResponse describing the processed OffensivenessModelResponse. The response
        /// is either descriptive of the processed response or "not complete" if the given response was invalid.</returns>
        public static OffensivenessLogicResponse ProcessNLPOffensivenessResponse(OffensivenessModelResponse offensivenessModel)
        {
            // Create an "invalid model responses" response
            OffensivenessLogicResponse nullResponse = new OffensivenessLogicResponse();

            // Check to see whether there is at least a valid answer given
            if (offensivenessModel == null ||
                !offensivenessModel.IsComplete())
            {
                return(nullResponse);
            }

            // Decide whether the given question is offensive
            bool offensive = false;

            if (offensivenessModel.prob > OffensiveThreshold)
            {
                offensive = true;
            }

            // Check if the sentence contains a blacklisted word
            DBManager     manager  = new DBManager(true);
            String        sentence = offensivenessModel.question;
            StringBuilder sb       = new StringBuilder();

            sb.Append("SELECT forbidden_word ");
            sb.Append("FROM dbo.Blacklist;");
            String sql             = sb.ToString();
            String blacklistedWord = null;

            using (SqlDataReader reader = manager.Read(sql))
            {
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        blacklistedWord = reader.GetString(0);
                        if (sentence.Contains(blacklistedWord))
                        {
                            offensive = true;
                            break;
                        }
                    }
                }
                else
                {
                    Debug.WriteLine("Reader null");
                }
            }
            manager.Close();

            // Return the result
            return(new OffensivenessLogicResponse(
                       offensivenessModel.question_id,
                       offensive,
                       offensivenessModel.question,
                       offensivenessModel.msg_id
                       ));
        }