示例#1
0
        public QueryResult GenerateSlotMessage(DialogSlot slot)
        {
            string question = slot.question;

            if (this.runningMode == RUNNINGMODE.DEVELOPMENT)
            {
                foreach (OptionItem item in slot.items)
                {
                    question += item.seqNo.ToString() + ". " + item.vertex.name + "\n";
                }
            }

            QueryResult result = new QueryResult(true, question, ResponseItemType.Option);

            result.AddResponseItems(slot.items);

            return(result);
        }
示例#2
0
        private QueryResult ResolveInvalidOptionInput(ContextManager contextMgmt)
        {
            if (contextMgmt.DureInvalidInput())
            {
                //Repeat the previous question
                QueryResult       result = new QueryResult(true, "请选择正确的选项:\n" + contextMgmt.GetQuestion(), ResponseItemType.Option);
                List <OptionItem> items  = new List <OptionItem>();

                if (contextMgmt.GetStatus() == DialogStatus.SLOTFILLING)
                {
                    DialogSlot currentSlot = contextMgmt.GetSlot(contextMgmt.GetCurrentSlotSeqNum());
                    foreach (OptionItem item in currentSlot.items)
                    {
                        items.Add(item);
                    }
                }
                else
                {
                    foreach (int seqNo in contextMgmt.GetCandidates().Keys)
                    {
                        OptionItem item = new OptionItem();
                        item.seqNo  = seqNo;
                        item.vertex = contextMgmt.GetCandidates()[seqNo];

                        items.Add(item);
                    }
                }

                result.AddResponseItems(items);

                return(result);
            }
            else
            {
                QueryResult responseContent = this.msgGenerator.GenerateErrorMessage("连续" + (contextMgmt.GetMaxDurationTime()) + "次输入无效选项,退出当前对话。");
                contextMgmt.ExitDialog();
                return(responseContent);
            }
        }
示例#3
0
        private QueryResult ResponseDialog(NLUResult nlu, ContextManager contextMgmt)
        {
            string intent = nlu.GetIntent();

            if (string.IsNullOrWhiteSpace(intent))
            {
                intent = contextMgmt.GetIntent();
            }
            else
            {
                contextMgmt.SetIntent(intent);
                contextMgmt.SetScenarioName(intent);
            }

            try
            {
                if (nlu.GetAttributes() != null && nlu.GetAttributes().Count() > 0)
                {
                    foreach (AttributePair attribute in nlu.GetAttributes())
                    {
                        if (attribute != null)
                        {
                            contextMgmt.AddAttributeFilterCondition(attribute);
                        }
                    }
                }

                LogInformation(_log.Here(), "DialogStatus", contextMgmt.GetStatus().ToString());

                if (contextMgmt.GetStatus() == DialogStatus.PENDING)
                {
                    if (nlu.GetType() == NLUResultType.NORMAL)
                    {
                        QueryResult responseContent = dQuerier.SearchVertexes(contextMgmt, nlu.GetEntities());

                        if (responseContent.success)
                        {
                            contextMgmt.SetIntent(intent);
                            contextMgmt.SaveQuestion(responseContent.responseMessage);
                            contextMgmt.RefreshDurationTime();
                        }


                        return(responseContent);
                    }
                    else
                    {
                        QueryResult responseContent = this.msgGenerator.GenerateErrorMessage("无法识别意图。");
                        contextMgmt.ExitDialog();
                        return(responseContent);
                    }
                }
                else if (contextMgmt.GetStatus() == DialogStatus.SLOTFILLING)
                {
                    QueryResult responseContent;
                    if (nlu.GetType() == NLUResultType.NUMBER)
                    {
                        int        lastStep = contextMgmt.GetCurrentSlotSeqNum();
                        DialogSlot lastSlot = contextMgmt.GetSlot(lastStep);
                        int        option   = nlu.GetOption();

                        try
                        {
                            string attributeName  = lastSlot.correspondingAttribute;
                            string attributeValue = lastSlot.answerValues[option - 1];

                            contextMgmt.AddAttributeFilterCondition(new AttributePair(attributeName, attributeValue));
                            responseContent = dQuerier.HandleSlotFilling(contextMgmt, nlu.GetRelationTypeSet());

                            contextMgmt.SaveQuestion(responseContent.responseMessage);
                            contextMgmt.RefreshDurationTime();
                        }
                        catch (Exception e)
                        {
                            LogError(_log.Here(), e);
                            responseContent = ResolveInvalidOptionInput(contextMgmt);
                        }
                    }
                    else
                    {
                        responseContent = ResolveInvalidOptionInput(contextMgmt);
                    }

                    return(responseContent);
                }
                else //if (context.GetStatus() == DialogStatus.INPROCESS)
                {
                    QueryResult responseContent;

                    if (nlu.GetType() == NLUResultType.NUMBER)
                    {
                        var candidates = contextMgmt.GetCandidates();
                        try
                        {
                            Vertex vertex = candidates[nlu.GetOption()];

                            if (vertex.isLeaf())
                            {
                                responseContent = msgGenerator.GenerateEndVertexMessage(vertex);
                                contextMgmt.ExitDialog();
                            }
                            else
                            {
                                responseContent = dQuerier.GetChildren(contextMgmt, vertex, nlu.GetRelationTypeSet());
                            }

                            contextMgmt.SaveQuestion(responseContent.responseMessage);
                            contextMgmt.RefreshDurationTime();
                        }
                        catch (Exception e)
                        {
                            LogError(_log.Here(), e);
                            responseContent = ResolveInvalidOptionInput(contextMgmt);
                        }
                    }
                    else
                    {
                        responseContent = ResolveInvalidOptionInput(contextMgmt);
                    }

                    return(responseContent);
                }
            }
            catch (Exception e)
            {
                LogError(_log.Here(), e);
                QueryResult responseContent = this.msgGenerator.GenerateErrorMessage("无法识别意图。");
                contextMgmt.ExitDialog();

                return(responseContent);
            }
        }
示例#4
0
        private QueryResult ResponseDialog(NLUResult nlu, ContextManager contextMgmt)
        {
            string intent       = nlu.GetIntent();
            string scenarioName = intent;


            if (string.IsNullOrWhiteSpace(intent))
            {
                intent       = contextMgmt.GetIntent();
                scenarioName = contextMgmt.GetSecnarioName();
            }
            else
            {
                contextMgmt.SetIntent(intent);
                contextMgmt.SetScenarioName(scenarioName);
            }

            try
            {
                DataManager kgMgmt = new DataManager();

                if (nlu.GetAttributes() != null && nlu.GetAttributes().Count() > 0)
                {
                    foreach (AttributePair attribute in nlu.GetAttributes())
                    {
                        if (attribute != null)
                        {
                            contextMgmt.AddAttributeFilterCondition(attribute);
                        }
                    }
                }

                LogInformation(_log.Here(), "DialogStatus", contextMgmt.GetStatus().ToString());

                if (contextMgmt.GetStatus() == DialogStatus.PENDING)
                {
                    if (nlu.GetType() == NLUResultType.NORMAL)
                    {
                        string startVertexName = null;

                        foreach (NLUEntity entity in nlu.GetEntities())
                        {
                            if (entity.GetEntityType() == "NodeName")
                            {
                                startVertexName = entity.GetEntityValue();
                                break;
                            }
                        }

                        contextMgmt.SetStartVertexName(startVertexName);

                        Vertex vertex = kgMgmt.SearchGraph(contextMgmt.GetStartVertexName(), contextMgmt.GetSecnarioName(), contextMgmt.GetSavedAttributes());

                        QueryResult responseContent;

                        if (vertex == null)
                        {
                            responseContent = GenerateErrorMessage("无法查找到对应节点,请确定输入的限定条件正确", contextMgmt);
                        }
                        if (vertex.isLeaf())
                        {
                            responseContent = GenerateEndVertexMessage(vertex, contextMgmt);
                        }
                        else
                        {
                            List <DialogSlot> validSlots = new List <DialogSlot>();
                            List <DialogSlot> slots      = kgMgmt.GetConfiguredSlots(scenarioName);

                            if (slots != null && slots.Count() > 0)
                            {
                                List <AttributePair> attributes = contextMgmt.GetSavedAttributes();
                                if (attributes != null && attributes.Count() > 0)
                                {
                                    foreach (DialogSlot slot in slots)
                                    {
                                        bool isFilled = false;
                                        foreach (AttributePair attribute in attributes)
                                        {
                                            string attributeName = attribute.attributeName;
                                            if (slot.correspondingAttribute == attributeName)
                                            {
                                                isFilled = true;
                                                break;
                                            }
                                        }

                                        if (!isFilled)
                                        {
                                            validSlots.Add(slot);
                                        }
                                    }
                                }
                                else
                                {
                                    validSlots = slots;
                                }
                            }

                            if (validSlots.Count() == 0)
                            {
                                contextMgmt.StartDialog();
                                responseContent = GetChildren(contextMgmt, kgMgmt, vertex, nlu.GetRelationTypeSet(), scenarioName);
                            }
                            else
                            {
                                contextMgmt.SetSlots(validSlots);
                                contextMgmt.EnterSlotFilling();

                                responseContent = GenerateSlotMessage(validSlots[0], contextMgmt);
                            }
                        }

                        if (responseContent.success)
                        {
                            contextMgmt.SetIntent(intent);
                            contextMgmt.SetScenarioName(scenarioName);
                            contextMgmt.SaveQuestion(responseContent.responseMessage);
                            contextMgmt.RefreshDurationTime();
                        }

                        return(responseContent);
                    }
                    else
                    {
                        return(GenerateErrorMessage("无法识别意图。", contextMgmt));
                    }
                }
                else if (contextMgmt.GetStatus() == DialogStatus.SLOTFILLING)
                {
                    QueryResult responseContent;
                    if (nlu.GetType() == NLUResultType.NUMBER)
                    {
                        int        lastStep = contextMgmt.GetCurrentSlotSeqNum();
                        DialogSlot lastSlot = contextMgmt.GetSlot(lastStep);
                        int        option   = nlu.GetOption();

                        try
                        {
                            string attributeName  = lastSlot.correspondingAttribute;
                            string attributeValue = lastSlot.answerValues[option - 1];

                            contextMgmt.AddAttributeFilterCondition(new AttributePair(attributeName, attributeValue));
                            responseContent = HandleSlotFilling(contextMgmt, kgMgmt, nlu.GetRelationTypeSet(), scenarioName);

                            contextMgmt.SaveQuestion(responseContent.responseMessage);
                            contextMgmt.RefreshDurationTime();
                        }
                        catch (Exception e)
                        {
                            LogError(_log.Here(), e);
                            responseContent = ResolveInvalidOptionInput(contextMgmt);
                        }
                    }
                    else
                    {
                        responseContent = ResolveInvalidOptionInput(contextMgmt);
                    }

                    return(responseContent);
                }
                else //if (context.GetStatus() == DialogStatus.INPROCESS)
                {
                    QueryResult responseContent;

                    if (nlu.GetType() == NLUResultType.NUMBER)
                    {
                        var candidates = contextMgmt.GetCandidates();
                        try
                        {
                            Vertex vertex = candidates[nlu.GetOption()];
                            responseContent = GoForward(contextMgmt, kgMgmt, vertex, nlu.GetRelationTypeSet(), scenarioName);

                            contextMgmt.SaveQuestion(responseContent.responseMessage);
                            contextMgmt.RefreshDurationTime();
                        }
                        catch (Exception e)
                        {
                            LogError(_log.Here(), e);
                            responseContent = ResolveInvalidOptionInput(contextMgmt);
                        }
                    }
                    else
                    {
                        responseContent = ResolveInvalidOptionInput(contextMgmt);
                    }

                    return(responseContent);
                }
            }
            catch (Exception e)
            {
                LogError(_log.Here(), e);
                return(GenerateErrorMessage("无法识别意图。", contextMgmt));
            }
        }