예제 #1
0
        public KeyValuePair <int, string> OnlineApiChannelforNLP(ChatIntent intent, List <ChatSessionEntity> entityList)
        {
            string      response      = string.Empty;
            ChatIntent  respondIntent = new ChatIntent();
            HttpContext httpContext   = HttpContext.Current;
            int         sessionId     = entityList.Select(x => x.SessionId).FirstOrDefault();

            if (intent.IntentName.Trim() == "ChangeAddressMeIntent")
            {
                string entityValue = entityList.Where(x => x.EntityType == "PERSON").Select(y => y.EntityValue).FirstOrDefault().ToLower().Trim();
                if (entityValue == "ravi raghu")
                {
                    respondIntent = db.ChatIntent.Where(x => x.ParentId == intent.ChatIntentId && x.IntentName.ToLower().Contains("yes")).FirstOrDefault();
                }
                else
                {
                    respondIntent = db.ChatIntent.Where(x => x.ParentId == intent.ChatIntentId && x.IntentName.ToLower().Contains("no")).FirstOrDefault();
                }
            }

            else if (intent.IntentName.Trim() == "NoOnlineAccount")
            {
                string entityValue = entityList.Where(x => x.EntityType == "LOCATION").Select(y => y.EntityValue).FirstOrDefault().ToLower().Trim();
                if (entityValue == "chennai")
                {
                    respondIntent = db.ChatIntent.Where(x => x.ParentId == intent.ChatIntentId && x.IntentName.ToLower().Contains("yes")).FirstOrDefault();
                }
                else
                {
                    respondIntent = db.ChatIntent.Where(x => x.ParentId == intent.ChatIntentId && x.IntentName.ToLower().Contains("no")).FirstOrDefault();
                }
            }
            else
            {
                respondIntent = intent;
            }

            httpContext.Session[intent.ChatIntentId.ToString()] = null;
            List <ChatSessionEntity> recognizedList = db.ChatSessionEntity.Where(x => x.SessionId == sessionId).ToList();

            foreach (ChatSessionEntity chatEntity in recognizedList)
            {
                chatEntity.EntityType    = "recog";
                chatEntity.NotRecognized = false;
            }
            db.SaveChanges();

            foreach (var recog in entityList)
            {
                respondIntent.Response = respondIntent.Response.Replace(recog.EntityName, recog.EntityValue);
            }
            return(new KeyValuePair <int, string>(respondIntent.ChatIntentId, respondIntent.Response));
        }
예제 #2
0
        public ActionResult StartChat()
        {
            AskMeCommon   common  = new AskMeCommon("hello", 0);
            List <string> suggest = common.GetSuggestionList(0);
            ChatSession   session = new ChatSession();

            session.SessionStart = DateTime.UtcNow;
            session.SessionUd    = "";
            session.isAuth       = false;
            db.ChatSession.Add(session);
            db.SaveChanges();
            int    sessionId     = session.SessionId;
            string finalResponse = db.ChatIntent.Where(x => x.ChatIntentId == 0).Select(y => y.Response).FirstOrDefault();
            var    result        = new { node = 0, response = finalResponse, suggest = suggest, sessionId = sessionId };

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
예제 #3
0
        public bool LogFailureResponse()
        {
            ChatFailureResponse fail = new ChatFailureResponse();

            try
            {
                fail.QuestionByUser = Message;
                fail.ParentId       = Node;
                fail.Reviewed       = false;
                fail.UpdatedDate    = DateTime.UtcNow;
                db.ChatFailureResponse.Add(fail);
                db.SaveChanges();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #4
0
        public JsonResult QuestionUpdate(AskOperation askOperation)
        {
            AskDto             ask       = askOperation.ask;
            string             operation = askOperation.Operation;
            ChatIntentQuestion question  = new ChatIntentQuestion();
            bool changed = false;

            ask.UpdatedDate = DateTime.UtcNow;
            try
            {
                if (operation == "a")
                {
                    question.QuestionDesc = ask.QuestionDesc;
                    question.ChatIntentId = ask.ChatIntentId;
                    question.UpdatedDate  = DateTime.Now;
                    db.ChatIntentQuestion.Add(question);
                }
                else if (operation == "u")
                {
                    question = db.ChatIntentQuestion.Where(x => x.QuestionId == ask.QuestionId).FirstOrDefault();
                    question.QuestionDesc = ask.QuestionDesc;
                    question.ChatIntentId = ask.ChatIntentId;
                    question.UpdatedDate  = DateTime.Now;
                }
                else
                {
                    question = db.ChatIntentQuestion.Where(x => x.QuestionId == ask.QuestionId).FirstOrDefault();
                    db.ChatIntentQuestion.Attach(question);
                    db.ChatIntentQuestion.Remove(question);
                }
                changed = true;
                db.SaveChanges();
                return(Json(changed, JsonRequestBehavior.AllowGet));
            }

            catch (Exception e)

            {
                Console.WriteLine(e.Message);
                return(Json(changed, JsonRequestBehavior.AllowGet));
            }
        }
예제 #5
0
        public ChatResponseDto GetChatResponse(int sessionId, string message, int node)
        {
            ChatDatabaseModel db          = new ChatDatabaseModel();
            ChatResponseDto   responseDto = new ChatResponseDto();

            #region Declaration
            AskMeContentManager contentManager = new AskMeContentManager();
            AskMeCommon         common         = new AskMeCommon(message, node);
            string            finalResponse    = string.Empty;
            string            phoneNumber      = string.Empty;
            ChatIntent        responseIntent   = new ChatIntent();
            List <ChatIntent> intentList       = db.ChatIntent.ToList();
            List <ChatEntity> entityList       = db.ChatEntity.ToList();
            ChatSession       chatSession      = db.ChatSession.Where(x => x.SessionId == sessionId).FirstOrDefault();

            #endregion

            #region Prerequsite Check
            if (common.CheckEmptyMessage())
            {
                finalResponse = "Sorry i did not understand";
                List <string> suggestforEmpty = common.GetSuggestionList();
                responseDto.Node       = node;
                responseDto.Message    = finalResponse;
                responseDto.Suggestion = suggestforEmpty;
                return(responseDto);
            }
            else
            {
                message = message.Trim();
            }
            #endregion

            #region Has Atleast Unrecognized Entity
            var hasEntity = (from inte in intentList
                             join ent in entityList on inte.ChatIntentId equals ent.ChatIntentId
                             where inte.ChatIntentId == node
                             select inte).ToList();

            // Has Atleast Unrecognized Entity

            var hasUnrecognizedEntity = db.ChatSessionEntity.Where(x => x.SessionId == sessionId && x.NotRecognized).ToList();

            if (hasEntity.Count > 0 && hasUnrecognizedEntity.Count > 0)
            {
                List <ChatSessionEntity> entityRecognized = hasUnrecognizedEntity;

                AskMeEntityExtraction      entityMatch       = new AskMeEntityExtraction(message, node, sessionId);
                KeyValuePair <int, string> oneEntityResponse = new KeyValuePair <int, string>();
                if (entityRecognized.Where(x => x.EntityType.Contains("PASSCODE")).Any())
                {
                    oneEntityResponse = entityMatch.CheckIfAtleastOneEntitywithPasscode(entityRecognized);
                }
                else
                {
                    oneEntityResponse = entityMatch.CheckIfAtleastOneEntityHasValue(entityRecognized);
                }
                finalResponse = replaceParam(oneEntityResponse.Value);
                node          = oneEntityResponse.Key;

                List <string> suggestforEntity = common.GetSuggestionList(node);

                responseDto.Node       = node;
                responseDto.Message    = finalResponse;
                responseDto.Suggestion = suggestforEntity;
                return(responseDto);
            }

            #endregion

            #region Has one Intent with an Entity
            var hasOneChildIntent = (from inte in intentList
                                     where inte.ParentId == node
                                     select inte).ToList();


            // Has one Intent with an Entity
            if (hasOneChildIntent.Count == 1)
            {
                ChatIntent childIntent = hasOneChildIntent.FirstOrDefault();

                var hasOneIntentwithEntity = (from ent in entityList
                                              where ent.ChatIntentId == childIntent.ChatIntentId
                                              select ent).ToList();
                if (hasOneIntentwithEntity.Count > 0)
                {
                    List <ChatSessionEntity> entityRecognized = new List <ChatSessionEntity>();
                    List <ChatEntity>        possibleEntities = hasOneIntentwithEntity;
                    foreach (ChatEntity entity in possibleEntities)
                    {
                        ChatSessionEntity recognized = new ChatSessionEntity();
                        recognized.SessionId     = sessionId;
                        recognized.EntityType    = entity.EntityType;
                        recognized.EntityName    = entity.EntityName;
                        recognized.EntityValue   = entity.EntityDescription;
                        recognized.NotRecognized = true;
                        entityRecognized.Add(recognized);
                        db.ChatSessionEntity.Add(recognized);
                    }
                    db.SaveChanges();
                    AskMeEntityExtraction      entityMatch       = new AskMeEntityExtraction(message, childIntent.ChatIntentId, sessionId);
                    KeyValuePair <int, string> oneEntityResponse = new KeyValuePair <int, string>();
                    if (entityRecognized.Where(x => x.EntityType.Contains("PASSCODE")).Any())
                    {
                        oneEntityResponse = entityMatch.CheckIfAtleastOneEntitywithPasscode(entityRecognized);
                    }
                    else
                    {
                        oneEntityResponse = entityMatch.CheckIfAtleastOneEntityHasValue(entityRecognized);
                    }
                    finalResponse = replaceParam(oneEntityResponse.Value);
                    node          = oneEntityResponse.Key;

                    List <string> suggestforEntity = common.GetSuggestionList(node);

                    responseDto.Node       = node;
                    responseDto.Message    = finalResponse;
                    responseDto.Suggestion = suggestforEntity;
                    return(responseDto);
                }
            }
            #endregion

            #region Main Channel
            AskMeChannel channel = new AskMeChannel(message, node, sessionId);
            responseIntent = channel.ChatInitializer();
            bool hasRedirect = (responseIntent.RedirectIntent.HasValue) ? true : false;

            node          = responseIntent.ChatIntentId;
            finalResponse = responseIntent.Response;

            if (responseIntent.NeedAuth)
            {
                bool hasBeenAuth = chatSession.isAuth && chatSession.SessionStart.AddDays(2) > DateTime.UtcNow;
                if (!hasBeenAuth)
                {
                    ChatIntent authIntent = intentList.Where(x => x.IntentName.ToLower().Contains("auth")).FirstOrDefault();
                    node          = authIntent.ChatIntentId;
                    finalResponse = authIntent.Response;
                    chatSession.IntentBeforeAuth = responseIntent.ChatIntentId;
                    db.SaveChanges();
                }
            }

            if (hasRedirect) // askpaymentspecialist)
            {
                int        redirectIntentId = Convert.ToInt32(responseIntent.RedirectIntent);
                ChatIntent redirectIntent   = intentList.Where(x => x.ChatIntentId == redirectIntentId).FirstOrDefault();
                node          = redirectIntent.ChatIntentId;
                finalResponse = redirectIntent.Response;
            }

            finalResponse = replaceParam(finalResponse);

            // Get Suggestions List
            List <string> suggest = common.GetSuggestionList(node);
            responseDto.Node       = node;
            responseDto.Message    = finalResponse;
            responseDto.Suggestion = suggest;
            return(responseDto);

            #endregion
        }
예제 #6
0
        public ChatIntent GetEntityforIntentfromNLP(ChatIntent responseIntent)
        {
            AskMeEntityExtracttionNLP nlp = new AskMeEntityExtracttionNLP();
            List <EntityRecognition>  entityListMessage    = new List <EntityRecognition>();
            List <EntityRecognized>   entityListRecognized = new List <EntityRecognized>();
            List <ChatEntity>         entityListDb         = db.ChatEntity.Where(x => x.ChatIntentId == Node).ToList();

            if (entityListDb.Where(x => x.EntityType.Contains("PERSON")).Any() || entityListDb.Where(x => x.EntityType.Contains("LOCATION")).Any())
            {
                entityListMessage = nlp.ExtractionChannel(Message);
            }
            HttpContext httpContext = HttpContext.Current;

            foreach (ChatEntity entity in entityListDb)
            {
                if (entity.EntityType.ToUpper().Contains("PERSON") && entityListMessage.Count > 0)
                {
                    var hasRecognition = entityListMessage.Where(x => x.EntityType == "PERSON");
                    if (hasRecognition.Any())
                    {
                        EntityRecognition recog      = hasRecognition.FirstOrDefault();
                        EntityRecognized  recognized = new EntityRecognized();
                        recognized.EntityType    = entity.EntityType;
                        recognized.EntityName    = entity.EntityName;
                        recognized.EntityValue   = recog.EntityValue;
                        recognized.NotRecognized = false;
                        entityListRecognized.Add(recognized);
                        if (entity.EntityName.ToUpper().Contains("USERNAME"))
                        {
                            httpContext.Session["authUser"] = recog.EntityValue;
                            ChatSession chatSession = db.ChatSession.Where(x => x.SessionId == SessionId).FirstOrDefault();
                            chatSession.SessionUd = recog.EntityValue;
                            db.SaveChanges();
                        }
                    }
                }
                else if (entity.EntityType.ToUpper().Contains("LOCATION") && entityListMessage.Count > 0)
                {
                    var hasRecognition = entityListMessage.Where(x => x.EntityType == "LOCATION");
                    if (hasRecognition.Any())
                    {
                        EntityRecognition recog      = hasRecognition.FirstOrDefault();
                        EntityRecognized  recognized = new EntityRecognized();
                        recognized.EntityType    = entity.EntityType;
                        recognized.EntityName    = entity.EntityName;
                        recognized.EntityValue   = recog.EntityValue;
                        recognized.NotRecognized = false;
                        entityListRecognized.Add(recognized);
                    }
                }
                else if (entity.EntityType.ToUpper().Contains("NUMBER") && entityListMessage.Count > 0)
                {
                    string numericValue = new String(Message.Where(Char.IsDigit).ToArray());
                    if (numericValue.Length > 0)
                    {
                        EntityRecognized recognized = new EntityRecognized();
                        recognized.EntityType    = entity.EntityType;
                        recognized.EntityName    = entity.EntityName;
                        recognized.EntityValue   = numericValue;
                        recognized.NotRecognized = false;
                        entityListRecognized.Add(recognized);
                    }
                }
                else
                {
                    EntityRecognized recognized = new EntityRecognized();
                    recognized.EntityType    = entity.EntityType;
                    recognized.EntityName    = entity.EntityName;
                    recognized.EntityValue   = entity.EntityDescription;
                    recognized.NotRecognized = true;
                    entityListRecognized.Add(recognized);
                }
            }

            string noEntityMessage = string.Empty;

            List <ChatSessionEntity> sessionEntityList = new List <ChatSessionEntity>();

            foreach (EntityRecognized recognized in entityListRecognized)
            {
                ChatSessionEntity sessionEntity = new ChatSessionEntity();
                sessionEntity.SessionId     = SessionId;
                sessionEntity.EntityType    = recognized.EntityType;
                sessionEntity.EntityName    = recognized.EntityName;
                sessionEntity.EntityValue   = recognized.EntityValue;
                sessionEntity.NotRecognized = recognized.NotRecognized;
                db.ChatSessionEntity.Add(sessionEntity);
                sessionEntityList.Add(sessionEntity);
                if (recognized.NotRecognized)
                {
                    noEntityMessage = noEntityMessage + recognized.EntityValue + " ";
                }
            }
            db.SaveChanges();
            if (noEntityMessage.Length > 1)
            {
                Message = noEntityMessage;
                httpContext.Session[Node.ToString()] = entityListRecognized;
            }
            else
            {
                AskMeOnlineApi             onlineApi      = new AskMeOnlineApi();
                ChatIntent                 forOnline      = db.ChatIntent.Where(x => x.ChatIntentId == Node).FirstOrDefault();
                KeyValuePair <int, string> onlineResponse = onlineApi.OnlineApiChannelforNLP(forOnline, sessionEntityList);
                Node    = onlineResponse.Key;
                Message = onlineResponse.Value;
            }

            responseIntent.ChatIntentId = Node;
            responseIntent.Response     = Message;
            return(responseIntent);
        }