Ejemplo n.º 1
0
        public KeyValuePair <int, string> CheckIfAtleastOneEntityHasValue(List <ChatSessionEntity> entityListRecognized)
        {
            AskMeEntityExtracttionNLP nlp = new AskMeEntityExtracttionNLP();
            List <EntityRecognition>  entityListMessage = new List <EntityRecognition>();
            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 (ChatSessionEntity entityRecognized in entityListRecognized)
            {
                if (entityRecognized.NotRecognized)
                {
                    if (entityRecognized.EntityName.ToUpper().Contains("PERSON"))
                    {
                        if (entityListMessage.Where(x => x.EntityType == "PERSON").Any())
                        {
                            entityRecognized.EntityValue   = entityListMessage.Where(x => x.EntityType == "PERSON").Select(Y => Y.EntityValue).FirstOrDefault();
                            entityRecognized.EntityType    = "PERSON";
                            entityRecognized.NotRecognized = false;
                            if (entityRecognized.EntityName.ToUpper().Contains("USERNAME"))
                            {
                                httpContext.Session["authUser"] = entityRecognized.EntityValue;
                                ChatSession chatSession = db.ChatSession.Where(x => x.SessionId == SessionId).FirstOrDefault();
                                chatSession.SessionUd = entityRecognized.EntityValue;
                                db.SaveChanges();
                            }
                        }
                    }
                    else if (entityRecognized.EntityName.ToUpper().Contains("LOCATION"))
                    {
                        if (entityListMessage.Where(x => x.EntityType == "LOCATION").Any())
                        {
                            entityRecognized.EntityValue   = entityListMessage.Where(x => x.EntityType == "LOCATION").Select(Y => Y.EntityValue).FirstOrDefault();
                            entityRecognized.EntityType    = "LOCATION";
                            entityRecognized.NotRecognized = false;
                        }
                    }
                    else if (entityRecognized.EntityName.ToUpper().Contains("NUMBER"))
                    {
                        string numericValue = new String(Message.Where(Char.IsDigit).ToArray());
                        if (numericValue.Length > 0)
                        {
                            entityRecognized.EntityValue   = numericValue;
                            entityRecognized.EntityType    = "NUMBER";
                            entityRecognized.NotRecognized = false;
                        }
                    }
                }
            }

            string noEntityMessage = string.Empty;

            foreach (ChatSessionEntity 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;
                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();
                return(onlineApi.OnlineApiChannelforNLP(forOnline, entityListRecognized));
            }

            return(new KeyValuePair <int, string>(Node, Message));
        }
Ejemplo n.º 2
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);
        }