-
Notifications
You must be signed in to change notification settings - Fork 7
/
IClassifier.cs
198 lines (185 loc) · 6.37 KB
/
IClassifier.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
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
using Microsoft.Bot.Builder;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Framework.Classifier
{
/// <summary>
/// Defines the interface for classifier connections.
/// </summary>
public interface IClassifier
{
/// <summary>
/// Interprets the current user input.
/// </summary>
/// <param name="context">Current context of the dialog.</param>
/// <param name="cancellationToken">Cancellation token of the current context.</param>
/// <returns></returns>
Task Recognize(ITurnContext context, CancellationToken cancellationToken);
/// <summary>
/// Retrieves the last classification result.
/// </summary>
/// <param name="cleanup">Indicates whether the entities shell be preprocessed.</param>
/// <returns></returns>
ClassifierResult GetResult(bool cleanup = true);
}
/// <summary>
/// Defines a classification result.
/// </summary>
public sealed class ClassifierResult
{
/// <summary>
/// Create a new classification result.
/// </summary>
/// <param name="text">The given input.</param>
/// <param name="intents">The found intents with its scorings.</param>
/// <param name="entities">The found entities.</param>
public ClassifierResult(string text, Dictionary<string, double> intents, Dictionary<string, List<IEntity>> entities)
{
Text = text;
Intents = intents;
Entities = entities;
}
/// <summary>
/// The given input.
/// </summary>
public string Text { get; }
/// <summary>
/// The found intents with its scorings.
/// </summary>
public Dictionary<string, double> Intents { get; }
/// <summary>
/// The found entities.
/// </summary>
public Dictionary<string, List<IEntity>> Entities { get; }
/// <summary>
/// Retrieves the top scoring intent and its score.
/// </summary>
/// <returns>Tuple of top intent and score.</returns>
public (string, double) GetTopScoringIntent()
{
(string, double) max = (null, double.NaN);
double maxScore = double.NegativeInfinity;
foreach (string key in Intents.Keys)
{
if (Intents[key] > maxScore)
{
max = (key, Intents[key]);
maxScore = Intents[key];
}
}
return max;
}
}
/// <summary>
/// Defines an entity.
/// </summary>
public interface IEntity
{
/// <summary>
/// The text snippet matched to the entity.
/// </summary>
string Text { get; }
/// <summary>
/// Start index of the snippet.
/// </summary>
int StartIndex { get; }
/// <summary>
/// End index of the snippet.
/// </summary>
int EndIndex { get; }
/// <summary>
/// Type of the found entity.
/// </summary>
EntityType EType { get; }
/// <summary>
/// Confidence of the found entity.
/// </summary>
double Confidence { get; }
}
/// <summary>
/// Defines the type of an entity. May be extended in later versions.
/// </summary>
public enum EntityType
{
/// <summary>
/// Defines the type of a grouped Entity. See Luis List Entity.
/// </summary>
/// <see cref="GroupEntity"/>
Group,
/// <summary>
/// Defines the type of a time specification.
/// </summary>
/// <see cref="TimeEntity"/>
Time
}
/// <summary>
/// Defines the result of an entity which defines a group of shapes (and their associated text snippets).
/// </summary>
public sealed class GroupEntity : IEntity
{
/// <summary>
/// Create a new result.
/// </summary>
/// <param name="text">The text snippet.</param>
/// <param name="startIndex">The start index of the snippet.</param>
/// <param name="endIndex">The end index of the snippet.</param>
/// <param name="group">The associated group.</param>
/// <param name="shape">Defines the shape.</param>
/// <param name="confidence">Confidence of the entity.</param>
public GroupEntity(string text, int startIndex, int endIndex, string group, string shape, double confidence=1)
{
Text = text;
StartIndex = startIndex;
EndIndex = endIndex;
Group = group;
Shape = shape;
Confidence = confidence;
}
public string Text { get; }
public int StartIndex { get; }
public int EndIndex { get; }
public double Confidence { get; }
/// <summary>
/// The name of the group.
/// </summary>
public string Group { get; }
/// <summary>
/// The name of the shape.
/// </summary>
public string Shape { get; }
public EntityType EType => EntityType.Group;
}
/// <summary>
/// Defines the result of an entity which defines times.
/// </summary>
public sealed class TimeEntity : IEntity
{
/// <summary>
/// Creates a time entity.
/// </summary>
/// <param name="text">The text snippet.</param>
/// <param name="startIndex">The start index of the snippet.</param>
/// <param name="endIndex">The end index of the snippet.</param>
/// <param name="times">Datetimes associated with the datetime results.</param>
/// <param name="confidence">Confidence of the entity.</param>
public TimeEntity(string text, int startIndex, int endIndex, List<DateTime> times, double confidence=1)
{
Text = text;
StartIndex = startIndex;
EndIndex = endIndex;
Times = times;
Confidence = confidence;
}
public string Text { get; }
public int StartIndex { get; }
public int EndIndex { get; }
public double Confidence { get; }
/// <summary>
/// The found datetimes.
/// </summary>
public List<DateTime> Times { get; }
public EntityType EType => EntityType.Time;
}
}