-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVoiceCommandMatcher.cs
More file actions
308 lines (263 loc) · 9.01 KB
/
Copy pathVoiceCommandMatcher.cs
File metadata and controls
308 lines (263 loc) · 9.01 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System.Text;
namespace PrimeDictate;
internal readonly record struct VoiceCommandMatch(
string CleanedText,
bool CommitRequested,
bool StopRequested,
bool HistoryRequested,
VoiceShellCommandInvocation? ShellCommandInvocation);
internal sealed record VoiceShellCommandInvocation(
VoiceShellCommand Command,
string TextToType);
internal sealed record VoiceCommandOptions(
bool Enabled,
string DictationPhrase,
string StopPhrase,
string HistoryPhrase,
IReadOnlyList<VoiceShellCommand> ShellCommands);
internal static class VoiceCommandMatcher
{
public static VoiceCommandMatch Apply(
string transcript,
VoiceCommandOptions options,
bool includeShellCommands = false)
{
if (!options.Enabled || string.IsNullOrWhiteSpace(transcript))
{
return new VoiceCommandMatch(
transcript,
CommitRequested: false,
StopRequested: false,
HistoryRequested: false,
ShellCommandInvocation: null);
}
var cleaned = transcript;
var commitRequested = TryRemovePhrase(cleaned, options.DictationPhrase, out cleaned);
var stopRequested = TryRemovePhrase(cleaned, options.StopPhrase, out cleaned);
var historyRequested = TryRemovePhrase(cleaned, options.HistoryPhrase, out cleaned);
VoiceShellCommandInvocation? shellCommandInvocation = null;
if (includeShellCommands &&
!commitRequested &&
!stopRequested &&
TryFindShellCommand(cleaned, options.ShellCommands, out var matchedInvocation, out var shellCleaned))
{
shellCommandInvocation = matchedInvocation;
cleaned = shellCleaned;
}
return new VoiceCommandMatch(
CollapseWhitespace(cleaned).Trim(),
commitRequested,
stopRequested,
historyRequested,
shellCommandInvocation);
}
private static bool TryFindShellCommand(
string transcript,
IReadOnlyList<VoiceShellCommand> shellCommands,
out VoiceShellCommandInvocation invocation,
out string cleaned)
{
cleaned = transcript;
var textTokens = Tokenize(transcript);
if (textTokens.Count == 0)
{
invocation = null!;
return false;
}
foreach (var candidate in shellCommands.OrderByDescending(command => Tokenize(command.Phrase).Count))
{
if (!candidate.Enabled ||
string.IsNullOrWhiteSpace(candidate.Phrase) ||
string.IsNullOrWhiteSpace(candidate.Command))
{
continue;
}
var phraseTokens = Tokenize(candidate.Phrase);
if (phraseTokens.Count == 0 || textTokens.Count < phraseTokens.Count)
{
continue;
}
for (var startIndex = 0; startIndex <= textTokens.Count - phraseTokens.Count; startIndex++)
{
if (!TokensMatchAt(textTokens, phraseTokens, startIndex))
{
continue;
}
var removeStart = textTokens[startIndex].Start;
var phraseEnd = textTokens[startIndex + phraseTokens.Count - 1].End;
var removeEnd = phraseEnd;
var textToType = string.Empty;
var remainder = transcript[phraseEnd..];
if (TryGetChainedTypeText(remainder, out var chainedText, out var chainedRemoveEnd))
{
textToType = chainedText;
removeEnd = phraseEnd + chainedRemoveEnd;
}
cleaned = RemoveSpan(transcript, removeStart, removeEnd);
invocation = new VoiceShellCommandInvocation(candidate, textToType);
return true;
}
}
invocation = null!;
return false;
}
private static bool TokensMatchAt(
IReadOnlyList<TokenSpan> textTokens,
IReadOnlyList<TokenSpan> phraseTokens,
int startIndex)
{
for (var index = 0; index < phraseTokens.Count; index++)
{
if (!string.Equals(
textTokens[startIndex + index].Value,
phraseTokens[index].Value,
StringComparison.OrdinalIgnoreCase))
{
return false;
}
}
return true;
}
private static bool TryGetChainedTypeText(string remainder, out string textToType, out int removeEnd)
{
removeEnd = 0;
var tokens = Tokenize(remainder);
if (tokens.Count < 2)
{
textToType = string.Empty;
return false;
}
var typeTokenIndex = GetTypeTokenIndex(tokens);
if (typeTokenIndex >= tokens.Count - 1 ||
!string.Equals(tokens[typeTokenIndex].Value, "type", StringComparison.OrdinalIgnoreCase))
{
textToType = string.Empty;
return false;
}
textToType = CollapseWhitespace(remainder[tokens[typeTokenIndex].End..]).Trim();
if (textToType.Length == 0)
{
return false;
}
removeEnd = remainder.Length;
return true;
}
private static int GetTypeTokenIndex(IReadOnlyList<TokenSpan> tokens)
{
if (tokens.Count == 0)
{
return 0;
}
if (string.Equals(tokens[0].Value, "and", StringComparison.OrdinalIgnoreCase) ||
string.Equals(tokens[0].Value, "then", StringComparison.OrdinalIgnoreCase))
{
if (tokens.Count > 2 &&
string.Equals(tokens[0].Value, "and", StringComparison.OrdinalIgnoreCase) &&
string.Equals(tokens[1].Value, "then", StringComparison.OrdinalIgnoreCase))
{
return 2;
}
return 1;
}
return 0;
}
private static bool TryRemovePhrase(string text, string phrase, out string cleaned)
{
cleaned = text;
var phraseTokens = Tokenize(phrase);
if (phraseTokens.Count == 0)
{
return false;
}
var textTokens = Tokenize(text);
if (textTokens.Count < phraseTokens.Count)
{
return false;
}
for (var startIndex = 0; startIndex <= textTokens.Count - phraseTokens.Count; startIndex++)
{
var matched = true;
for (var phraseIndex = 0; phraseIndex < phraseTokens.Count; phraseIndex++)
{
if (!string.Equals(
textTokens[startIndex + phraseIndex].Value,
phraseTokens[phraseIndex].Value,
StringComparison.OrdinalIgnoreCase))
{
matched = false;
break;
}
}
if (!matched)
{
continue;
}
cleaned = RemoveSpan(
text,
textTokens[startIndex].Start,
textTokens[startIndex + phraseTokens.Count - 1].End);
return true;
}
return false;
}
private static string RemoveSpan(string text, int removeStart, int removeEnd)
{
while (removeStart > 0 && char.IsWhiteSpace(text[removeStart - 1]))
{
removeStart--;
}
while (removeEnd < text.Length && (char.IsWhiteSpace(text[removeEnd]) || char.IsPunctuation(text[removeEnd])))
{
removeEnd++;
}
return text[..removeStart] + " " + text[removeEnd..];
}
private static List<TokenSpan> Tokenize(string text)
{
var tokens = new List<TokenSpan>();
var index = 0;
while (index < text.Length)
{
while (index < text.Length && !char.IsLetterOrDigit(text[index]))
{
index++;
}
if (index >= text.Length)
{
break;
}
var start = index;
while (index < text.Length && char.IsLetterOrDigit(text[index]))
{
index++;
}
tokens.Add(new TokenSpan(text[start..index], start, index));
}
return tokens;
}
private static string CollapseWhitespace(string text)
{
if (string.IsNullOrWhiteSpace(text))
{
return string.Empty;
}
var builder = new StringBuilder(text.Length);
var lastWasWhitespace = false;
foreach (var character in text)
{
if (char.IsWhiteSpace(character))
{
if (!lastWasWhitespace)
{
builder.Append(' ');
lastWasWhitespace = true;
}
continue;
}
builder.Append(character);
lastWasWhitespace = false;
}
return builder.ToString();
}
private readonly record struct TokenSpan(string Value, int Start, int End);
}