示例#1
0
        public NLUResult Parse(string query)
        {
            if (int.TryParse(query, out int num))
            {
                NLUResult result = new NLUResult(num);
                return(result);
            }
            else
            {
                if (query.Trim().ToLower() == "q")
                {
                    NLUResult result = new NLUResult(NLUResultType.QUITDIALOG);
                    return(result);
                }
                else
                {
                    string intentName = nluStore.DetectIntent(query);

                    if (intentName == null)
                    {
                        NLUResult result = new NLUResult(NLUResultType.UNKNOWN);
                        return(result);
                    }
                    else
                    {
                        NLUResult result = ParseIntentEntity(intentName, query);
                        return(result);
                    }
                }
            }
        }
示例#2
0
        private NLUResult ParseIntentEntity(string intentName, string query)
        {
            List <NLUEntity> entities = nluDF.DetectEntities(intentName, query);

            List <AttributePair> attributes = nluDF.ParseAttributes(intentName, entities);

            HashSet <string> relationTypeSet = new HashSet <string>();

            if (entities != null && entities.Count > 0)
            {
                foreach (NLUEntity entity in entities)
                {
                    if (entity.GetEntityType() == "RelationType")
                    {
                        relationTypeSet.Add(entity.GetEntityValue());
                    }
                }
            }

            NLUResult result = new NLUResult(intentName, entities, attributes, relationTypeSet);

            return(result);
        }
示例#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));
            }
        }