public KeyValuePair <int, string> CheckIfAtleastOneEntitywithPasscode(List <ChatSessionEntity> entityListRecognized) { ChatEntity entity = db.ChatEntity.Where(x => x.ChatIntentId == Node && x.EntityType.Contains("PASSCODE")).FirstOrDefault(); HttpContext httpContext = HttpContext.Current; ChatSessionEntity entityRecognized = entityListRecognized.Where(x => x.EntityType.Contains("PASSCODE")).FirstOrDefault(); if (Message.ToLower() == "apollo") { ChatIntent passwordIntent = db.ChatIntent.Where(x => x.ChatIntentId == Node).FirstOrDefault(); Message = passwordIntent.Response; Node = passwordIntent.ChatIntentId; httpContext.Session[passwordIntent.ChatIntentId.ToString()] = null; httpContext.Session["auth"] = true; ChatSession chatSession = db.ChatSession.Where(x => x.SessionId == SessionId).FirstOrDefault(); chatSession.isAuth = true; ChatSessionEntity chatSessionEntity = db.ChatSessionEntity.Where(x => x.SessionEntId == entityRecognized.SessionEntId).FirstOrDefault(); chatSessionEntity.EntityType = "recog"; chatSessionEntity.NotRecognized = false; db.SaveChanges(); int authIntentId = (chatSession.IntentBeforeAuth != null) ? chatSession.IntentBeforeAuth.Value : 0; if (authIntentId != 0) { ChatIntent authIntent = db.ChatIntent.Where(x => x.ChatIntentId == authIntentId).FirstOrDefault(); Node = authIntent.ChatIntentId; Message = authIntent.Response; } return(new KeyValuePair <int, string>(Node, Message)); } else { httpContext.Session[Node.ToString()] = entityListRecognized; return(new KeyValuePair <int, string>(Node, entity.EntityDescription)); } }
public KeyValuePair <int, string> OnlineApiChannel(ChatIntent intent, string extractEntityName, string extractedEntityValue) { string response = string.Empty; ChatIntent respondIntent = new ChatIntent(); if (intent.IntentName == "ChangeAddressMeIntent") { if (extractedEntityValue.ToLower().Trim() == "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 == "NoOnlineAccount") { if (extractedEntityValue.ToLower().Trim() == "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 { return(new KeyValuePair <int, string>(intent.ChatIntentId, intent.Response)); } respondIntent.Response = respondIntent.Response.Replace(extractEntityName, extractedEntityValue); return(new KeyValuePair <int, string>(respondIntent.ChatIntentId, respondIntent.Response)); }
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)); }
public KeyValuePair <int, string> GetEntityforIntent(ChatIntent intent) { string entity = string.Empty; List <ChatEntity> entityList = new List <ChatEntity>(); // db.ChatEntity.Where(z => z.ChatIntentId == chatIntentId.ToString()).ToList(); List <string> questionList = db.ChatIntentQuestion.Where(x => x.ChatIntentId == intent.ChatIntentId && x.QuestionDesc.ToLower().Contains("entity")).Select(y => y.QuestionDesc).ToList(); TextInfo textInfo = new CultureInfo("en-US", false).TextInfo; KeyValuePair <int, string> responseIntent = new KeyValuePair <int, string>(); int foundEntityMatch = 0; string extractedEntityName = string.Empty; string extractedEntityValue = string.Empty; foreach (string question in questionList) { string textPriortoEntityinQuestion = string.Empty; string textPriortoEntityinMessage = string.Empty; int iStart = question.IndexOf("["); int iEnd = question.IndexOf("]"); if (iStart != -1 && iEnd != -1) { extractedEntityName = question.Substring(iStart + 1, iEnd - iStart - 1); textPriortoEntityinQuestion = question.Substring(0, iStart); textPriortoEntityinMessage = Message.Substring(0, iStart); LevenshteinDistance dist = new LevenshteinDistance(); int matching = dist.Compute(textPriortoEntityinQuestion.ToLower(), textPriortoEntityinMessage.ToLower()); if (matching <= 6) { int iStartSpace = textPriortoEntityinMessage.LastIndexOf(" "); int iEndSpace = Message.IndexOf(",", textPriortoEntityinMessage.Length); if (iEndSpace == -1) { iEndSpace = Message.Length; } extractedEntityValue = Message.Substring(iStartSpace, iEndSpace - iStartSpace); extractedEntityValue = textInfo.ToTitleCase(extractedEntityValue); AskMeOnlineApi online = new AskMeOnlineApi(); responseIntent = online.OnlineApiChannel(intent, extractedEntityName, extractedEntityValue); foundEntityMatch = foundEntityMatch + 1; break; } } } if (foundEntityMatch == 0) { ChatIntent noEntityFound = db.ChatIntent.Where(x => x.ParentId == intent.ChatIntentId).Where(x => x.IntentName.Contains("Entity")).FirstOrDefault(); HttpContext httpContext = HttpContext.Current; httpContext.Session["EntityIssue"] = extractedEntityName; responseIntent = new KeyValuePair <int, string>(noEntityFound.ChatIntentId, noEntityFound.Response); } return(responseIntent); }
public JsonResult IntentUpdate(IntentOperation intOperation) { ChatIntentDto intent = intOperation.intent; string operation = intOperation.Operation; ChatIntent finalIntent = new ChatIntent(); bool changed = false; intent.UpdatedDate = DateTime.UtcNow; try { if (operation == "a") { finalIntent.IntentName = intent.IntentName; finalIntent.IntentDescription = intent.IntentDescription; finalIntent.Response = intent.Response; finalIntent.ParentId = intent.ParentId; finalIntent.NeedAuth = intent.NeedAuth; finalIntent.IsRedirect = intent.IsRedirect; finalIntent.RedirectIntent = intent.RedirectIntent; finalIntent.UpdatedDate = DateTime.Now; db.ChatIntent.Add(finalIntent); } else if (operation == "u") { finalIntent = db.ChatIntent.Where(x => x.ChatIntentId == intent.ChatIntentId).FirstOrDefault(); finalIntent.IntentName = intent.IntentName; finalIntent.IntentDescription = intent.IntentDescription; finalIntent.Response = intent.Response; finalIntent.ParentId = intent.ParentId; finalIntent.NeedAuth = intent.NeedAuth; finalIntent.IsRedirect = intent.IsRedirect; finalIntent.RedirectIntent = intent.RedirectIntent; finalIntent.UpdatedDate = DateTime.Now; } else { finalIntent = db.ChatIntent.Where(x => x.ChatIntentId == intent.ChatIntentId).FirstOrDefault(); db.ChatIntent.Attach(finalIntent); db.ChatIntent.Remove(finalIntent); } changed = true; db.SaveChanges(); return(Json(changed, JsonRequestBehavior.AllowGet)); } catch (Exception e) { Console.WriteLine(e.Message); return(Json(changed, JsonRequestBehavior.AllowGet)); } }
private ChatIntent UpdateIntent(int node, string message, ChatIntent respondIntent) { if (respondIntent != null) { respondIntent.ChatIntentId = node; respondIntent.Response = message; } else { respondIntent = new ChatIntent(); respondIntent.ChatIntentId = node; respondIntent.Response = "Sorry I did not understand, Please enter one of the suggestions"; } return(respondIntent); }
//ChaT Initializer public ChatIntent ChatInitializer() { finalResponse = ChatResponseMain(); if (finalResponse.Response == contentManager.NoIntentMatchedResponse) { var hasParentNode = db.ChatIntent.Where(x => x.ChatIntentId == Node && Node != 0); if (hasParentNode.Any()) { Node = hasParentNode.Select(y => y.ParentId).FirstOrDefault(); finalResponse = ChatInitializer(); } } if (finalResponse.Response == contentManager.NoIntentMatchedResponse) { common.LogFailureResponse(); } return(finalResponse); }
public KeyValuePair <int, string> PutEntityforIntent(ChatIntent intent) { KeyValuePair <int, string> responseIntent = new KeyValuePair <int, string>(); HttpContext httpContext = HttpContext.Current; if (httpContext.Session["EntityIssue"] != null) { string extractedEntityName = httpContext.Session["EntityIssue"].ToString(); string extractedEntityValue = Message; AskMeOnlineApi online = new AskMeOnlineApi(); responseIntent = online.OnlineApiChannel(intent, extractedEntityName, extractedEntityValue); httpContext.Session["EntityIssue"] = null; } else { responseIntent = new KeyValuePair <int, string>(intent.ChatIntentId, "Sorry, i did not understand"); } return(responseIntent); }
public bool CheckIfRedirect(ChatIntent intent, List <ChatIntent> intentList) { if (intent.IsRedirect) { return(true); } ChatIntent parent = intentList.Where(x => x.ChatIntentId == intent.ParentId).FirstOrDefault(); if (parent != null) { if (parent.IsRedirect) { return(true); } if (parent.ChatIntentId == 0) { return(false); } } return(false); // CheckIfRedirect(parent, intentList); }
public EntityIdentifiedDto HasOneChildIntentWithOneEntity(ChatEntity entity, ChatIntent intent) { var hasEntity = db.ChatEntity.Where(x => x.ChatIntentId == Node); EntityIdentifiedDto dto = new EntityIdentifiedDto(); string finalResponse = string.Empty; HttpContext context = HttpContext.Current; finalResponse = intent.Response; dto.EntityName = entity.EntityName; dto.EntityValue = EntityPatternMatch(entity); dto.MatchConfidence = "high"; dto.ChatResponse = finalResponse; dto = SetEntityinResponse(dto); //phoneNumber = (context.Session["phone"] != null ? context.Session["phone"].ToString() : "980 000 000"); //message = (context.Session["datetime"] != null ? context.Session["datetime"].ToString() : "12:00 AM EST"); //finalResponse = finalResponse.Replace("paramphonenumber", phoneNumber); //finalResponse = finalResponse.Replace("paramappointmenttime", message); //dto.ChatResponse = finalResponse; return(dto); }
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 }
//ChaT Bot Reponse Main Entry public ChatIntent ChatResponseMain() { string responseMessage = contentManager.NoIntentMatchedResponse; TFIDF getVocab = new TFIDF(); Dictionary <string, string> reponseDict = new Dictionary <string, string>(); List <ChatIntent> intentListAll = db.ChatIntent.ToList(); ChatIntent responseIntent = intentListAll.Where(x => x.ChatIntentId == 0).FirstOrDefault(); #region 1.CheckIntentGreetingOrGoodbye if (hiBye.Greet()) { return(UpdateIntent(Node, contentManager.GreetResponse, responseIntent)); } else if (hiBye.GoodBye()) { return(UpdateIntent(Node, contentManager.GoodbyeResponse, responseIntent)); } #endregion List <ChatIntent> intentList = (from intention in intentListAll where intention.ChatIntentId > 2 && intention.ParentId == Node select intention).ToList(); #region 2.CheckIntentFullMatchbySuggestion KeyValuePair <int, bool> fullMatch = suggestionMatch.FullSuggestionMatch(intentList); if (fullMatch.Value) { ChatIntent fullMatchIntent = intentList.Where(x => x.ChatIntentId == fullMatch.Key).FirstOrDefault(); responseMessage = fullMatchIntent.Response; var hasEntity = (from ent in db.ChatEntity where ent.ChatIntentId == fullMatchIntent.ChatIntentId select ent); if (hasEntity.Any()) { AskMeEntityExtraction entity = new AskMeEntityExtraction(Message, fullMatchIntent.ChatIntentId, SessionId); return(entity.GetEntityforIntentfromNLP(fullMatchIntent)); } return(fullMatchIntent); } KeyValuePair <int, bool> partialMatch = suggestionMatch.PartialSuggestionMatch(intentList); if (partialMatch.Value) { ChatIntent partialMatchIntent = intentList.Where(x => x.ChatIntentId == partialMatch.Key).FirstOrDefault(); responseMessage = partialMatchIntent.Response; var hasEntity = (from ent in db.ChatEntity where ent.ChatIntentId == partialMatchIntent.ChatIntentId select ent); if (hasEntity.Any()) { AskMeEntityExtraction entity = new AskMeEntityExtraction(Message, partialMatchIntent.ChatIntentId, SessionId); return(entity.GetEntityforIntentfromNLP(partialMatchIntent)); } return(partialMatchIntent); } #endregion List <string> vocabList = getVocab.GetVocabulary(Message); if (vocabList.Count == 0) { return(UpdateIntent(Node, contentManager.NoIntentMatchedResponse, responseIntent)); } if (Message.ToLower() == "yes" || Message.ToLower() == "no") { return(UpdateIntent(Node, contentManager.NoIntentMatchedResponse, responseIntent)); } #region 3.TFIDF Match Process SimilarityCalculator similarityCalculator = new SimilarityCalculator(); List <ChatIntentQuestion> questionList = db.ChatIntentQuestion.ToList(); Dictionary <int, double> scoreDict = new Dictionary <int, double>(); foreach (ChatIntentQuestion question in questionList) { double compare = similarityCalculator.CompareString(Message, question.QuestionDesc, 1); KeyValuePair <int, double> score = new KeyValuePair <int, double>(question.ChatIntentId, compare); if (scoreDict.ContainsKey(score.Key)) { if (scoreDict[score.Key] < compare) { scoreDict[score.Key] = compare; } } else { scoreDict.Add(score.Key, score.Value); } } if (scoreDict.Where(x => x.Value > 0.45).Any()) { int maxScoreChatIntentId = scoreDict.OrderByDescending(x => x.Value).Select(y => y.Key).FirstOrDefault(); ChatIntent maxIntent = intentListAll.Where(x => x.ChatIntentId == maxScoreChatIntentId).FirstOrDefault(); Node = maxScoreChatIntentId; var hasEntity = (from ent in db.ChatEntity where ent.ChatIntentId == maxIntent.ChatIntentId select ent); if (hasEntity.Any()) { AskMeEntityExtraction entity = new AskMeEntityExtraction(Message, maxIntent.ChatIntentId, SessionId); return(entity.GetEntityforIntentfromNLP(maxIntent)); } //KeyValuePair<int, string> responseIntent = GetEntityforIntent(Node, maxIntent.Response); return(maxIntent); } else if (scoreDict.Where(x => x.Value >= 0.23).Any()) { List <int> possibeMatch = scoreDict.OrderByDescending(x => x.Value).Where(x => x.Value >= 0.23).Select(y => y.Key).ToList(); responseMessage = contentManager.IntentPossibleMatchedResponse; foreach (int match in possibeMatch) { responseMessage = responseMessage + ", "; string suggestion = intentListAll.Where(x => x.ChatIntentId == match).Select(y => y.IntentDescription).FirstOrDefault(); responseMessage = responseMessage + suggestion; } responseMessage = responseMessage + ", " + contentManager.IntentSuggestionResponse; return(UpdateIntent(Node, responseMessage, responseIntent)); } #endregion #region 4.Probable Match Process KeyValuePair <string, bool> probableMatchCorrect = zPossibleMatch.ProbableMatchCorrectSpelling(vocabList, intentListAll); if (probableMatchCorrect.Value) { common.LogFailureResponse(); responseMessage = probableMatchCorrect.Key; return(UpdateIntent(Node, responseMessage, responseIntent)); } KeyValuePair <string, bool> probableMatchTypo = zPossibleMatch.ProbableMatchTypoError(vocabList, intentListAll); if (probableMatchTypo.Value) { common.LogFailureResponse(); responseMessage = probableMatchTypo.Key; return(UpdateIntent(Node, responseMessage, responseIntent)); } #endregion #region 4.Synonym Match Process KeyValuePair <string, bool> synMatch = synonymMatch.SynonymMatch(vocabList, intentListAll); if (synMatch.Value) { common.LogFailureResponse(); responseMessage = synMatch.Key; return(UpdateIntent(Node, responseMessage, responseIntent)); } #endregion if (responseIntent != null) { responseIntent.ChatIntentId = Node; responseIntent.Response = responseMessage; } else { responseIntent = new ChatIntent(); responseIntent.ChatIntentId = Node; responseIntent.Response = "Sorry I did not understand, Please enter one of the suggestions"; } return(responseIntent); }
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)); }
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); }