-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathNumberWithUnitPrompt.cs
117 lines (99 loc) · 4.28 KB
/
NumberWithUnitPrompt.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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;
using Microsoft.Recognizers.Text.NumberWithUnit;
using static Microsoft.Recognizers.Text.Culture;
namespace Bot.Builder.Community.Dialogs.Prompts
{
public class NumberWithUnitPrompt : Prompt<NumberWithUnitResult>
{
public NumberWithUnitPrompt(string dialogId, NumberWithUnitPromptType type, PromptValidator<NumberWithUnitResult> validator = null, string defaultLocale = null)
: base(dialogId, validator)
{
DefaultLocale = defaultLocale;
PromptType = type;
}
public string DefaultLocale { get; set; }
public NumberWithUnitPromptType PromptType { get; set; }
protected override async Task OnPromptAsync(ITurnContext turnContext, IDictionary<string, object> state, PromptOptions options, bool isRetry, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (isRetry && options.RetryPrompt != null)
{
await turnContext.SendActivityAsync(options.RetryPrompt, cancellationToken).ConfigureAwait(false);
}
else if (options.Prompt != null)
{
await turnContext.SendActivityAsync(options.Prompt, cancellationToken).ConfigureAwait(false);
}
}
protected override Task<PromptRecognizerResult<NumberWithUnitResult>> OnRecognizeAsync(ITurnContext turnContext, IDictionary<string, object> state, PromptOptions options, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}
var result = new PromptRecognizerResult<NumberWithUnitResult>();
if (turnContext.Activity.Type == ActivityTypes.Message)
{
var message = turnContext.Activity.AsMessageActivity();
var culture = turnContext.Activity.Locale ?? DefaultLocale ?? English;
List<Microsoft.Recognizers.Text.ModelResult> results = null;
switch(PromptType)
{
case NumberWithUnitPromptType.Currency:
results = NumberWithUnitRecognizer.RecognizeCurrency(message.Text, culture);
break;
case NumberWithUnitPromptType.Dimension:
results = NumberWithUnitRecognizer.RecognizeDimension(message.Text, culture);
break;
case NumberWithUnitPromptType.Age:
results = NumberWithUnitRecognizer.RecognizeAge(message.Text, culture);
break;
case NumberWithUnitPromptType.Temperature:
results = NumberWithUnitRecognizer.RecognizeTemperature(message.Text, culture);
break;
}
if (results?.Count > 0)
{
var resolvedUnit = results[0].Resolution["unit"].ToString();
var resolvedValue = results[0].Resolution["value"].ToString();
if (double.TryParse(resolvedValue, out var value))
{
result.Succeeded = true;
var numberWithUnitResult = new NumberWithUnitResult
{
Unit = resolvedUnit,
Value = value
};
result.Value = numberWithUnitResult;
}
}
}
return Task.FromResult(result);
}
}
public class NumberWithUnitResult
{
public string Unit { get; set; }
public dynamic Value { get; set; }
}
public enum NumberWithUnitPromptType
{
Currency,
Temperature,
Age,
Dimension
}
}