Пример #1
0
        public static List <ModelResult <FoundChoice> > RecognizeChoices(string utterance, List <Choice> list, FindChoicesOptions options = null)
        {
            // Try finding choices by text search first
            // - We only want to use a single strategy for returning results to avoid issues where utterances
            //   like the "the third one" or "the red one" or "the first division book" would miss-recognize as
            //   a numerical index or ordinal as well.

            var locale  = options?.Locale ?? Recognizers.Text.Culture.English;
            var matched = Find.FindChoices(utterance, list, options);

            if (matched.Count == 0)
            {
                // Next try finding by ordinal
                var matches = RecognizeOrdinal(utterance, locale);
                if (matches.Count() > 0)
                {
                    foreach (var match in matches)
                    {
                        MatchChoiceByIndex(list, matched, match);
                    }
                }
                else
                {
                    // Finally try by numerical index
                    matches = RecognizeNumber(utterance, locale);

                    foreach (var match in matches)
                    {
                        MatchChoiceByIndex(list, matched, match);
                    }
                }

                // Sort any found matches by their position within the utterance.
                // - The results from findChoices() are already properly sorted so we just need this
                //   for ordinal & numerical lookups.
                matched.Sort((a, b) => a.Start - b.Start);
            }
            return(matched);
        }
Пример #2
0
        public static List <ModelResult <FoundChoice> > FindChoices(string utterance, List <Choice> choices, FindChoicesOptions options = null)
        {
            if (string.IsNullOrEmpty(utterance))
            {
                throw new ArgumentNullException(nameof(utterance));
            }
            if (choices == null)
            {
                throw new ArgumentNullException(nameof(choices));
            }

            var opt = options ?? new FindChoicesOptions();

            // Build up full list of synonyms to search over.
            // - Each entry in the list contains the index of the choice it belongs to which will later be
            //   used to map the search results back to their choice.
            var synonyms = new List <SortedValue>();

            for (int index = 0; index < choices.Count; index++)
            {
                var choice = choices[index];

                if (!opt.NoValue)
                {
                    synonyms.Add(new SortedValue {
                        Value = choice.Value, Index = index
                    });
                }
                if (choice.Action != null && choice.Action.Title != null && !opt.NoAction)
                {
                    synonyms.Add(new SortedValue {
                        Value = choice.Action.Title, Index = index
                    });
                }

                if (choice.Synonyms != null)
                {
                    foreach (var synonym in choice.Synonyms)
                    {
                        synonyms.Add(new SortedValue {
                            Value = synonym, Index = index
                        });
                    }
                }
            }

            // Find synonyms in utterance and map back to their choices
            return(FindValues(utterance, synonyms, options).Select((v) =>
            {
                var choice = choices[v.Resolution.Index];
                return new ModelResult <FoundChoice>
                {
                    Start = v.Start,
                    End = v.End,
                    TypeName = "choice",
                    Text = v.Text,
                    Resolution = new FoundChoice
                    {
                        Value = choice.Value,
                        Index = v.Resolution.Index,
                        Score = v.Resolution.Score,
                        Synonym = v.Resolution.Value
                    }
                };
            }).ToList());
        }
Пример #3
0
 public static List <ModelResult <FoundChoice> > RecognizeChoices(string utterance, List <string> choices, FindChoicesOptions options = null)
 {
     return(RecognizeChoices(utterance, choices.Select(s => new Choice {
         Value = s
     }).ToList(), options));
 }
Пример #4
0
        public static List <ModelResult <FoundChoice> > FindChoices(string utterance, List <string> choices, FindChoicesOptions options = null)
        {
            if (choices == null)
            {
                throw new ArgumentNullException(nameof(choices));
            }

            return(FindChoices(utterance, choices.Select(s => new Choice {
                Value = s
            }).ToList(), options));
        }