Skip to content

Commit

Permalink
Use slot name instead of list name
Browse files Browse the repository at this point in the history
  • Loading branch information
synesthesiam committed Jul 25, 2023
1 parent ca9a9aa commit 0af1b4e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
8 changes: 4 additions & 4 deletions hassil/recognize.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ def match_expression(
is_start_of_word=context.is_start_of_word,
#
unmatched_entities=context.unmatched_entities
+ [UnmatchedTextEntity(name=list_ref.list_name, text="")],
+ [UnmatchedTextEntity(name=list_ref.slot_name, text="")],
close_wildcards=True,
)

Expand Down Expand Up @@ -886,7 +886,7 @@ def match_expression(
unmatched_entities=context.unmatched_entities
+ [
UnmatchedRangeEntity(
name=list_ref.list_name, value=word_number
name=list_ref.slot_name, value=word_number
)
],
)
Expand All @@ -900,7 +900,7 @@ def match_expression(
is_start_of_word=context.is_start_of_word,
#
unmatched_entities=context.unmatched_entities
+ [UnmatchedTextEntity(name=list_ref.list_name, text="")],
+ [UnmatchedTextEntity(name=list_ref.slot_name, text="")],
close_wildcards=True,
)
elif isinstance(slot_list, WildcardSlotList):
Expand All @@ -916,7 +916,7 @@ def match_expression(
entities=context.entities
+ [
MatchEntity(
name=list_ref.list_name, value="", text="", is_wildcard=True
name=list_ref.slot_name, value="", text="", is_wildcard=True
)
],
close_unmatched=True,
Expand Down
64 changes: 64 additions & 0 deletions tests/test_recognize.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,46 @@ def test_unmatched_entity_context() -> None:
assert name.text == "back door"


def test_unmatched_slot_name() -> None:
"""Test that unmatched entities use slot name instead of list name."""
yaml_text = """
language: "en"
intents:
Test:
data:
- sentences:
- "run {script_name:name}"
- "execute script {script_number:number}"
lists:
script_name:
values:
- stealth mode
script_number:
range:
from: 1
to: 100
"""

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

sentence = "run missing name"
result = recognize(sentence, intents, allow_unmatched_entities=True)
assert result is not None, f"{sentence} should match"
assert set(result.unmatched_entities.keys()) == {"name"}

sentence = "execute script wrong number"
result = recognize(sentence, intents, allow_unmatched_entities=True)
assert result is not None, f"{sentence} should match"
assert set(result.unmatched_entities.keys()) == {"number"}

# Outside range
sentence = "execute script 0"
result = recognize(sentence, intents, allow_unmatched_entities=True)
assert result is not None, f"{sentence} should match"
assert set(result.unmatched_entities.keys()) == {"number"}


def test_wildcard() -> None:
"""Test wildcard slot lists/entities."""
yaml_text = """
Expand Down Expand Up @@ -752,3 +792,27 @@ def test_optional_wildcard() -> None:
assert result is not None, f"{sentence} should match"
assert set(result.entities.keys()) == {"album"}
assert result.entities["album"].value == "the white album"


def test_wildcard_slot_name() -> None:
"""Test wildcard uses slot instead of list name."""
yaml_text = """
language: "en"
intents:
Test:
data:
- sentences:
- "run {script_name:name}"
lists:
script_name:
wildcard: true
"""

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

sentence = "run script 1"
result = recognize(sentence, intents)
assert result is not None, f"{sentence} should match"
assert set(result.entities.keys()) == {"name"}
assert result.entities["name"].value == "script 1"

0 comments on commit 0af1b4e

Please sign in to comment.