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); }
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 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; }
private BotContext GetValidContext(string userId) { ContextInfo ci = cManager.GetContext(userId); if (ci == null) { return(null); } DateTime currentTime = DateTime.Now; if (currentTime.Subtract(ci.lastUpdatedTime).TotalMinutes > 480) // 8 hours { return(null); } else { if (ci.type == ContextType.PSAContext) { PSAContext psaContext = JsonConvert.DeserializeObject <PSAContext>(ci.jsonString); if (IsValid(psaContext)) { return(psaContext); } else { return(null); } } else if (ci.type == ContextType.ExamSuitContext) { ExamSuitContext esContext = JsonConvert.DeserializeObject <ExamSuitContext>(ci.jsonString); return(esContext); } else if (ci.type == ContextType.ScoreContext) { ScoreContext scContext = JsonConvert.DeserializeObject <ScoreContext>(ci.jsonString); return(scContext); } else { TaskFlowContext tfContext = JsonConvert.DeserializeObject <TaskFlowContext>(ci.jsonString); return(tfContext); } } }
private bool IsValid(PSAContext context) { DateTime nowTime = DateTime.Now; if (context.validTime == null) { return(false); } else if (nowTime.Subtract(context.validTime).TotalMinutes > 30) { return(false); } else if (string.IsNullOrWhiteSpace(context.CourseName)) { return(false); } else { return(true); } }
private PSAContext GetValidPSAContext(string userId) { ContextInfo ci = cManager.GetContext(userId); if (ci == null) { return(null); } DateTime currentTime = DateTime.Now; if (currentTime.Subtract(ci.lastUpdatedTime).TotalMinutes > 30) { return(null); } else { PSAContext psaContext = JsonConvert.DeserializeObject <PSAContext>(ci.jsonString); return(psaContext); } }
public override string GetAnswer(PSAContext context) { TimeRange range = context.timeRange; string type = "Course"; switch (context.Intent) { case "AskCourseSchedule": type = "Course"; break; case "AskDelivery": type = "Delivery"; break; case "AskTrainingSchedule": type = "Training"; break; } string typeCondition = " Type=N'" + type + "' "; string dateCondition = "Date >= '" + range.startDate.ToString("yyyy-MM-dd") + "' "; if (range.endDate != null) { dateCondition += " AND Date <= '" + range.endDate.ToString("yyyy-MM-dd") + "' "; } string weekdaystr = DatetimeUtils.GetTodayWeekDayString(range.startDate); string weekdayCondition = " Weekday=N'" + weekdaystr + "'"; string timesectionCondition = null; if (context.Intent == "AskCourseSchedule" && range.startDate != null && range.endDate != null && range.endDate != range.startDate) { if (range.endDate.Subtract(range.startDate).Hours < 24) { if (range.startDate.Hour == 6) { timesectionCondition = " TimeSector=N'上午' "; } else if (range.startDate.Hour == 12) { timesectionCondition = " TimeSector=N'下午' "; } } } string[] columns = { "Date", "Weekday", "TimeSector", "StartTime", "EndTime", "Name", "Condition", "IsActive" }; string activeCondition = " IsActive=1 "; List <string> conditionsFirstRound = new List <string>(); conditionsFirstRound.Add(activeCondition); conditionsFirstRound.Add(typeCondition); conditionsFirstRound.Add(dateCondition); if (!string.IsNullOrWhiteSpace(timesectionCondition)) { conditionsFirstRound.Add(timesectionCondition); } List <string> conditionsSecondRound = new List <string>(); conditionsSecondRound.Add(activeCondition); conditionsSecondRound.Add(typeCondition); conditionsSecondRound.Add(weekdayCondition); if (!string.IsNullOrWhiteSpace(timesectionCondition)) { conditionsSecondRound.Add(timesectionCondition); } List <Dictionary <string, string> > results = dbChecker.SearchTable(tableName, columns, conditionsFirstRound); if (results == null || results.Count == 0) { results = dbChecker.SearchTable(tableName, columns, conditionsSecondRound); } string result = ""; switch (context.Intent) { case "AskCourseSchedule": if (results == null || results.Count == 0) { result += weekdaystr + "没有安排课程"; break; } result += "课程安排\r\n"; foreach (Dictionary <string, string> dict in results) { string line = ""; if (!string.IsNullOrWhiteSpace(dict["Date"])) { line += DateTime.Parse(dict["Date"]).ToString("yyyy-MM-dd") + " "; } line += dict["Weekday"] + dict["TimeSector"] + " " + dict["Name"]; result += line + "\r\n"; } break; case "AskDelivery": if (results == null || results.Count == 0) { result += weekdaystr + "不上学,不需要家长接送"; break; } foreach (Dictionary <string, string> dict in results) { string line = ""; if (!string.IsNullOrWhiteSpace(dict["Date"])) { line += DateTime.Parse(dict["Date"]).ToString("yyyy-MM-dd") + " "; } line += dict["Weekday"] + dict["TimeSector"] + "," + dict["Condition"] + " " + dict["StartTime"] + " " + dict["Name"]; result += line + "\r\n"; } break; case "AskTrainingSchedule": if (results == null || results.Count == 0) { result += weekdaystr + "没有兴趣小组"; break; } result += "兴趣小组\r\n"; foreach (Dictionary <string, string> dict in results) { string line = ""; if (!string.IsNullOrWhiteSpace(dict["Date"])) { line += DateTime.Parse(dict["Date"]).ToString("yyyy-MM-dd") + " "; } line += dict["Weekday"] + " " + dict["Name"]; result += line + "\r\n"; } break; } return(result); }
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, ref PSAContext psaContext) { string outofScopeStr = "真抱歉,北宝还只是一个宝宝,懂的东西太少,您的问题我没法回答。\r\n不过我会记录下来,尽快解决。谢谢!"; string GreetingStr = "你好,我叫北宝,是个会说话的小机器人。\r\n我可以告诉你咱们班的课程和兴趣小组安排,还可以帮您记着今天老师布置的任务。\r\n有什么问题就请问吧[微笑]\r\n加我为好友好,单独发送“我要做题”给我,可以做小测验,发送“性格测试”可以测试职场性格"; string answer = ""; switch (psaContext.Intent) { case "AskTest": answer = "输入“我要做题”四个字,并选择要测试的科目,即可开始进行一次小测验。北宝出一道题,请您答一道题,最后北宝会给出本次小测验的结果。"; break; case "AskExam": answer = "DICS测试是1928年美国心理学家威廉.莫尔顿.马斯顿创建的一套性格测试标准。\r\n目前很多大企业和猎头公司招聘人才时会用到。大家不妨了解一下自己行为模式。\r\n输入“性格测试”四个字,开始测试!"; 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": answer = GreetingStr; break; case "Praise": answer = "谢谢您的夸奖,我会继续努力的。"; break; case "AskSwimmingClass": answer = "游泳课已经结束,本学期不再有游泳课。"; break; default: answer = outofScopeStr; break; } if (string.IsNullOrWhiteSpace(answer)) { foreach (IntentSrv srv in this.srvs) { string singleAnswer = srv.GetAnswer(psaContext); answer += singleAnswer; } this.srvs.Clear(); } return(answer); }
private void SetTestModeContext(ref PSAContext context) { context.Intent = DOTESTINTENT; }
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 override string GetAnswer(PSAContext context /*, LUInfo luInfo*/) { DateTime now = DateTime.Now; if (context.timeRange != null && context.timeRange.startDate != null) { now = context.timeRange.startDate; } List <string> conditions = new List <string>(); string dateStr = context.timeRange.startDate.ToString("yyyy-MM-dd"); if (context.timeRange.startDate == context.timeRange.endDate) { conditions.Add("DateTime=N'" + dateStr + "'"); } else { dateStr = context.timeRange.startDate.ToString("yyyy-MM-dd") + " ~ " + context.timeRange.endDate.ToString("yyyy-MM-dd"); conditions.Add("Timestamp>='" + context.timeRange.startDate.ToString("yyyy-MM-dd") + "'"); conditions.Add("Timestamp<'" + context.timeRange.endDate.AddDays(1).ToString("yyyy-MM-dd") + "'"); } string[] columns = { "DateTime", "Notifier", "Content" }; List <Dictionary <string, string> > results = dbChecker.SearchTable(tableName, columns, conditions); StringBuilder builder = new StringBuilder(); if (results == null || results.Count == 0) { builder.AppendLine(dateStr + " 没有通知"); } else { foreach (Dictionary <string, string> dict in results) { string line = ""; line += dict["DateTime"] + " "; if (dict["Notifier"] != "YJL SOEVPM") { line += dict["Notifier"] + "通知: "; } string content = dict["Content"]; if (!string.IsNullOrWhiteSpace(content)) { content = content.Replace("<br/>", "\r\n"); } line += content; builder.AppendLine(line); } } string queryresult = builder.ToString(); return(queryresult); }
public abstract string GetAnswer(PSAContext context);
public override string GetAnswer(PSAContext context /*, LUInfo luInfo*/) { throw new NotImplementedException(); }