Debian Codegraph @

tests/test_sample.py

raw
1 from hassil import parse_sentence
2 from hassil.intents import RangeFractionType, RangeSlotList, TextSlotList
3 from hassil.sample import sample_sentence
4
5
6 def test_text_chunk():
7 assert set(sample_sentence(parse_sentence("this is a test"))) == {"this is a test"}
8
9
10 def test_group():
11 assert set(sample_sentence(parse_sentence("this (is a) test"))) == {
12 "this is a test"
13 }
14
15
16 def test_optional():
17 assert set(sample_sentence(parse_sentence("turn on [the] light[s]"))) == {
18 "turn on light",
19 "turn on lights",
20 "turn on the light",
21 "turn on the lights",
22 }
23
24
25 def test_double_optional():
26 assert set(sample_sentence(parse_sentence("turn [on] [the] light[s]"))) == {
27 "turn light",
28 "turn lights",
29 "turn on light",
30 "turn on lights",
31 "turn the light",
32 "turn the lights",
33 "turn on the light",
34 "turn on the lights",
35 }
36
37
38 def test_alternative():
39 assert set(sample_sentence(parse_sentence("this is (the | a) test"))) == {
40 "this is a test",
41 "this is the test",
42 }
43
44
45 def test_list():
46 sentence = parse_sentence("turn off {area}")
47 areas = TextSlotList.from_strings(["kitchen", "living room"])
48 assert set(sample_sentence(sentence, slot_lists={"area": areas})) == {
49 "turn off kitchen",
50 "turn off living room",
51 }
52
53
54 def test_list_range():
55 sentence = parse_sentence("run test {num}")
56 num_list = RangeSlotList(name=None, start=1, stop=3)
57 assert set(sample_sentence(sentence, slot_lists={"num": num_list})) == {
58 "run test 1",
59 "run test 2",
60 "run test 3",
61 }
62
63
64 def test_list_range_missing_language():
65 sentence = parse_sentence("run test {num}")
66 num_list = RangeSlotList(name=None, start=1, stop=3, words=True)
67
68 # Range slot digits cannot be converted to words without a language available.
69 assert set(sample_sentence(sentence, slot_lists={"num": num_list})) == {
70 "run test 1",
71 "run test 2",
72 "run test 3",
73 }
74
75
76 def test_list_range_words():
77 sentence = parse_sentence("run test {num}")
78 num_list = RangeSlotList(name=None, start=1, stop=3, words=True)
79 assert set(
80 sample_sentence(sentence, slot_lists={"num": num_list}, language="en")
81 ) == {
82 "run test 1",
83 "run test one",
84 "run test 2",
85 "run test two",
86 "run test 3",
87 "run test three",
88 }
89
90
91 def test_list_range_halves_words():
92 sentence = parse_sentence("run test {num}")
93 num_list = RangeSlotList(
94 name=None, start=1, stop=1, fraction_type=RangeFractionType.HALVES, words=True
95 )
96 assert set(
97 sample_sentence(sentence, slot_lists={"num": num_list}, language="en")
98 ) == {
99 "run test 1",
100 "run test one",
101 "run test 1.5",
102 "run test one point five",
103 }
104
105
106 def test_list_range_tenths_words():
107 sentence = parse_sentence("run test {num}")
108 num_list = RangeSlotList(
109 name=None, start=1, stop=1, fraction_type=RangeFractionType.TENTHS, words=True
110 )
111 assert set(
112 sample_sentence(sentence, slot_lists={"num": num_list}, language="en")
113 ) == {
114 "run test 1",
115 "run test one",
116 "run test 1.1",
117 "run test one point one",
118 "run test 1.2",
119 "run test one point two",
120 "run test 1.3",
121 "run test one point three",
122 "run test 1.4",
123 "run test one point four",
124 "run test 1.5",
125 "run test one point five",
126 "run test 1.6",
127 "run test one point six",
128 "run test 1.7",
129 "run test one point seven",
130 "run test 1.8",
131 "run test one point eight",
132 "run test 1.9",
133 "run test one point nine",
134 }
135
136
137 def test_rule():
138 sentence = parse_sentence("turn off <area>")
139 assert set(
140 sample_sentence(
141 sentence,
142 expansion_rules={"area": parse_sentence("[the] kitchen")},
143 )
144 ) == {"turn off kitchen", "turn off the kitchen"}
145
146
147 def test_permutation():
148 assert set(sample_sentence(parse_sentence("a;b;[c] d"))) == {
149 "a b d",
150 "a b c d",
151 "a c d b",
152 "a d b",
153 "b a d",
154 "b a c d",
155 "b c d a",
156 "b d a",
157 "c d a b",
158 "c d b a",
159 "d a b",
160 "d b a",
161 }
162
163
164 def test_skip_optionals() -> None:
165 assert set(sample_sentence(parse_sentence("a [b] c [d]"), skip_optionals=True)) == {
166 "a c "
167 }