示例#1
0
        private TaskFlowContext updateTFContext(ref TaskFlowContext context, string utterance)
        {
            context.IsInTesting = true;
            context.UserInput   = utterance;

            return(context);
        }
示例#2
0
        public string StartTest(ref TaskFlowContext context)
        {
            string firstText = "";

            if (context.CourseInTest == "数学")
            {
                Init(ref context);

                firstText += "下面我们就要开始答题了,每一道题目显示出来以后,请直接输入答案。\r\n比如题目是:“2 + 3 = ”,那么直接输入“5”。\r\n准备好了吗?现在开始做题啦。\r\n";
                firstText += this.GetNextText(ref context);
            }
            else if (context.CourseInTest == "英语")
            {
                Init(ref context);

                firstText += "下面我们就要开始答题了,每一个中文单词显示出来以后,请直接输入对应英文单词。\r\n比如题目是:“苹果”,那么直接输入“apple”。\r\n准备好了吗?现在开始做题啦。\r\n";
                firstText += this.GetNextText(ref context);
            }
            else
            {
                context.IsInTesting = true;

                CourseSelectionTaskItem selectionTask = new CourseSelectionTaskItem();
                context.courseSelectionItem = selectionTask;

                firstText += selectionTask.question;
            }

            return(firstText);
        }
示例#3
0
        private string AskBack(ref TaskFlowContext context)
        {
            context.AskBackTime++;

            if (context.AskBackTime > ConfigurationData.ASKBACKLIMITTIME)
            {
                context.IsInTesting = false;

                if (context.CourseInTest != null)
                {
                    return("测试中止\r\n" + GenerateResult(ref context));
                }
                else
                {
                    return("测试中止\r\n想重新开始测试请输入“我要做题”。");
                }
            }

            if (context.CourseInTest == "数学")
            {
                return("做题的过程中请只输入答案的数字,不要有其他字符。\r\n如果还要继续做题请重新输入这道题答案,否则请随便输入一个字母。\r\n" + GetNextText(ref context));
            }
            else if (context.CourseInTest == "英语")// 英语
            {
                return("做题的过程中请只输入答案的英语单词,不要有其他字符。\r\n如果还要继续做题请重新输入这道题答案,否则请随便输入一个数字或汉字。\r\n" + GetNextText(ref context));
            }
            else
            {
                return("请仅输入对应科目的字母,不要有其他字符\r\n" + context.courseSelectionItem.question);
            }
        }
示例#4
0
        public string Answer(string userId, ref TaskFlowContext tfContext)
        {
            string answer    = "";
            string userInput = tfContext.UserInput;

            switch (tfContext.Intent)
            {
            case "DoTest":

                if (!tfContext.IsInTesting)
                {
                    answer = this.taskSrv.StartTest(ref tfContext);
                }
                else
                {
                    if (tfContext.CourseInTest == null)
                    {
                        answer = this.taskSrv.GetCourseInTest(ref tfContext, userInput);
                    }
                    else
                    {
                        answer = this.taskSrv.ReceiveUserAnswer(ref tfContext, userInput);
                    }
                }

                break;
            }

            return(answer);
        }
示例#5
0
 private void SetTestResult(ref TaskFlowContext context, string result)
 {
     context.taskItems.ElementAt(context.currentIndex++).userAnswer = result;
     if (context.currentIndex == context.taskItems.Count)
     {
         context.IsInTesting = false;
     }
 }
示例#6
0
        private string GenerateResult(ref TaskFlowContext context)
        {
            context.IsInTesting = false;

            int correctNum         = 0;
            int testedNum          = 0;
            int TotalTestItemCount = 0;

            List <string> errorItems = new List <string>();

            foreach (TestItem item in context.taskItems)
            {
                if (item.userAnswer != null && item.correctResult != null && item.userAnswer.Trim().ToLower() == item.correctResult.Trim().ToLower())
                {
                    correctNum++;
                    testedNum++;
                }
                else if (item.userAnswer != null)
                {
                    string errorLine = item.question + " " + item.userAnswer + " (X) " + "[" + item.correctResult + "]";
                    errorItems.Add(errorLine);
                    testedNum++;
                }
                TotalTestItemCount++;
            }

            double percentage = (double)correctNum / (double)testedNum;


            string text = "";

            if (testedNum == TotalTestItemCount)
            {
                int score = (int)(percentage * 100);
                text += "你得了" + score.ToString() + "分。";
                if (score >= 80)
                {
                    text += "真是太棒了!继续努力哦[微笑]\r\n";
                }
                else if (score >= 60)
                {
                    text += "还是不错的,不过最好加强训练了。[微笑]\r\n";
                }
                else
                {
                    text += "宝宝要主动练习哦![微笑]\r\n";
                }
            }

            text += "你一共完成" + testedNum.ToString() + "道题," + correctNum.ToString() + "道正确。";
            if (errorItems.Count > 0)
            {
                text += "\r\n做错的题如下:\r\n";
                text += string.Join("\r\n", errorItems);
            }
            return(text);
        }
示例#7
0
        private TaskFlowContext InitTFContext(string userId, TestLUInfo currentLUInfo)
        {
            TaskFlowContext tfContext = new TaskFlowContext(userId);

            tfContext.Intent       = currentLUInfo.Intent.intent;
            tfContext.CourseName   = DatetimeUtils.GetCourseName(currentLUInfo);
            tfContext.currentIndex = 0;

            return(tfContext);
        }
示例#8
0
        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);
                }
            }
        }
示例#9
0
 private string GetNextText(ref TaskFlowContext context)
 {
     if (context.IsInTesting)
     {
         return(GetFormula(ref context));
     }
     else
     {
         if (context.CourseInTest != null)
         {
             return(GenerateResult(ref context));
         }
         else
         {
             context.IsInTesting = false;
             return("测试中止\r\n想重新开始测试请输入“我要做题”。");
         }
     }
 }
示例#10
0
        public string ReceiveUserAnswer(ref TaskFlowContext context, string userAnswer)
        {
            bool askBack = false;

            if (string.IsNullOrWhiteSpace(userAnswer))
            {
                askBack = true;
            }
            else
            {
                userAnswer = userAnswer.Trim();

                string answer = userAnswer;

                if (!string.IsNullOrWhiteSpace(answer))
                {
                    answer = answer.Trim().ToLower();
                }

                string matchedRule = context.taskItems[context.currentIndex].answerMatchRule;

                Regex regex = new Regex(matchedRule);
                Match match = regex.Match(answer);

                if (match.Success)
                {
                    this.SetTestResult(ref context, answer);

                    return(this.GetNextText(ref context));
                }
                else
                {
                    askBack = true;
                }
            }

            if (askBack)
            {
                return(this.AskBack(ref context));
            }

            return(null);
        }
示例#11
0
        private TaskFlowContext GetValidTaskFlowContext(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
            {
                TaskFlowContext tfContext = JsonConvert.DeserializeObject <TaskFlowContext>(ci.jsonString);
                return(tfContext);
            }
        }
示例#12
0
        public string GetCourseInTest(ref TaskFlowContext context, string userAnswer)
        {
            bool askBack = false;

            if (string.IsNullOrWhiteSpace(userAnswer))
            {
                askBack = true;
            }
            else
            {
                string answer      = userAnswer.Trim().ToLower();
                string matchedRule = context.courseSelectionItem.answerMatchRule;

                Regex regex = new Regex(matchedRule);
                Match match = regex.Match(answer);

                if (match.Success)
                {
                    //this.SetTestResult(ref context, answer);
                    string matchedValue = match.Value;
                    string courseName   = context.courseSelectionItem.answerMap[matchedValue];
                    context.CourseInTest = courseName;

                    Init(ref context);

                    return(this.GetNextText(ref context));
                }
                else
                {
                    askBack = true;
                }
            }
            if (askBack)
            {
                return(this.AskBack(ref context));
            }

            return(null);
        }
示例#13
0
        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);
        }
示例#14
0
 public bool IsTesting(ref TaskFlowContext context)
 {
     return(context.IsInTesting);
 }
示例#15
0
        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);
        }
示例#16
0
 private void Init(ref TaskFlowContext context)
 {
     context.taskItems    = checker.GenerateTestItems(ConfigurationData.TASKITEMCOUNT, context.CourseInTest);
     context.currentIndex = 0;
     context.IsInTesting  = true;
 }
示例#17
0
        private string GetFormula(ref TaskFlowContext context)
        {
            string formula = context.taskItems.ElementAt(context.currentIndex).question;

            return(formula);
        }