| 1 |
from hassil.util import ( |
| 2 |
is_template, |
| 3 |
merge_dict, |
| 4 |
normalize_for_matching, |
| 5 |
normalize_text, |
| 6 |
normalize_whitespace, |
| 7 |
remove_escapes, |
| 8 |
remove_punctuation, |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
def test_merge_dict(): |
| 13 |
base_dict = {"a": 1, "list": [1], "dict": {"a": 1}} |
| 14 |
merge_dict(base_dict, {"a": 2, "list": [2], "dict": {"b": 2}}) |
| 15 |
assert base_dict == {"a": 2, "list": [1, 2], "dict": {"a": 1, "b": 2}} |
| 16 |
|
| 17 |
|
| 18 |
def test_remove_escapes(): |
| 19 |
assert remove_escapes("\\[test\\]") == "[test]" |
| 20 |
|
| 21 |
|
| 22 |
def test_normalize_whitespace(): |
| 23 |
assert normalize_whitespace("this is a test") == "this is a test" |
| 24 |
|
| 25 |
|
| 26 |
def test_normalize_text(): |
| 27 |
assert normalize_text("tHIS is A Test") == "tHIS is A Test" |
| 28 |
assert normalize_text("what’s") == "what's" |
| 29 |
|
| 30 |
|
| 31 |
def test_is_template(): |
| 32 |
assert not is_template("just some plain text") |
| 33 |
assert is_template("[optional] word") |
| 34 |
assert is_template("a {list}") |
| 35 |
assert is_template("a <rule>") |
| 36 |
assert is_template("(a group)") |
| 37 |
assert is_template("an | alternative") |
| 38 |
|
| 39 |
|
| 40 |
def test_remove_punctuation(): |
| 41 |
assert remove_punctuation("test") == "test" |
| 42 |
assert remove_punctuation("test.") == "test" |
| 43 |
assert remove_punctuation("A.C.") == "A.C." |
| 44 |
assert remove_punctuation("A.C") == "A.C" |
| 45 |
assert remove_punctuation("turn A.C. off") == "turn A.C. off" |
| 46 |
# A trailing initialism is preserved even after other words, matching the |
| 47 |
# standalone case ("A.C." above) so input/slot normalization stays in sync. |
| 48 |
assert remove_punctuation("turn off A.C.") == "turn off A.C." |
| 49 |
assert remove_punctuation("Main St. next") == "Main St next" |
| 50 |
assert remove_punctuation("Chambre d'Ariane") == "Chambre d'Ariane" |
| 51 |
assert remove_punctuation("Chambre d’Ariane") == "Chambre d’Ariane" |
| 52 |
|
| 53 |
|
| 54 |
def test_normalize_for_matching_apostrophes(): |
| 55 |
"""Both apostrophes normalize the same way, whatever follows them.""" |
| 56 |
# Templates only ever contain "'", so "’" has to reach the matcher as "'" |
| 57 |
# rather than being stripped as punctuation. |
| 58 |
assert normalize_for_matching("nell'ingresso").text == "nell'ingresso" |
| 59 |
assert normalize_for_matching("nell’ingresso").text == "nell'ingresso" |
| 60 |
assert normalize_for_matching("nell' ingresso").text == "nell' ingresso" |
| 61 |
assert normalize_for_matching("nell’ ingresso").text == "nell' ingresso" |
| 62 |
|
| 63 |
# Punctuation removal still happens afterwards. |
| 64 |
assert normalize_for_matching("ciao, come va?").text == "ciao come va" |
| 65 |
|
| 66 |
|
| 67 |
def test_remove_dashes(): |
| 68 |
"""Speech-to-text engines sometimes append a dash to the transcript.""" |
| 69 |
assert remove_punctuation("we are going to bed—") == "we are going to bed" |
| 70 |
assert remove_punctuation("we are going to bed–") == "we are going to bed" |
| 71 |
assert remove_punctuation("we are going to bed―") == "we are going to bed" |
| 72 |
assert remove_punctuation("—we are going to bed") == "we are going to bed" |
| 73 |
assert remove_punctuation("we are going to bed…") == "we are going to bed" |
| 74 |
|
| 75 |
# Hyphens and minus signs are kept: they are part of words and numbers. |
| 76 |
assert remove_punctuation("turn on the air-conditioner") == ( |
| 77 |
"turn on the air-conditioner" |
| 78 |
) |
| 79 |
assert remove_punctuation("set temperature to -5 degrees") == ( |
| 80 |
"set temperature to -5 degrees" |
| 81 |
) |
| 82 |
assert remove_punctuation("set temperature to −5 degrees") == ( |
| 83 |
"set temperature to −5 degrees" |
| 84 |
) |
| 85 |
|
| 86 |
# Dashes inside a word are left alone. |
| 87 |
assert remove_punctuation("bed—time") == "bed—time" |
| 88 |
assert remove_punctuation("5–10 minutes") == "5–10 minutes" |