private static FuzzyRecognizerResult FindAllMatches(IEnumerable <string> choices, string utterance, FuzzyRecognizerOptions options) { var result = new FuzzyRecognizerResult() { Matches = new List <FuzzyMatch>() }; var choicesList = choices as IList <string> ?? choices.ToList(); if (!choicesList.Any()) { return(result); } string utteranceToCheck = utterance; if (options.IgnoreCase) { utteranceToCheck = utteranceToCheck.ToLower(); } if (options.IgnoreNonAlphanumeric) { utteranceToCheck = Regex.Replace(utteranceToCheck, "[^a-zA-Z0-9_. ]+", "", RegexOptions.Compiled).Trim(); } foreach (var choice in choicesList) { double score = 0; var choiceValue = choice.Trim(); if (options.IgnoreNonAlphanumeric) { choiceValue = Regex.Replace(choiceValue, "[^a-zA-Z0-9_. ]+", "", RegexOptions.Compiled); } if (options.IgnoreCase) { choiceValue = choiceValue.ToLower(); } var editDistance = EditDistance(choiceValue, utteranceToCheck); var maxLength = (double)Math.Max(utteranceToCheck.Length, choiceValue.Length); var matchResult = maxLength - editDistance; score = matchResult / maxLength; if (score >= options.Threshold) { result.Matches.Add(new FuzzyMatch { Choice = choice, Score = score }); } } result.Matches = result.Matches.OrderByDescending(m => m.Score).ToList(); return(result); }
public FuzzyRecognizer(FuzzyRecognizerOptions fuzzyRecognizerOptions = null) { _fuzzyRecognizerOptions = fuzzyRecognizerOptions ?? new FuzzyRecognizerOptions(); }