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
|
from hassil import parse_sentence
from hassil.intents import RangeFractionType, RangeSlotList, TextSlotList
from hassil.sample import sample_sentence
def test_text_chunk():
assert set(sample_sentence(parse_sentence("this is a test"))) == {"this is a test"}
def test_group():
assert set(sample_sentence(parse_sentence("this (is a) test"))) == {
"this is a test"
}
def test_optional():
assert set(sample_sentence(parse_sentence("turn on [the] light[s]"))) == {
"turn on light",
"turn on lights",
"turn on the light",
"turn on the lights",
}
def test_double_optional():
assert set(sample_sentence(parse_sentence("turn [on] [the] light[s]"))) == {
"turn light",
"turn lights",
"turn on light",
"turn on lights",
"turn the light",
"turn the lights",
"turn on the light",
"turn on the lights",
}
def test_alternative():
assert set(sample_sentence(parse_sentence("this is (the | a) test"))) == {
"this is a test",
"this is the test",
}
def test_list():
sentence = parse_sentence("turn off {area}")
areas = TextSlotList.from_strings(["kitchen", "living room"])
assert set(sample_sentence(sentence, slot_lists={"area": areas})) == {
"turn off kitchen",
"turn off living room",
}
def test_list_range():
sentence = parse_sentence("run test {num}")
num_list = RangeSlotList(name=None, start=1, stop=3)
assert set(sample_sentence(sentence, slot_lists={"num": num_list})) == {
"run test 1",
"run test 2",
"run test 3",
}
def test_list_range_missing_language():
sentence = parse_sentence("run test {num}")
num_list = RangeSlotList(name=None, start=1, stop=3, words=True)
# Range slot digits cannot be converted to words without a language available.
assert set(sample_sentence(sentence, slot_lists={"num": num_list})) == {
"run test 1",
"run test 2",
"run test 3",
}
def test_list_range_words():
sentence = parse_sentence("run test {num}")
num_list = RangeSlotList(name=None, start=1, stop=3, words=True)
assert set(
sample_sentence(sentence, slot_lists={"num": num_list}, language="en")
) == {
"run test 1",
"run test one",
"run test 2",
"run test two",
"run test 3",
"run test three",
}
def test_list_range_halves_words():
sentence = parse_sentence("run test {num}")
num_list = RangeSlotList(
name=None, start=1, stop=1, fraction_type=RangeFractionType.HALVES, words=True
)
assert set(
sample_sentence(sentence, slot_lists={"num": num_list}, language="en")
) == {
"run test 1",
"run test one",
"run test 1.5",
"run test one point five",
}
def test_list_range_tenths_words():
sentence = parse_sentence("run test {num}")
num_list = RangeSlotList(
name=None, start=1, stop=1, fraction_type=RangeFractionType.TENTHS, words=True
)
assert set(
sample_sentence(sentence, slot_lists={"num": num_list}, language="en")
) == {
"run test 1",
"run test one",
"run test 1.1",
"run test one point one",
"run test 1.2",
"run test one point two",
"run test 1.3",
"run test one point three",
"run test 1.4",
"run test one point four",
"run test 1.5",
"run test one point five",
"run test 1.6",
"run test one point six",
"run test 1.7",
"run test one point seven",
"run test 1.8",
"run test one point eight",
"run test 1.9",
"run test one point nine",
}
def test_rule():
sentence = parse_sentence("turn off <area>")
assert set(
sample_sentence(
sentence,
expansion_rules={"area": parse_sentence("[the] kitchen")},
)
) == {"turn off kitchen", "turn off the kitchen"}
def test_permutation():
assert set(sample_sentence(parse_sentence("a;b;[c] d"))) == {
"a b d",
"a b c d",
"a c d b",
"a d b",
"b a d",
"b a c d",
"b c d a",
"b d a",
"c d a b",
"c d b a",
"d a b",
"d b a",
}
|