示例#1
0
 public SwitchOnEntityVariables(DialogNode parentNode, EntityMatch entityMatch)
 {
     Type          = DialogNodeType.SwitchOnEntityVariables;
     ParentNode    = parentNode;
     ChildrenNodes = new List <DialogNode>();
     EntityMatch   = entityMatch;
 }
示例#2
0
        public void SetEntityMatchAndDisambiguationOptions(EntityMatch entityMatch, IList <string> disambiguationOptionsText, Dialog dialog)
        {
            EntityMatch = entityMatch;
            if (entityMatch == null)
            {
                dialog.LogMessage(LineNumber, MessageType.IncorrectPattern, "Disambiguation question \"" + QuestionText.Replace('\r', ' ').Replace('\n', ' ').Trim() + "\" without any entity match => dead end");
            }
            else
            {
                if (entityMatch.EntityVariable1 != null)
                {
                    entityMatch.EntityVariable1.AddDialogNodeReference(this, VariableReferenceType.Write);
                }
                if (entityMatch.EntityVariable2 != null)
                {
                    entityMatch.EntityVariable2.AddDialogNodeReference(this, VariableReferenceType.Write);
                }
            }

            DisambiguationOptions = new List <DisambiguationOption>();
            if (disambiguationOptionsText == null || disambiguationOptionsText.Count == 0)
            {
                dialog.LogMessage(LineNumber, MessageType.IncorrectPattern, "Disambiguation question \"" + QuestionText.Replace('\r', ' ').Replace('\n', ' ').Trim() + "\" doesn't provide options to guide the user");
            }
            else if (entityMatch != null)
            {
                foreach (var optionText in disambiguationOptionsText)
                {
                    var entityValuesMatchResult = dialog.MatchEntityValueInOptionText(this, entityMatch.Entity, optionText);
                    if (entityValuesMatchResult.EntityValues.Count == 0)
                    {
                        string message = "Option \"" + optionText + "\" for question \"" + QuestionText.Replace('\r', ' ').Replace('\n', ' ').Trim() + "\" can't be matched with any entity value for entity " + entityMatch.Entity.Name;
                        if (entityValuesMatchResult.ConceptSubstitutions != null)
                        {
                            foreach (var conceptSubstitution in entityValuesMatchResult.ConceptSubstitutions)
                            {
                                message += ",  " + conceptSubstitution.ToString();
                            }
                        }
                        var suggestedOptionText = entityMatch.Entity.SuggestEntityValueFromText(optionText);
                        if (suggestedOptionText != null && !suggestedOptionText.Equals(optionText, StringComparison.InvariantCultureIgnoreCase))
                        {
                            message += ", you may want to replace the option text with \"" + suggestedOptionText + "\"";
                        }
                        dialog.LogMessage(LineNumber, MessageType.InvalidReference, message);
                    }
                    else
                    {
                        var matchedEntityValue = entityValuesMatchResult.EntityValues[0];
                        DisambiguationOptions.Add(new DisambiguationOption(optionText, matchedEntityValue));
                        matchedEntityValue.AddDialogNodeReference(this);
                    }
                }
            }
        }
示例#3
0
 private static void WriteEntityMatch(XmlWriter xw, EntityMatch entityMatch)
 {
     xw.WriteStartElement("Match");
     xw.WriteAttributeString("Entity", entityMatch.EntityName);
     xw.WriteAttributeString("Variable1", entityMatch.EntityVariableName1);
     if (entityMatch.EntityVariableName2 != null)
     {
         xw.WriteAttributeString("Variable2", entityMatch.EntityVariableName2);
     }
     xw.WriteEndElement();
 }
示例#4
0
        public static DialogExecutionResult ExecuteUserInputNode(Dialog dialog, DialogNode dialogNode, string userInputText, DialogExecutionResult result)
        {
            IEnumerable <EntityMatch> entityMatches = null;

            if (dialogNode is MatchIntentAndEntities)
            {
                var intent = (MatchIntentAndEntities)dialogNode;
                entityMatches = intent.EntityMatches;
            }
            else if (dialogNode is DisambiguationQuestion)
            {
                var question = (DisambiguationQuestion)dialogNode;
                if (question.EntityMatch != null)
                {
                    entityMatches = new EntityMatch[] { question.EntityMatch };
                }
                result.AddUserInput(userInputText);
            }
            else
            {
                throw new ArgumentException("Dialog node must be of type MatchIntentAndEntities or DisambiguationQuestion");
            }

            EntityValuesMatchResult matchResult = null;

            if (entityMatches != null)
            {
                // Try to match entity values in the questions
                var entities = entityMatches.Select(entityMatch => entityMatch.Entity);
                matchResult = EntityValuesMatcher.MatchEntityValues(entities, userInputText, dialog.ConceptsSynonyms, dialog.ConceptsRegex);

                // Store the matched entity values in their assigned variables
                foreach (var entityValuesGroup in matchResult.EntityValues.GroupBy(ev => ev.Entity))
                {
                    var entityMatch = entityMatches.Where(em => em.Entity == entityValuesGroup.Key).First();
                    int matchIndex  = 0;
                    foreach (var entityValue in entityValuesGroup)
                    {
                        if (matchIndex == 0 && entityMatch.EntityVariableName1 != null)
                        {
                            result.VariablesValues[entityMatch.EntityVariableName1] = entityValue.Name;
                        }
                        else if (matchIndex == 1 && entityMatch.EntityVariableName2 != null)
                        {
                            result.VariablesValues[entityMatch.EntityVariableName2] = entityValue.Name;
                        }
                        matchIndex++;
                    }
                }
                ExecuteVariableAssignments(dialogNode, result.VariablesValues);
            }

            // Store the result of this execution
            var nodeExecution = new MatchEntitiesNodeExecution(dialogNode, matchResult);

            if (result.ExecutionResult != null && result.ExecutionResult.DialogNode == dialogNode)
            {
                result.DialogNodesExecutionPath.Remove(result.ExecutionResult);
            }
            result.AddDialogNodeExecution(nodeExecution);

            // Traverse the children nodes
            SelectChildNode(dialog, result.VariablesValues, dialogNode, null, result);

            return(result);
        }
示例#5
0
 internal void AddEntityMatch(EntityMatch entityMatch)
 {
     EntityMatches.Add(entityMatch);
 }
        internal void LinkEntityMatchToEntityAndDialogVariables(DialogNode dialogNode, EntityMatch entityMatch)
        {
            entityMatch.Entity = Entities[entityMatch.EntityName];
            entityMatch.Entity.AddDialogNodeReference(dialogNode);

            if (entityMatch.EntityVariableName1 != null)
            {
                entityMatch.EntityVariable1 = Variables[entityMatch.EntityVariableName1];
                entityMatch.EntityVariable1.AddDialogNodeReference(dialogNode, VariableReferenceType.Write);
            }
            if (entityMatch.EntityVariableName2 != null)
            {
                entityMatch.EntityVariable2 = Variables[entityMatch.EntityVariableName2];
                entityMatch.EntityVariable2.AddDialogNodeReference(dialogNode, VariableReferenceType.Write);
            }
        }