Example #1
0
 public AskMeEntityExtraction(string message, int node, int sessionId)
 {
     this.Message   = textInfoMain.ToTitleCase(message);
     this.Node      = node;
     this.SessionId = sessionId;
     db             = new ChatDatabaseModel();
 }
Example #2
0
 public AskMeChannel(string message, int node, int sessionId)
 {
     this.Message    = message.ToLower();
     this.Node       = node;
     this.SessionId  = sessionId;
     db              = new ChatDatabaseModel();
     hiBye           = new AskMeHiBye(Message, Node);
     suggestionMatch = new AskMeSuggestionMatch(Message, Node);
     common          = new AskMeCommon(Message, Node);
     zPossibleMatch  = new AskMezPossibleMatch(Message, Node);
     synonymMatch    = new AskMeSynonymMatch(Message, Node);
 }
Example #3
0
 public HomeController()
 {
     db = new ChatDatabaseModel();
 }
Example #4
0
 public AskMeOnlineApi()
 {
     db = new ChatDatabaseModel();
 }
Example #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
        }
 public AskMeSuggestionMatch(string message, int node)
 {
     this.Message = message.ToLower();
     this.Node    = node;
     db           = new ChatDatabaseModel();
 }
Example #7
0
 public AskMeCommon(string message, int node)
 {
     this.Message = message.ToLower();
     this.Node    = node;
     db           = new ChatDatabaseModel();
 }
Example #8
0
 public AdminController()
 {
     db             = new ChatDatabaseModel();
     intentTreeDtos = new List <ChatIntentTreeDto>();
 }