-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathBestMatchMiddleware.cs
More file actions
219 lines (186 loc) · 8.7 KB
/
BestMatchMiddleware.cs
File metadata and controls
219 lines (186 loc) · 8.7 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
namespace Bot.Builder.Community.Middleware.BestMatch
{
public abstract class BestMatchMiddleware : IMiddleware
{
protected Dictionary<BestMatchAttribute, BestMatchHandler> HandlerByBestMatchLists;
public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext.Activity.Type == ActivityTypes.Message)
{
await HandleMessage(turnContext, turnContext.Activity.Text, next, cancellationToken);
}
else
{
await next(cancellationToken).ConfigureAwait(false);
}
}
private async Task HandleMessage(ITurnContext context, string messageText, NextDelegate next, CancellationToken cancellationToken)
{
if (HandlerByBestMatchLists == null)
{
HandlerByBestMatchLists =
new Dictionary<BestMatchAttribute, BestMatchHandler>(GetHandlersByBestMatchLists());
}
BestMatchHandler handler = null;
double bestMatchedScore = 0;
foreach (var handlerByBestMatchList in HandlerByBestMatchLists)
{
var match = FindBestMatch(handlerByBestMatchList.Key.BestMatchList,
messageText,
handlerByBestMatchList.Key.Threshold,
handlerByBestMatchList.Key.IgnoreCase,
handlerByBestMatchList.Key.IgnoreNonAlphanumericCharacters);
if (match?.Score > bestMatchedScore)
{
bestMatchedScore = match.Score;
handler = handlerByBestMatchList.Value;
}
}
await (handler ?? NoMatchHandler).Invoke(context, messageText, next, cancellationToken);
}
protected virtual IDictionary<BestMatchAttribute, BestMatchHandler> GetHandlersByBestMatchLists()
{
return EnumerateHandlers(this).ToDictionary(kv => kv.Key, kv => kv.Value);
}
private static StringMatch FindBestMatch(IEnumerable<string> choices, string utterance, double threshold = 0.5, bool ignoreCase = true, bool ignoreNonAlphanumeric = true)
{
StringMatch bestMatch = null;
var matches = FindAllMatches(choices, utterance, threshold, ignoreCase, ignoreNonAlphanumeric);
foreach (var match in matches)
{
if (bestMatch == null || match.Score > bestMatch.Score)
{
bestMatch = match;
}
}
return bestMatch;
}
private static IEnumerable<StringMatch> FindAllMatches(IEnumerable<string> choices, string utterance, double threshold = 0.6, bool ignoreCase = true, bool ignoreNonAlphanumeric = true)
{
var matches = new List<StringMatch>();
var choicesList = choices as IList<string> ?? choices.ToList();
if (!choicesList.Any())
return matches;
var utteranceToCheck = ignoreNonAlphanumeric
? Regex.Replace(utterance, @"[^A-Za-z0-9 ]", string.Empty)
: utterance;
var tokens = utterance.Split(' ');
foreach (var choice in choicesList)
{
double score = 0;
var choiceValue = choice.Trim();
if (ignoreNonAlphanumeric)
Regex.Replace(choiceValue, @"[^A-Za-z0-9 ]", string.Empty);
if (choiceValue.IndexOf(utteranceToCheck, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) >= 0)
{
score = (double)decimal.Divide((decimal)utteranceToCheck.Length, (decimal)choiceValue.Length);
}
else if (utteranceToCheck.IndexOf(choiceValue) >= 0)
{
score = Math.Min(0.5 + (choiceValue.Length / utteranceToCheck.Length), 0.9);
}
else
{
foreach (var token in tokens)
{
var matched = string.Empty;
if (choiceValue.IndexOf(token, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) >= 0)
{
matched += token;
}
score = (double)decimal.Divide((decimal)matched.Length, (decimal)choiceValue.Length);
}
}
if (score >= threshold)
{
matches.Add(new StringMatch { Choice = choiceValue, Score = score });
}
}
return matches;
}
internal static IEnumerable<KeyValuePair<BestMatchAttribute, BestMatchHandler>> EnumerateHandlers(object dialog)
{
var type = dialog.GetType();
var methods = type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (var method in methods)
{
var bestMatchListAttributes = method.GetCustomAttributes<BestMatchAttribute>(inherit: true).ToArray();
Delegate created = null;
try
{
created = Delegate.CreateDelegate(typeof(BestMatchHandler), dialog, method, throwOnBindFailure: false);
}
catch (ArgumentException)
{
// "Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type."
// https://github.com/Microsoft/BotBuilder/issues/634
// https://github.com/Microsoft/BotBuilder/issues/435
}
var bestMatchHandler = (BestMatchHandler)created;
if (bestMatchHandler != null)
{
foreach (var bestMatchListAttribute in bestMatchListAttributes)
{
if (bestMatchListAttribute != null && bestMatchListAttributes.Any())
yield return new KeyValuePair<BestMatchAttribute, BestMatchHandler>(bestMatchListAttribute, bestMatchHandler);
}
}
}
}
public virtual async Task NoMatchHandler(ITurnContext context, string messageText, NextDelegate next, CancellationToken cancellationToken)
{
await next(cancellationToken).ConfigureAwait(false);
}
internal class StringMatch
{
public string Choice { get; set; }
public double Score { get; set; }
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class BestMatchAttribute : Attribute
{
public readonly string BestMatchListDelimited;
public readonly string[] BestMatchList;
public readonly bool IgnoreCase;
public readonly bool IgnoreNonAlphanumericCharacters;
public readonly double Threshold;
public BestMatchAttribute(string[] bestMatchList, double threshold = 0.5, bool ignoreCase = true, bool ignoreNonAlphaNumericCharacters = true)
{
BestMatchList = bestMatchList;
IgnoreCase = ignoreCase;
IgnoreNonAlphanumericCharacters = ignoreNonAlphaNumericCharacters;
Threshold = threshold;
}
public BestMatchAttribute(string bestMatchListDelimited, double threshold = 0.5, bool ignoreCase = true, bool ignoreNonAlphaNumericCharacters = true, char listDelimiter = ',')
{
BestMatchListDelimited = bestMatchListDelimited;
if (!string.IsNullOrEmpty(bestMatchListDelimited))
{
BestMatchList = StringToListString(bestMatchListDelimited, listDelimiter).ToArray<string>();
}
IgnoreCase = ignoreCase;
IgnoreNonAlphanumericCharacters = ignoreNonAlphaNumericCharacters;
Threshold = threshold;
}
public static IEnumerable<string> StringToListString(string str, char delimiter = ',')
{
if (String.IsNullOrEmpty(str))
yield break;
foreach (var s in str.Split(delimiter))
{
yield return s;
}
}
}
public delegate Task BestMatchHandler(ITurnContext context, string messageText, NextDelegate next, CancellationToken cancellationToken);
}