IRecognize.cs
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license.
4 //
5 // Microsoft Bot Framework: http://botframework.com
6 //
7 // Bot Builder SDK GitHub:
8 // https://github.com/Microsoft/BotBuilder
9 //
10 // Copyright (c) Microsoft Corporation
11 // All rights reserved.
12 //
13 // MIT License:
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 
34 using System.Collections.Generic;
35 using System.Linq;
36 
37 namespace Microsoft.Bot.Builder.FormFlow.Advanced
38 {
42  public enum SpecialValues
43  {
47  Field
48  };
49 
53  public class TermMatch
54  {
62  public TermMatch(int start, int length, double confidence, object value)
63  {
64  Start = start;
65  Length = length;
66  Confidence = confidence;
67  Value = value;
68  }
69 
73  public readonly int Start;
74 
78  public int End { get { return Start + Length; } }
79 
83  public readonly int Length;
84 
88  public readonly double Confidence;
89 
93  public readonly object Value;
94 
100  public bool Same(TermMatch match)
101  {
102  return Start == match.Start && End == match.End;
103  }
104 
110  public bool Covers(TermMatch match)
111  {
112  return Start <= match.Start && End >= match.End && (Start != match.Start || End != match.End);
113  }
114 
120  public bool Overlaps(TermMatch match)
121  {
122  return (match.Start <= End && Start <= match.Start && End <= match.End) // tmtm
123  || (Start <= match.End && match.Start <= Start && match.End <= End) // mtmt
124  || (Start <= match.Start && End >= match.End) // tmmt
125  || (match.Start <= Start && match.End >= End) // mttm
126  ;
127  }
128 
129  public override string ToString()
130  {
131  return string.Format("TermMatch({0}, {1}, {2}-{3})", Value, Confidence, Start, Start + Length);
132  }
133 
134  public override bool Equals(object obj)
135  {
136  return obj is TermMatch && this == (TermMatch)obj;
137  }
138 
139  public static bool operator ==(TermMatch m1, TermMatch m2)
140  {
141  return ReferenceEquals(m1, m2) || (!ReferenceEquals(m1, null) && !ReferenceEquals(m2, null) && m1.Start == m2.Start && m1.Length == m2.Length && m1.Confidence == m2.Confidence && m1.Value == m2.Value);
142  }
143 
144  public static bool operator !=(TermMatch m1, TermMatch m2)
145  {
146  return !(m1 == m2);
147  }
148 
149  public override int GetHashCode()
150  {
151  return Start.GetHashCode() ^ Length.GetHashCode() ^ Confidence.GetHashCode() ^ Value.GetHashCode();
152  }
153  }
154 
159  public interface IRecognize<T>
160  {
161  #region Documentation
162  #endregion
166  object[] PromptArgs();
167 
172  IEnumerable<object> Values();
173 
178  IEnumerable<DescribeAttribute> ValueDescriptions();
179 
185  DescribeAttribute ValueDescription(object value);
186 
192  IEnumerable<string> ValidInputs(object value);
193 
198  string Help(T state, object defaultValue = null);
199 
206  IEnumerable<TermMatch> Matches(string input, object defaultValue = null);
207  }
208 }
readonly int Length
Length of match in input string.
Definition: IRecognize.cs:83
int End
End of match in input string.
Definition: IRecognize.cs:78
Interface for recognizers that look for matches in user input.
Definition: IRecognize.cs:159
What to display when asked for help.
TermMatch(int start, int length, double confidence, object value)
Construct a match.
Definition: IRecognize.cs:62
readonly double Confidence
Confidence of match, 0-1.0.
Definition: IRecognize.cs:88
readonly int Start
Start of match in input string.
Definition: IRecognize.cs:73
Field is used to get a value to set in the form state.
Base class with declarative implementation of IField.
Definition: Field.cs:67
SpecialValues
Enumeration of special kinds of matches.
Definition: IRecognize.cs:42
bool Covers(TermMatch match)
Check to see if this completely covers match.
Definition: IRecognize.cs:110
readonly object Value
Underlying C# value.
Definition: IRecognize.cs:93
bool Overlaps(TermMatch match)
Check to see if this overlaps with match in input.
Definition: IRecognize.cs:120
Describe a possible match in the user input.
Definition: IRecognize.cs:53
bool Same(TermMatch match)
Check to see if this covers the same span as match.
Definition: IRecognize.cs:100
Attribute to override the default description of a field, property or enum value. ...
Definition: Attributes.cs:62