public List <EntityRecognition> getEntityList(string result) { List <EntityRecognition> entity = new List <EntityRecognition>(); int currentlength = 0; while (currentlength < result.Length) { int StartIndex = result.IndexOf("<PERSON>", currentlength); int EndIndex = result.IndexOf("</PERSON>", currentlength); if (StartIndex != -1 || EndIndex != -1) { StartIndex = StartIndex + 8; string entityValue = (result.Substring(StartIndex, EndIndex - StartIndex)); EntityRecognition recog = new EntityRecognition(); recog.EntityType = "PERSON"; recog.EntityValue = entityValue; entity.Add(recog); currentlength = currentlength + EndIndex + 1; } else { break; } } currentlength = 0; while (currentlength < result.Length) { int StartIndex = result.IndexOf("<LOCATION>", currentlength); int EndIndex = result.IndexOf("</LOCATION>", currentlength); if (StartIndex != -1 || EndIndex != -1) { StartIndex = StartIndex + 10; string entityValue = (result.Substring(StartIndex, EndIndex - StartIndex)); EntityRecognition recog = new EntityRecognition(); recog.EntityType = "LOCATION"; recog.EntityValue = entityValue; entity.Add(recog); currentlength = currentlength + EndIndex + 1; } else { break; } } return(entity); }
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); }