forked from BotBuilderCommunity/botbuilder-community-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuzzyRecognizer.cs
168 lines (140 loc) · 5.9 KB
/
FuzzyRecognizer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Bot.Builder.Community.Recognizers.Fuzzy
{
public class FuzzyRecognizer
{
private readonly FuzzyRecognizerOptions _fuzzyRecognizerOptions;
public FuzzyRecognizer(FuzzyRecognizerOptions fuzzyRecognizerOptions = null)
{
_fuzzyRecognizerOptions = fuzzyRecognizerOptions ?? new FuzzyRecognizerOptions();
}
public Task<FuzzyRecognizerResult> Recognize(IEnumerable<string> choices, string utterance)
{
if (string.IsNullOrEmpty(utterance))
throw new ArgumentNullException(nameof(utterance));
if (choices == null)
throw new ArgumentNullException(nameof(choices));
return Task.FromResult(FindAllMatches(choices, utterance, _fuzzyRecognizerOptions));
}
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;
}
/// <summary>
/// Code for calculating distance from Stephen Toub
/// https://blogs.msdn.microsoft.com/toub/2006/05/05/generic-levenshtein-edit-distance-with-c/
/// </summary>
public static int EditDistance<T>(IEnumerable<T> x, IEnumerable<T> y) where T : IEquatable<T>
{
// Validate parameters
if (x == null) throw new ArgumentNullException("x");
if (y == null) throw new ArgumentNullException("y");
// Convert the parameters into IList instances
// in order to obtain indexing capabilities
IList<T> first = x as IList<T> ?? new List<T>(x);
IList<T> second = y as IList<T> ?? new List<T>(y);
// Get the length of both. If either is 0, return
// the length of the other, since that number of insertions
// would be required.
int n = first.Count, m = second.Count;
if (n == 0) return m;
if (m == 0) return n;
// Rather than maintain an entire matrix (which would require O(n*m) space),
// just store the current row and the next row, each of which has a length m+1,
// so just O(m) space. Initialize the current row.
int curRow = 0, nextRow = 1;
int[][] rows = new int[][] { new int[m + 1], new int[m + 1] };
for (int j = 0; j <= m; ++j) rows[curRow][j] = j;
// For each virtual row (since we only have physical storage for two)
for (int i = 1; i <= n; ++i)
{
// Fill in the values in the row
rows[nextRow][0] = i;
for (int j = 1; j <= m; ++j)
{
int dist1 = rows[curRow][j] + 1;
int dist2 = rows[nextRow][j - 1] + 1;
int dist3 = rows[curRow][j - 1] +
(first[i - 1].Equals(second[j - 1]) ? 0 : 1);
rows[nextRow][j] = Math.Min(dist1, Math.Min(dist2, dist3));
}
// Swap the current and next rows
if (curRow == 0)
{
curRow = 1;
nextRow = 0;
}
else
{
curRow = 0;
nextRow = 1;
}
}
// Return the computed edit distance
return rows[curRow][m];
}
}
public class FuzzyRecognizerResult
{
public FuzzyRecognizerResult()
{
Matches = new List<FuzzyMatch>();
}
public List<FuzzyMatch> Matches { get; set; }
}
public class FuzzyRecognizerOptions
{
public FuzzyRecognizerOptions(double threshold = 0.6, bool ignoreCase = true, bool ignoreNonAlphanumeric = true)
{
Threshold = threshold;
IgnoreCase = ignoreCase;
IgnoreNonAlphanumeric = ignoreNonAlphanumeric;
}
public bool IgnoreNonAlphanumeric { get; set; }
public bool IgnoreCase { get; set; }
public double Threshold { get; set; }
}
public class FuzzyMatch
{
public string Choice { get; set; }
public double Score { get; set; }
}
}