| 1 |
from hassil import is_match, parse_sentence |
| 2 |
from hassil.intents import TextSlotList |
| 3 |
|
| 4 |
|
| 5 |
def test_no_match(): |
| 6 |
sentence = parse_sentence("turn on the lights") |
| 7 |
assert is_match("turn on the lights", sentence) |
| 8 |
assert not is_match("turn off the lights", sentence) |
| 9 |
assert not is_match("don't turn on the lights", sentence) |
| 10 |
|
| 11 |
|
| 12 |
def test_punctuation(): |
| 13 |
sentence = parse_sentence("turn on the lights") |
| 14 |
assert is_match("turn on the lights.", sentence) |
| 15 |
assert is_match("turn on the lights!", sentence) |
| 16 |
|
| 17 |
|
| 18 |
def test_dash(): |
| 19 |
"""Test that a dash appended by speech-to-text does not break matching.""" |
| 20 |
sentence = parse_sentence("we are going to bed") |
| 21 |
assert is_match("we are going to bed—", sentence) |
| 22 |
assert is_match("we are going to bed –", sentence) |
| 23 |
|
| 24 |
|
| 25 |
def test_whitespace(): |
| 26 |
sentence = parse_sentence("turn on the lights") |
| 27 |
assert is_match(" turn on the lights", sentence) |
| 28 |
|
| 29 |
|
| 30 |
def test_skip_punctuation(): |
| 31 |
sentence = parse_sentence("turn on the lights") |
| 32 |
assert is_match("turn ! on ? the, lights.", sentence) |
| 33 |
|
| 34 |
|
| 35 |
def test_skip_words(): |
| 36 |
sentence = parse_sentence("turn on [the] lights") |
| 37 |
skip_words = {"please", "could", "you", "my"} |
| 38 |
assert is_match( |
| 39 |
"could you please turn on my lights?", sentence, skip_words=skip_words |
| 40 |
) |
| 41 |
assert is_match("turn on the lights, please", sentence, skip_words=skip_words) |
| 42 |
|
| 43 |
|
| 44 |
def test_optional(): |
| 45 |
sentence = parse_sentence("turn on [the] lights in [the] kitchen") |
| 46 |
assert is_match("turn on the lights in the kitchen", sentence) |
| 47 |
assert is_match("turn on lights in kitchen", sentence) |
| 48 |
|
| 49 |
|
| 50 |
def test_optional_plural(): |
| 51 |
sentence = parse_sentence("turn on the light[s]") |
| 52 |
assert is_match("turn on the light", sentence) |
| 53 |
assert is_match("turn on the lights", sentence) |
| 54 |
|
| 55 |
|
| 56 |
def test_group_plural(): |
| 57 |
sentence = parse_sentence("give me the penn(y|ies)") |
| 58 |
assert is_match("give me the penny", sentence) |
| 59 |
assert is_match("give me the pennies", sentence) |
| 60 |
|
| 61 |
|
| 62 |
def test_list(): |
| 63 |
sentence = parse_sentence("turn off {area}") |
| 64 |
areas = TextSlotList.from_strings(["kitchen", "living room"]) |
| 65 |
assert is_match("turn off kitchen", sentence, slot_lists={"area": areas}) |
| 66 |
assert is_match("turn off living room", sentence, slot_lists={"area": areas}) |
| 67 |
|
| 68 |
|
| 69 |
def test_list_prefix_suffix(): |
| 70 |
sentence = parse_sentence("turn off abc-{area}-123") |
| 71 |
areas = TextSlotList.from_strings(["kitchen", "living room"]) |
| 72 |
assert is_match("turn off abc-kitchen-123", sentence, slot_lists={"area": areas}) |
| 73 |
assert is_match( |
| 74 |
"turn off abc-living room-123", sentence, slot_lists={"area": areas} |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
def test_rule(): |
| 79 |
sentence = parse_sentence("turn off <area>") |
| 80 |
assert is_match( |
| 81 |
"turn off kitchen", |
| 82 |
sentence, |
| 83 |
expansion_rules={"area": parse_sentence("[the] kitchen")}, |
| 84 |
) |
| 85 |
|
| 86 |
|
| 87 |
def test_rule_prefix_suffix(): |
| 88 |
sentence = parse_sentence("turn off abc-<area>-123") |
| 89 |
assert is_match( |
| 90 |
"turn off abc-kitchen-123", |
| 91 |
sentence, |
| 92 |
expansion_rules={"area": parse_sentence("[the ]kitchen")}, |
| 93 |
) |
| 94 |
|
| 95 |
|
| 96 |
def test_alternative_whitespace(): |
| 97 |
sentence = parse_sentence("(start|stopp)ed") |
| 98 |
assert is_match("started", sentence) |
| 99 |
assert is_match("stopped", sentence) |
| 100 |
|
| 101 |
|
| 102 |
def test_alternative_whitespace_2(): |
| 103 |
sentence = parse_sentence("set brightness to ( minimum | lowest)") |
| 104 |
assert is_match("set brightness to lowest", sentence) |
| 105 |
|
| 106 |
|
| 107 |
def test_no_allow_template(): |
| 108 |
sentence = parse_sentence("turn off {name}") |
| 109 |
names = TextSlotList.from_strings(["light[s]"]) |
| 110 |
assert is_match("turn off lights", sentence, slot_lists={"name": names}) |
| 111 |
|
| 112 |
names = TextSlotList.from_strings(["light[s]"], allow_template=False) |
| 113 |
assert not is_match("turn off lights", sentence, slot_lists={"name": names}) |
| 114 |
assert is_match("turn off light[s]", sentence, slot_lists={"name": names}) |
| 115 |
|
| 116 |
|
| 117 |
def test_no_whitespace_fails(): |
| 118 |
sentence = parse_sentence("this is a test") |
| 119 |
assert not is_match("thisisatest", sentence) |
| 120 |
|
| 121 |
|
| 122 |
def test_permutations(): |
| 123 |
sentence = parse_sentence("(in the kitchen;is there smoke)") |
| 124 |
assert is_match("in the kitchen is there smoke", sentence) |
| 125 |
assert is_match("is there smoke in the kitchen", sentence) |
| 126 |
|
| 127 |
sentence = parse_sentence("(a;b;c)") |
| 128 |
assert is_match("a b c", sentence) |
| 129 |
assert is_match("a c b", sentence) |
| 130 |
assert is_match("b a c", sentence) |
| 131 |
assert is_match("b c a", sentence) |
| 132 |
assert is_match("c a b", sentence) |
| 133 |
assert is_match("c b a", sentence) |
| 134 |
|
| 135 |
|
| 136 |
def test_nl_optional_whitespace(): |
| 137 |
sentence = parse_sentence( |
| 138 |
"[<doe>] (alle|in) <area>[ ]<lamp> aan [willen | kunnen] [<doe>]" |
| 139 |
) |
| 140 |
slot_lists = { |
| 141 |
"area": TextSlotList.from_strings(["Keuken", "Woonkamer"], allow_template=False) |
| 142 |
} |
| 143 |
expansion_rules = { |
| 144 |
"area": parse_sentence("[de|het] {area}"), |
| 145 |
"doe": parse_sentence("(zet|mag|mogen|doe|verander|maak|schakel)"), |
| 146 |
"lamp": parse_sentence("[de|het] (lamp[en]|licht[en]|verlichting)"), |
| 147 |
} |
| 148 |
|
| 149 |
for text in [ |
| 150 |
"Mogen in de keuken de lampen aan?", |
| 151 |
"Mogen in de keukenlampen aan?", |
| 152 |
]: |
| 153 |
assert is_match( |
| 154 |
text, |
| 155 |
sentence, |
| 156 |
slot_lists=slot_lists, |
| 157 |
expansion_rules=expansion_rules, |
| 158 |
) |