private void ProcessNonsenseResult(NonsenseLogicResponse result)
 {
     if (ServerUtilities.msgIdToUserID[result.Msg_id] is NewAnswerNonsenseCheck)
     {
         if (result.Nonsense)
         {
             ProcessChatbotLogic.ProcessNonsenseAnswer((NewAnswerNonsenseCheck)ServerUtilities.msgIdToUserID[result.Msg_id]);
         }
         else
         {
             SendAnswerOffenseRequest((NewAnswerNonsenseCheck)ServerUtilities.msgIdToUserID[result.Msg_id]);
         }
     }
     else if (ServerUtilities.msgIdToUserID[result.Msg_id] is NewQuestionNonsenseCheck)
     {
         if (result.Nonsense)
         {
             ProcessChatbotLogic.ProcessNonsenseQuestion((NewQuestionNonsenseCheck)ServerUtilities.msgIdToUserID[result.Msg_id]);
             ChatbotWebSocketController.SendAnswerToQuestion(new ChatbotVariousServerResponses((NewQuestionNonsenseCheck)ServerUtilities.msgIdToUserID[result.Msg_id]));
         }
         else
         {
             SendQuestionOffenseRequest((NewQuestionNonsenseCheck)ServerUtilities.msgIdToUserID[result.Msg_id]);
         }
     }
 }
        /// <summary>
        /// Process an NLP NonsenseModelResponse and turn it into an NonsenseLogicResponse.
        /// </summary>
        /// <param name="nonsenseModelResponse">The NLP model to process.</param>
        /// <returns>An NonsenseLogicResponse describing the processed NonsenseModelResponse. The response
        /// is either descriptive of the processed response or "not complete" if the given response was invalid.</returns>
        public static NonsenseLogicResponse ProcessNLPNonsenseResponse(NonsenseModelResponse nonsenseModelResponse)
        {
            // Create an "invalid model responses" response
            // Check to see whether there is at least a valid answer given
            NonsenseLogicResponse nullResponse = new NonsenseLogicResponse();

            if (nonsenseModelResponse == null ||
                !nonsenseModelResponse.IsComplete())
            {
                return(nullResponse);
            }

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

            if (nonsenseModelResponse.nonsense)
            {
                nonsense = true;
            }

            // Return the result
            return(new NonsenseLogicResponse(
                       nonsenseModelResponse.question_id,
                       nonsense,
                       nonsenseModelResponse.question,
                       nonsenseModelResponse.msg_id
                       ));
        }
        public static void SendQuestionOffenseRequest(NonsenseLogicResponse offensivenessModel)
        {
            if (connections.ContainsKey("NLP") && connections["NLP"] != null && connections["NLP"].State == WebSocketState.Open && offensivenessModel != null)
            {
                String json = JsonSerializer.Serialize(offensivenessModel);

                connections["NLP"].SendAsync(new ArraySegment <byte>(usedEncoding.GetBytes(json), 0, json.Length), WebSocketMessageType.Text, true, CancellationToken.None);
            }
            else
            {
                taskQueue.Enqueue(new NLPTask <NonsenseLogicResponse>(offensivenessModel, SendQuestionOffenseRequest));
            }
        }
        private void HandleResponse(KeyValuePair <WEBSOCKET_RESPONSE_TYPE, List <BaseModel> > model)
        {
            //TODO: what should the websocket do with bad responses?
            if (model.Key == WEBSOCKET_RESPONSE_TYPE.NONE)
            {
                return;
            }

            switch (model.Key)
            {
            case WEBSOCKET_RESPONSE_TYPE.MATCH_QUESTION:
                try
                {
                    {
                        MatchQuestionLogicResponse result = ProcessNLPResponse.ProcessNLPMatchQuestionsResponse(model.Value.Cast <MatchQuestionModelResponse>().ToList().First());

                        if (result != null)
                        {
                            if (ServerUtilities.msgIdToUserID[result.Msg_id] is NewQuestion)
                            {
                                if (result.Match)
                                {
                                    ChatbotWebSocketController.SendAnswerToQuestion(new ChatbotNewAnswerModel(result));
                                }
                                else
                                {
                                    var temp = ProcessChatbotLogic.GenerateModelCompareToOpenQuestions((NewQuestion)ServerUtilities.msgIdToUserID[result.Msg_id]);
                                    if (temp != null)
                                    {
                                        NLPWebSocketController.SendQuestionMatchRequest(temp);
                                    }
                                }
                            }
                            else if (ServerUtilities.msgIdToUserID[result.Msg_id] is NewOpenQuestion)
                            {
                                if (result.Match)
                                {
                                    ChatbotWebSocketController.SendAnswerToQuestion(new ChatbotNewAnswerModel(result));
                                }
                                else
                                {
                                    int    question_id = ProcessChatbotLogic.SaveQuestionToDatabase((NewOpenQuestion)ServerUtilities.msgIdToUserID[result.Msg_id]);
                                    String user_id     = ((NewOpenQuestion)ServerUtilities.msgIdToUserID[((MatchQuestionModelResponse)model.Value.First()).msg_id]).user_id;
                                    ChatbotWebSocketController.SendAnswerToQuestion(new ServerResponseNoAnswerToQuestion(result, (MatchQuestionModelResponse)model.Value.First(), question_id));
                                    ProcessChatbotLogic.AddNewOpenAnswer(user_id, question_id);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    System.Diagnostics.Trace.Write(e.Message);
                }
                break;

            case WEBSOCKET_RESPONSE_TYPE.OFFENSIVENESS:
            {
                try
                {
                    var result = ProcessNLPResponse.ProcessNLPOffensivenessResponse(model.Value.Cast <OffensivenessModelResponse>().ToList().First());
                    if (result is OffensivenessLogicResponse)
                    {
                        ProcessOffenseResult(result);
                    }
                }
                catch (Exception e)
                {
                    System.Diagnostics.Trace.Write(e.Message);
                }
            }
            break;

            case WEBSOCKET_RESPONSE_TYPE.NONSENSE:
            {
                try
                {
                    NonsenseLogicResponse result = ProcessNLPResponse.ProcessNLPNonsenseResponse(model.Value.Cast <NonsenseModelResponse>().ToList().First());
                    if (result is NonsenseLogicResponse)
                    {
                        ProcessNonsenseResult(result);
                        //SendQuestionOffenseRequest((NonsenseLogicResponse)result);
                    }
                }
                catch (Exception e)
                {
                    System.Diagnostics.Trace.Write(e.Message);
                }
            }
            break;
            }
        }