Skip to content

Commit

Permalink
Merge pull request #182 from home-assistant/synesthesiam-20241202-unm…
Browse files Browse the repository at this point in the history
…atched-text-chunks

Fix counting of text chunks with unmatched entities
  • Loading branch information
synesthesiam authored Dec 2, 2024
2 parents af22163 + 622c21f commit a4e31c7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion hassil/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.4
2.0.5
4 changes: 2 additions & 2 deletions hassil/string_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ def match_expression(
entities=context.entities,
intent_context=context.intent_context,
is_start_of_word=True,
# unmatched_entities=context.unmatched_entities,
text_chunks_matched=context.text_chunks_matched,
text_chunks_matched=context.text_chunks_matched
+ len(chunk.text.strip()),
intent_sentence=context.intent_sentence,
intent_data=context.intent_data,
#
Expand Down
32 changes: 32 additions & 0 deletions tests/test_recognize.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,38 @@ def test_unmatched_entities_cant_skip_words() -> None:
assert len(results) == 0


def test_unmatched_entities_text_chunks_matched() -> None:
yaml_text = """
language: "en"
intents:
Test:
data:
- sentences:
- "[turn] on {name}"
- "[turn] on {name} light"
lists:
name:
values:
- test
"""

with io.StringIO(yaml_text) as test_file:
intents = Intents.from_yaml(test_file)

sentence = "turn on unknown light"

results = list(recognize_all(sentence, intents, allow_unmatched_entities=True))
assert len(results) == 2

# '[turn] on {name} light' should have more literal text matched
result_1, result_2 = results
assert result_1.intent_sentence is not None
if result_1.intent_sentence.text == "[turn] on {name}":
assert result_1.text_chunks_matched < result_2.text_chunks_matched
else:
assert result_2.text_chunks_matched < result_1.text_chunks_matched


def test_wildcard() -> None:
"""Test wildcard slot lists/entities."""
yaml_text = """
Expand Down

0 comments on commit a4e31c7

Please sign in to comment.