public LUInfo Understand(string utterance) { DateTime startTime = DateTime.Now; string newUtterance = ruleStore.Preprocess(utterance); LUInfo info = this.luisClient.Query(newUtterance); string ruleBasedIntent = ruleStore.DetermineIntent(utterance); if (!string.IsNullOrWhiteSpace(ruleBasedIntent)) { info.Intent.intent = ruleBasedIntent; info.Intent.score = 1; } List <Entity> rulebasedEntities = ruleStore.ExtractSlot(utterance); if (rulebasedEntities.Count > 0) { info.EntityList.AddRange(rulebasedEntities); } DateTime endTime = DateTime.Now; LogUtils.Log(startTime, endTime, "[LU Service]"); return(info); }
public override string GetAnswer(WBContext context, LUInfo luInfo) { string response = DatetimeUtils.GetOutofScopeAnswer(); string location = context.Location; TimeRange range = context.timeRange; List <string> stations = DatetimeUtils.GetEntity(luInfo, "Station"); string station = null; if (stations != null && stations.Count > 0) { station = stations[0]; } if (range == null) { response = client.GetRealTimeAirQuality(location, station); } else if (!range.IsHourly) { response = client.GetPredictedSmogDaily(location, range.startDate, range.endDate); } else { response = client.GetPredictedSmogHourly(location, range.startDate, range.endDate); } return(response); }
private PSAContext initPSAContext(string userId, LUInfo luinfo) { PSAContext context = new PSAContext(userId); string intent = luinfo.Intent.intent; string coursename = DatetimeUtils.GetCourseName(luinfo); TimeRange range = DatetimeUtils.GetTimeRange(luinfo); context.Intent = intent; context.CourseName = coursename; context.timeRange = range; context.validTime = DateTime.Now; if (context.timeRange == null) { context.timeRange = new TimeRange(); DateTime now = DateTime.Now; context.timeRange.startDate = now; context.timeRange.endDate = now; } return(context); }
private LUInfo GetLUInfo(QueryResult result) { LUInfo luInfo = new LUInfo(); luInfo.SetIntent(result.Intents[0]); if (result.Entities != null) { foreach (EntityValue entityValue in result.Entities) { Entity entity = new Entity(); entity.type = entityValue.Type; entity.value = entityValue.Entity; if (entityValue.resolution != null) { string resolutionValue = null; if (!string.IsNullOrWhiteSpace(entityValue.resolution.date)) { resolutionValue = entityValue.resolution.date; } else { resolutionValue = entityValue.resolution.time; } entity.resolution = resolutionValue; } luInfo.AddEntity(entity); } } return(luInfo); }
private EEContext CreateContext(string userId, LUInfo luInfo) { EEContext context; string intent = luInfo.Intent.intent; if (intent == "DoExam") { context = examCStore.GetContext(userId); if (context == null) { context = new ExamContext(userId); } } else if (intent == "IntelligenceRoute") { context = irCStore.GetContext(userId); if (context == null) { context = new IRContext(userId); } } else { context = new QAContext(userId, intent, luInfo.EntityList); } return(context); }
private PSAContext updatePSAContext(ref PSAContext context, LUInfo luinfo) { string intent = luinfo.Intent.intent; if (!string.IsNullOrWhiteSpace(intent) && ValidPSAIntents.Contains(intent)) { context.Intent = intent; } string cn = DatetimeUtils.GetCourseName(luinfo); if (!string.IsNullOrWhiteSpace(cn)) { context.CourseName = cn; } TimeRange range = DatetimeUtils.GetTimeRange(luinfo); if (range != null) { context.timeRange = range; } return(context); }
public static void TestLuis() { LuisClient client = new LuisClient(); string query = "周末天气好吗"; LUInfo result = client.Query(query); Console.WriteLine(result.ToString()); }
//new DBContextManager(); public LUInfo Understand(string utterance, bool isSingleContact) { LUInfo currentLUInfo = null; if (string.IsNullOrWhiteSpace(utterance)) { currentLUInfo = new LUInfo(); currentLUInfo.Intent = new Intent(); currentLUInfo.Intent.intent = "Greeting"; currentLUInfo.EntityList = new List <Entity>(); } else { utterance = ruleStore.Preprocess(utterance); string ruleBasedIntent = ruleStore.DetermineIntent(utterance); if ((!isSingleContact) && (!string.IsNullOrWhiteSpace(ruleBasedIntent)) && ruleBasedIntent == "DoTest") { ruleBasedIntent = null; } if (!string.IsNullOrWhiteSpace(ruleBasedIntent)) { if (ruleBasedIntent == "DoTest") { currentLUInfo = new TestLUInfo(); } else if (ruleBasedIntent == "DoDISC") { currentLUInfo = new ExamLUInfo(); } else { currentLUInfo = new LUInfo(); } currentLUInfo.Intent.intent = ruleBasedIntent; currentLUInfo.Intent.score = 1; currentLUInfo.EntityList = new List <Entity>(); } if (currentLUInfo == null) { currentLUInfo = this.luisClient.Query(utterance); } List <Entity> rulebasedEntities = ruleStore.ExtractSlot(utterance); if (rulebasedEntities.Count > 0) { currentLUInfo.EntityList.AddRange(rulebasedEntities); } } return(currentLUInfo); }
public LUInfo Query(string query) { string encodedQuestion = HttpUtility.UrlEncode(query, System.Text.Encoding.UTF8); string url = rootURL + "?id=" + this.AppId + "&subscription-key=" + this.SubscriptionKey + "&q=" + encodedQuestion; var jsonString = client.Query(url); QueryResult result = JsonConvert.DeserializeObject <QueryResult>(jsonString); LUInfo luInfo = GetLUInfo(result); return(luInfo); }
private bool ContainsLocationDateOnly(LUInfo luInfo, string utterance) { if (luInfo.EntityList == null || luInfo.EntityList.Count == 0) { return(false); } bool containLocation = false; bool containDate = false; bool containOther = false; string entityStr = ""; foreach (Entity entity in luInfo.EntityList) { if (entity.type == "Location") { containLocation = true; entityStr += entity.value; } else if (entity.type == "builtin.datetime.date" || entity.type == "builtin.datetime.time") { containDate = true; entityStr += entity.value.Replace(" ", ""); } else { containOther = true; } } if (containLocation && containDate && !containOther) { if (entityStr.Trim().Length == utterance.Trim().Length) { return(true); } else if ((((double)entityStr.Trim().Length / (double)utterance.Length) > 0.8) && (luInfo.Intent.intent == "None")) { return(true); } else { return(false); } } else { return(false); } }
private void initContext(ref PSAContext context, LUInfo luinfo, string utterance) { string intent = luinfo.Intent.intent; string coursename = DatetimeUtils.GetCourseName(luinfo); TimeRange range = DatetimeUtils.GetTimeRange(luinfo); context.Intent = intent; context.CourseName = coursename; context.timeRange = range; context.validTime = DateTime.Now; }
public static string GetCourseName(LUInfo luInfo) { List <string> coursenames = GetEntity(luInfo, "CourseItem"); if (coursenames == null || coursenames.Count == 0) { return(null); } else { return(string.Join("", coursenames)); } }
public override string GetAnswer(WBContext context, LUInfo luInfo) { string location = context.Location; TimeRange timeRange = context.timeRange; string response = client.GetSuggestion(location, LifeSuggestionType.CarWashing); if (timeRange != null && timeRange.startDate > DateTime.Now) { response += "\r\n我们仅提供今天的洗车指数\r\n"; } return(response); }
public static TimeRange GetTimeRange(LUInfo luInfo) { DatePeriod datePeriod = GetDatePeriod(luInfo); TimePeriod timePeriod = GetTimePeriod(luInfo); if (datePeriod == null && timePeriod == null) { return(null); } else { TimeRange range = new TimeRange(); DateTime startDate; DateTime endDate; if (timePeriod == null) { startDate = DateTime.Parse(datePeriod.startDateStr); endDate = DateTime.Parse(datePeriod.endDateStr); } else { DateTime today = DateTime.Now.Date; startDate = today; endDate = today; if (datePeriod != null) { startDate = DateTime.Parse(datePeriod.startDateStr); endDate = DateTime.Parse(datePeriod.endDateStr); } startDate = startDate.AddHours(timePeriod.startHour); endDate = endDate.AddHours(timePeriod.endHour); range.IsHourly = true; } range.startDate = startDate; range.endDate = endDate; return(range); } }
private bool IsLocationOnly(LUInfo luinfo, string utterance) { string location = DatetimeUtils.GetLocation(luinfo, false); if (string.IsNullOrWhiteSpace(location)) { return(false); } if (location == utterance) { return(true); } else { return(false); } }
public override string GetAnswer(WBContext context, LUInfo luInfo) { string response = DatetimeUtils.GetOutofScopeAnswer(); string location = context.Location; TimeRange range = context.timeRange; if (range == null) { response = client.GetRestrictedNumber(location, null, null); } else { response = client.GetRestrictedNumber(location, range.startDate, range.endDate); } return(response); }
private void initContext(ref WBContext context, LUInfo luinfo, string utterance) { string intent = luinfo.Intent.intent; if ((IsLocationOnly(luinfo, utterance)) || ContainsLocationDateOnly(luinfo, utterance)) { intent = "DefaultIntent"; } string location = DatetimeUtils.GetLocation(luinfo, true); TimeRange range = DatetimeUtils.GetTimeRange(luinfo); context.Intent = intent; context.Location = location; context.timeRange = range; context.validTime = DateTime.Now; }
public EEContext Understand(string userId, string userInput) { EEContext context = cStore.GetContext(userId); if ((context == null) || (!IsValid(context))) { log.Info("No valid EEContxt exists. Create new EEContext."); LUInfo luinfo = this.Parse(userInput); context = CreateContext(userId, luinfo); cStore.SetContext(userId, context); } else { log.Info("EEContxt exists. \r\n" + JsonConvert.SerializeObject(context)); MergeContext(userId, userInput, ref context); cStore.SetContext(userId, context); } return(context); }
private LUInfo Parse(string utterance) { LUInfo currentLUInfo = null; if (string.IsNullOrWhiteSpace(utterance)) { currentLUInfo = new LUInfo(); currentLUInfo.Intent = new Intent(); currentLUInfo.Intent.intent = "Greeting"; currentLUInfo.EntityList = new List <Entity>(); } else { utterance = ruleStore.Preprocess(utterance); string ruleBasedIntent = ruleStore.DetermineIntent(utterance); if (!string.IsNullOrWhiteSpace(ruleBasedIntent)) { currentLUInfo = new LUInfo(); currentLUInfo.Intent = new Intent(); currentLUInfo.Intent.intent = ruleBasedIntent; currentLUInfo.Intent.score = 1; currentLUInfo.EntityList = new List <Entity>(); } if (currentLUInfo == null) { currentLUInfo = this.luisClient.Query(utterance); } List <Entity> rulebasedEntities = ruleStore.ExtractSlot(utterance); if (rulebasedEntities.Count > 0) { currentLUInfo.EntityList.AddRange(rulebasedEntities); } } return(currentLUInfo); }
public override string GetAnswer(WBContext context, LUInfo luInfo) { string response = DatetimeUtils.GetOutofScopeAnswer(); string location = context.Location; TimeRange range = context.timeRange; if (range == null) { response = client.GetRealTimeWeather(location); } else if (!range.IsHourly) { response = client.GetPredictedWeatherDaily(location, range.startDate, range.endDate); } else { response = client.GetPredictedWeatherHourly(location, range.startDate, range.endDate); } return(response + "\r\n"); }
private void MergeContext(string userId, string userInput, ref EEContext context) { if (context.intent == "DoExam") { return; } else if (context.intent == "IntelligenceRoute") { return; } else { LUInfo luinfo = this.Parse(userInput); context.intent = luinfo.Intent.intent; if (luinfo.EntityList != null && luinfo.EntityList.Count > 0) { ((QAContext)context).entities = luinfo.EntityList; } if (context.intent == "DoExam") { ExamContext savedContext = examCStore.GetContext(userId); if (savedContext != null && IsValid(savedContext)) { context = savedContext; } } else if (context.intent == "IntelligenceRoute") { IRContext savedContext = irCStore.GetContext(userId); if (savedContext != null && IsValid(savedContext)) { context = savedContext; } } } }
private void updateContext(ref WBContext context, LUInfo luinfo) { string intent = luinfo.Intent.intent; if (!string.IsNullOrWhiteSpace(intent) && ValidIntent.Contains(intent)) { context.Intent = intent; } string location = DatetimeUtils.GetLocation(luinfo, false); if (!string.IsNullOrWhiteSpace(location)) { context.Location = location; } TimeRange range = DatetimeUtils.GetTimeRange(luinfo); if (range != null) { context.timeRange = range; } }
public static List <string> GetEntity(LUInfo luInfo, string EntityType) { List <string> result = new List <string>(); if (luInfo.EntityList.Count != 0) { foreach (Entity entity in luInfo.EntityList) { if (entity.type == EntityType) { if (string.IsNullOrWhiteSpace(entity.resolution)) { result.Add(entity.value); } else { result.Add(entity.resolution); } } } } return(result); }
public static string GetLocation(LUInfo luInfo, bool useDefault) { List <string> provices = GetEntity(luInfo, "Province"); List <string> locations = GetEntity(luInfo, "Location"); if (locations == null || locations.Count == 0) { if (useDefault) { return(DefaultLocation); } else { return(null); } } if (provices != null && provices.Count > 0) { provices.AddRange(locations); locations = provices; } return(string.Join("", locations));//locations[locations.Count - 1]; }
public static DatePeriod GetDatePeriod(LUInfo luInfo) { List <string> dateareas = GetEntity(luInfo, "builtin.datetime.date"); return(DatetimeUtils.ConvertDateArea(dateareas)); }
public abstract string GetAnswer(WBContext context, LUInfo luInfo);
public string Answer(string userId, string utterance) { LUInfo luinfo = this.luController.Understand(utterance); WBContext context = contextStore.GetContext(userId); if (!context.IsValid()) { initContext(ref context, luinfo, utterance); } else { updateContext(ref context, luinfo); } switch (context.Intent) { case "DefaultIntent": this.srvs.Add(new WeatherSrv()); this.srvs.Add(new SmogSrv()); break; case "CarWashing": this.srvs.Add(new CarWashingSrv()); break; case "Weather": this.srvs.Add(new WeatherSrv()); break; case "Smog": this.srvs.Add(new SmogSrv()); break; case "RestrictedDriving": this.srvs.Add(new RestrictedDrivingSrv()); break; case "Cloth": this.srvs.Add(new ClothSrv()); break; default: break; } string answer = ""; DateTime startTime = DateTime.Now; foreach (IntentSrv srv in this.srvs) { string singleAnswer = srv.GetAnswer(context, luinfo); answer += singleAnswer; } this.srvs.Clear(); if (string.IsNullOrWhiteSpace(answer)) { answer = DatetimeUtils.GetOutofScopeAnswer(); } DateTime endTime = DateTime.Now; LogUtils.Log(startTime, endTime, "[Seniverse Service]"); return(answer); }
public string Answer(string userId, string utterance) { BotContext context = this.GetValidContext(userId); bool isSingleContact = this.IsSingleContact(ref utterance); if (context == null) { LUInfo luInfo = Understand(utterance, isSingleContact); if (luInfo.GetType() == typeof(TestLUInfo)) { context = InitTFContext(userId, (TestLUInfo)luInfo); } else if (luInfo.GetType() == typeof(ExamLUInfo)) { context = InitESContext(userId, (ExamLUInfo)luInfo); } else { context = initPSAContext(userId, luInfo); } cManager.CreateContext(context, userId); } else { if (context.type == ContextType.TaskFlowContext && isSingleContact) { TaskFlowContext tfContext = (TaskFlowContext)context; context = updateTFContext(ref tfContext, utterance); } else if (context.type == ContextType.ExamSuitContext && isSingleContact) { ExamSuitContext esContext = (ExamSuitContext)context; context = updateESContext(ref esContext, utterance); } else { LUInfo luInfo = Understand(utterance, isSingleContact); if (context.type == ContextType.PSAContext) { PSAContext psacontext = (PSAContext)context; context = updatePSAContext(ref psacontext, luInfo); } else { return("[Error]: Unknown Context Type."); } } } string answer = null; switch (context.type) { case ContextType.PSAContext: ChatTableEngine engine = new ChatTableEngine(); PSAContext psacontext = (PSAContext)context; answer = engine.Answer(userId, ref psacontext); cManager.UpdateContext(psacontext, userId); break; case ContextType.TaskFlowContext: TestEngine engineT = new TestEngine(); TaskFlowContext tfContext = (TaskFlowContext)context; answer = engineT.Answer(userId, ref tfContext); if (tfContext.IsInTesting) { cManager.UpdateContext(tfContext, userId); } else { cManager.RemoveContext(userId); } break; case ContextType.ExamSuitContext: ExamSuitContext esContext = (ExamSuitContext)context; DICSExamSrv taskSrv = new DICSExamSrv(); string userInput = esContext.UserInput; switch (esContext.status) { case ESStatus.Started: ExamSuitContext cachedContext = this.GetCachedESContext(userId); answer = taskSrv.StartTest(ref esContext, cachedContext); break; case ESStatus.Restarted: answer = taskSrv.ContinueOrRefresh(ref esContext, userInput); break; case ESStatus.OnGoing: answer = taskSrv.ReceiveUserAnswer(ref esContext, userInput); break; } if (esContext.status == ESStatus.Finished || esContext.status == ESStatus.Aborded) { cManager.RemoveContext(userId); } else if (esContext.status == ESStatus.Paused) { cManager.StoreESContext(esContext, userId); cManager.RemoveContext(userId); } else { cManager.UpdateContext(esContext, userId); } break; } return(answer); }
public string Answer(string userId, string utterance) { string answer = ""; string outofScopeStr = "真抱歉,北宝还只是一个宝宝,懂的东西太少,您的问题我没法回答。\r\n不过我会记录下来,尽快解决。谢谢!"; string GreetingStr = "你好,我叫北宝,是个会说话的小机器人。\r\n我可以告诉你咱们班的课程和兴趣小组安排,还可以帮您记着今天老师布置的任务。\r\n有什么问题就请问吧[微笑]"; if (string.IsNullOrWhiteSpace(utterance)) { return(GreetingStr); } PSAContext psaContext = this.GetValidPSAContext(userId); if (psaContext == null) { psaContext = new PSAContext(userId); cManager.CreateContext(psaContext, userId); } if (IsTestIntent(utterance)) { TaskFlowContext tfContext = new TaskFlowContext(userId); string courseName = null; string rule = "英语|数学"; Regex regex = new Regex(rule); Match match = regex.Match(utterance); if (match.Success) { courseName = match.Value; tfContext.CourseInTest = courseName; } this.SetTestModeContext(ref psaContext); answer = this.taskSrv.StartTest(ref tfContext); cManager.CreateContext(tfContext, userId); } else { utterance = utterance.Replace(ONEONEPREFIX, ""); TaskFlowContext tfContext = this.GetValidTaskFlowContext(userId); if (tfContext == null || !tfContext.IsInTesting) { LUInfo luinfo = this.luController.Understand(utterance); if (!IsValid(psaContext)) { initContext(ref psaContext, luinfo, utterance); } else { updateContext(ref psaContext, luinfo); } //default time is now if (psaContext.timeRange == null) { psaContext.timeRange = new TimeRange(); DateTime now = DateTime.Now; psaContext.timeRange.startDate = now; psaContext.timeRange.endDate = now; } } } switch (psaContext.Intent) { case "DoTest": if (string.IsNullOrWhiteSpace(answer)) { TaskFlowContext tfContext = this.GetValidTaskFlowContext(userId); if (tfContext.CourseInTest == null) { answer = this.taskSrv.GetCourseInTest(ref tfContext, utterance); if (answer != null) { cManager.UpdateContext(tfContext, userId); } } else { answer = this.taskSrv.ReceiveUserAnswer(ref tfContext, utterance); if (!tfContext.IsInTesting) { cManager.RemoveContext(userId); } else { cManager.UpdateContext(tfContext, userId); } } } break; case "AskCourseSchedule": case "AskDelivery": case "AskTrainingSchedule": this.srvs.Add(new ScheduleQuerySrv()); break; case "AskHomework": case "AskNotification": this.srvs.Add(new NotificationQuerySrv()); break; case "AskStoryContribution": answer = "只要愿意,每一个家长都可以来给孩子们讲故事。\r\n您如果有这个意愿,请在班级群里联系王沐宁妈妈。"; break; case "AskStorySchedule": answer = "一般情况下是每周四早上上课前有家长到班里讲故事。\r\n根据学校的具体安排,可能时间会变,请您注意老师的通知。"; break; case "Greeting": //"Greeting", "None" answer = GreetingStr; //"你好,我叫北宝,是个会说话的小机器人。我可以告诉你咱们班的课程和兴趣小组安排,还可以帮您记着今天老师布置的任务。有什么问题就请问吧[微笑]";//DatetimeUtils.GetOutofScopeAnswer(BotType.PSA); break; case "Praise": answer = "谢谢您的夸奖,我会继续努力的。"; break; case "AskSwimmingClass": answer = "游泳课从5月15日至6月8日,持续4周,第一、三、四周,周一至四体育课时间游泳。\r\n第二周游泳时间,请注意通知。"; break; default: //case "None": answer = outofScopeStr; //"真抱歉,北宝还只是一个宝宝,懂的东西太少,您的问题我没法回答。不过我会记录下来,尽快解决。谢谢!"; break; } if (string.IsNullOrWhiteSpace(answer)) { foreach (IntentSrv srv in this.srvs) { string singleAnswer = srv.GetAnswer(psaContext); answer += singleAnswer; } this.srvs.Clear(); } cManager.UpdateContext(psaContext, userId); return(answer); }
public static TimePeriod GetTimePeriod(LUInfo luInfo) { List <string> timeareas = GetEntity(luInfo, "builtin.datetime.time"); return(DatetimeUtils.ConvertTimeArea(timeareas)); }