File size: 8,016 Bytes
0827183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
from typing import List

import aiounittest

from botbuilder.dialogs.choices import (
    ChoiceRecognizers,
    Find,
    FindValuesOptions,
    SortedValue,
)


def assert_result(result, start, end, text):
    assert (
        result.start == start
    ), f"Invalid ModelResult.start of '{result.start}' for '{text}' result."
    assert (
        result.end == end
    ), f"Invalid ModelResult.end of '{result.end}' for '{text}' result."
    assert (
        result.text == text
    ), f"Invalid ModelResult.text of '{result.text}' for '{text}' result."


def assert_value(result, value, index, score):
    assert (
        result.type_name == "value"
    ), f"Invalid ModelResult.type_name of '{result.type_name}' for '{value}' value."
    assert result.resolution, f"Missing ModelResult.resolution for '{value}' value."
    resolution = result.resolution
    assert (
        resolution.value == value
    ), f"Invalid resolution.value of '{resolution.value}' for '{value}' value."
    assert (
        resolution.index == index
    ), f"Invalid resolution.index of '{resolution.index}' for '{value}' value."
    assert (
        resolution.score == score
    ), f"Invalid resolution.score of '{resolution.score}' for '{value}' value."


def assert_choice(result, value, index, score, synonym=None):
    assert (
        result.type_name == "choice"
    ), f"Invalid ModelResult.type_name of '{result.type_name}' for '{value}' choice."
    assert result.resolution, f"Missing ModelResult.resolution for '{value}' choice."
    resolution = result.resolution
    assert (
        resolution.value == value
    ), f"Invalid resolution.value of '{resolution.value}' for '{value}' choice."
    assert (
        resolution.index == index
    ), f"Invalid resolution.index of '{resolution.index}' for '{value}' choice."
    assert (
        resolution.score == score
    ), f"Invalid resolution.score of '{resolution.score}' for '{value}' choice."
    if synonym:
        assert (  # pylint: disable=assert-on-tuple
            resolution.synonym == synonym,
            f"Invalid resolution.synonym of '{resolution.synonym}' for '{value}' choice.",
        )


_color_choices: List[str] = ["red", "green", "blue"]
_overlapping_choices: List[str] = ["bread", "bread pudding", "pudding"]

_color_values: List[SortedValue] = [
    SortedValue(value="red", index=0),
    SortedValue(value="green", index=1),
    SortedValue(value="blue", index=2),
]

_overlapping_values: List[SortedValue] = [
    SortedValue(value="bread", index=0),
    SortedValue(value="bread pudding", index=1),
    SortedValue(value="pudding", index=2),
]

_similar_values: List[SortedValue] = [
    SortedValue(value="option A", index=0),
    SortedValue(value="option B", index=1),
    SortedValue(value="option C", index=2),
]


class ChoiceRecognizersTest(aiounittest.AsyncTestCase):
    # Find.find_values

    def test_should_find_a_simple_value_in_a_single_word_utterance(self):
        found = Find.find_values("red", _color_values)
        assert len(found) == 1, f"Invalid token count of '{len(found)}' returned."
        assert_result(found[0], 0, 2, "red")
        assert_value(found[0], "red", 0, 1.0)

    def test_should_find_a_simple_value_in_an_utterance(self):
        found = Find.find_values("the red one please.", _color_values)
        assert len(found) == 1, f"Invalid token count of '{len(found)}' returned."
        assert_result(found[0], 4, 6, "red")
        assert_value(found[0], "red", 0, 1.0)

    def test_should_find_multiple_values_within_an_utterance(self):
        found = Find.find_values("the red and blue ones please.", _color_values)
        assert len(found) == 2, f"Invalid token count of '{len(found)}' returned."
        assert_result(found[0], 4, 6, "red")
        assert_value(found[0], "red", 0, 1.0)
        assert_value(found[1], "blue", 2, 1.0)

    def test_should_find_multiple_values_that_overlap(self):
        found = Find.find_values(
            "the bread pudding and bread please.", _overlapping_values
        )
        assert len(found) == 2, f"Invalid token count of '{len(found)}' returned."
        assert_result(found[0], 4, 16, "bread pudding")
        assert_value(found[0], "bread pudding", 1, 1.0)
        assert_value(found[1], "bread", 0, 1.0)

    def test_should_correctly_disambiguate_between_similar_values(self):
        found = Find.find_values(
            "option B", _similar_values, FindValuesOptions(allow_partial_matches=True)
        )
        assert len(found) == 1, f"Invalid token count of '{len(found)}' returned."
        assert_value(found[0], "option B", 1, 1.0)

    def test_should_find_a_single_choice_in_an_utterance(self):
        found = Find.find_choices("the red one please.", _color_choices)
        assert len(found) == 1, f"Invalid token count of '{len(found)}' returned."
        assert_result(found[0], 4, 6, "red")
        assert_choice(found[0], "red", 0, 1.0, "red")

    def test_should_find_multiple_choices_within_an_utterance(self):
        found = Find.find_choices("the red and blue ones please.", _color_choices)
        assert len(found) == 2, f"Invalid token count of '{len(found)}' returned."
        assert_result(found[0], 4, 6, "red")
        assert_choice(found[0], "red", 0, 1.0)
        assert_choice(found[1], "blue", 2, 1.0)

    def test_should_find_multiple_choices_that_overlap(self):
        found = Find.find_choices(
            "the bread pudding and bread please.", _overlapping_choices
        )
        assert len(found) == 2, f"Invalid token count of '{len(found)}' returned."
        assert_result(found[0], 4, 16, "bread pudding")
        assert_choice(found[0], "bread pudding", 1, 1.0)
        assert_choice(found[1], "bread", 0, 1.0)

    def test_should_accept_null_utterance_in_find_choices(self):
        found = Find.find_choices(None, _color_choices)
        assert not found

    # ChoiceRecognizers.recognize_choices

    def test_should_find_a_choice_in_an_utterance_by_name(self):
        found = ChoiceRecognizers.recognize_choices(
            "the red one please.", _color_choices
        )
        assert len(found) == 1
        assert_result(found[0], 4, 6, "red")
        assert_choice(found[0], "red", 0, 1.0, "red")

    def test_should_find_a_choice_in_an_utterance_by_ordinal_position(self):
        found = ChoiceRecognizers.recognize_choices(
            "the first one please.", _color_choices
        )
        assert len(found) == 1
        assert_result(found[0], 4, 8, "first")
        assert_choice(found[0], "red", 0, 1.0)

    def test_should_find_multiple_choices_in_an_utterance_by_ordinal_position(self):
        found = ChoiceRecognizers.recognize_choices(
            "the first and third one please", _color_choices
        )
        assert len(found) == 2
        assert_choice(found[0], "red", 0, 1.0)
        assert_choice(found[1], "blue", 2, 1.0)

    def test_should_find_a_choice_in_an_utterance_by_numerical_index_digit(self):
        found = ChoiceRecognizers.recognize_choices("1", _color_choices)
        assert len(found) == 1
        assert_result(found[0], 0, 0, "1")
        assert_choice(found[0], "red", 0, 1.0)

    def test_should_find_a_choice_in_an_utterance_by_numerical_index_text(self):
        found = ChoiceRecognizers.recognize_choices("one", _color_choices)
        assert len(found) == 1
        assert_result(found[0], 0, 2, "one")
        assert_choice(found[0], "red", 0, 1.0)

    def test_should_find_multiple_choices_in_an_utterance_by_numerical_index(self):
        found = ChoiceRecognizers.recognize_choices("option one and 3.", _color_choices)
        assert len(found) == 2
        assert_choice(found[0], "red", 0, 1.0)
        assert_choice(found[1], "blue", 2, 1.0)

    def test_should_accept_null_utterance_in_recognize_choices(self):
        found = ChoiceRecognizers.recognize_choices(None, _color_choices)
        assert not found