RecognitionOption.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
5 using Newtonsoft.Json;
6 
7 namespace Microsoft.Bot.Builder.Calling.ObjectModel.Contracts
8 {
13  [JsonObject(MemberSerialization.OptOut)]
14  public class RecognitionOption
15  {
19  [JsonProperty(Required = Required.Always)]
20  public string Name { get; set; }
21 
26  [JsonProperty(Required = Required.Default)]
27  public IEnumerable<string> SpeechVariation { get; set; }
28 
33  [JsonProperty(Required = Required.Default)]
34  public char? DtmfVariation { get; set; }
35 
36  public void Validate()
37  {
38  Utils.AssertArgument(!String.IsNullOrWhiteSpace(this.Name), "Choice 'Name' must be set to a valid non-empty value");
39 
40  bool speechVariationSet = this.SpeechVariation != null && this.SpeechVariation.Any();
41  bool dtmfVarationSet = this.DtmfVariation != null;
42 
43  Utils.AssertArgument(speechVariationSet || dtmfVarationSet, "SpeechVariation or DtmfVariation or both must be set");
44 
45  if (speechVariationSet)
46  {
47  foreach (string s in SpeechVariation)
48  {
49  Utils.AssertArgument(!String.IsNullOrWhiteSpace(s), "Null or empty choice cannot be set for speech variation");
50  }
51 
52  Utils.AssertArgument(this.SpeechVariation.Count() <= MaxValues.NumberOfSpeechVariations, "Number of speech variations specified cannot exceed : {0}", MaxValues.NumberOfSpeechVariations);
53  }
54 
55  if (dtmfVarationSet)
56  {
57  ValidDtmfs.Validate(this.DtmfVariation.Value);
58  }
59  }
60 
61  public static void Validate(IEnumerable<RecognitionOption> choices)
62  {
63  Utils.AssertArgument(choices != null, "choices list cannot be null");
64  Utils.AssertArgument(choices.Any(), "choices list cannot be empty");
65  HashSet<string> speechChoice = new HashSet<string>();
66  HashSet<char> dtmfChoice = new HashSet<char>();
67  foreach (var choice in choices)
68  {
69  Utils.AssertArgument(choice != null, "choice cannot be null");
70  choice.Validate();
71  if (choice.DtmfVariation.HasValue)
72  {
73  char c = choice.DtmfVariation.Value;
74  Utils.AssertArgument(!dtmfChoice.Contains(c), "Dtmf choices must be uniquely specified across all recognition options");
75  dtmfChoice.Add(c);
76  }
77  if (choice.SpeechVariation != null)
78  {
79  foreach (string sc in choice.SpeechVariation)
80  {
81  Utils.AssertArgument(!speechChoice.Contains(sc), "Speech choices must be uniquely specified across all recognition options");
82  speechChoice.Add(sc);
83  }
84  }
85  }
86  }
87  }
88 }
static readonly uint NumberOfSpeechVariations
Maximum number of speech variations for a choice
Definition: MaxValues.cs:42
Root namespace for the Microsoft Bot Builder Calling SDK object model.
static void AssertArgument(bool condition, string format, params object[] args)
Argument checker
Definition: Utils.cs:17
static void Validate(IEnumerable< RecognitionOption > choices)
This is part of the "recognize" action. If the customer wants to speech/dtmf choice based recognition...
Root namespace for the Microsoft Bot Builder Calling SDK.
This is a helper class for validating dtmfs specified in strings
Definition: ValidDtmfs.cs:10
Namespace for utilities and helpers in the Microsoft Bot Builder Calling SDK.
Root namespace for the Microsoft Bot Builder SDK.