Skip to content

Commit e7fc347

Browse files
committed
Merge branch 'release/1.2.0.2' into main
2 parents 92b835b + 79a19f1 commit e7fc347

File tree

5 files changed

+76
-8
lines changed

5 files changed

+76
-8
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Pyttman Changelog
22

3+
4+
# V 1.2.0.2
5+
6+
This is a hotfix release, fixing an issue with EntityFields with `valid_strings`
7+
configured combined with 'suffixes' and/or 'prefixes',
8+
where the 'prefixes' and/or 'suffixes' were ignored, if an entity matched
9+
a string mentioned in 'valid_strings'.
10+
11+
12+
313
# V 1.2.0.1
414

515
This is a hotfix release, fixing an issue with EntityFields with `as_list`

pyttman/core/entity_parsing/parsers.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,11 @@ def _prepare_params(self):
8686
f"Don't forget the trailing comma, "
8787
f"example: '(1,)' instead of '(1)'.")
8888

89-
if (
90-
self.valid_strings is not None
91-
) and (
92-
isinstance(self.valid_strings, typing.Sequence) is False
93-
):
89+
if self.valid_strings is not None and not isinstance(self.valid_strings, typing.Sequence):
9490
raise AttributeError("'valid_strings' must be a collection of "
9591
f"strings, got: '{self.valid_strings}'")
9692
else:
97-
self.valid_strings = tuple(
98-
[i.casefold() for i in self.valid_strings])
93+
self.valid_strings = tuple([i.casefold() for i in self.valid_strings])
9994

10095
def reset(self) -> None:
10196
"""
@@ -134,6 +129,8 @@ def parse_message(self, message: MessageMixin,
134129
else:
135130
self.value = Entity(self.default, is_fallback_default=True)
136131

132+
self._validate_prefixes_suffixes(message)
133+
137134
if self.value:
138135
entity = self.value
139136
if isinstance(entity.value, list):
@@ -157,6 +154,35 @@ def parse_message(self, message: MessageMixin,
157154
else:
158155
self.reset()
159156

157+
def _validate_message_with_affixes(self,
158+
affixes: tuple[str],
159+
message: MessageMixin,
160+
comparator: callable):
161+
entity = self.value
162+
if not (common_strings := set(affixes).intersection(message.content)):
163+
entity.value = self.default
164+
return
165+
for string in common_strings:
166+
if not comparator(message.content.index(string), entity.index_in_message):
167+
entity.value = self.default
168+
169+
def _validate_prefixes_suffixes(self, message: MessageMixin):
170+
"""
171+
Check 'prefixes' and 'suffixes' for Entity values to make sure
172+
that they comply
173+
:return:
174+
"""
175+
if self.prefixes:
176+
self._validate_message_with_affixes(
177+
self.prefixes,
178+
message,
179+
lambda affix_index, value_index: affix_index < value_index)
180+
if self.suffixes:
181+
self._validate_message_with_affixes(
182+
self.suffixes,
183+
message,
184+
lambda affix_index, value_index: affix_index > value_index)
185+
160186
def _identify_value(self, message: MessageMixin,
161187
start_index: int = 0) -> Union[None, Entity]:
162188
"""

pyttman/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
__version__ = "1.2.0.1"
2+
__version__ = "1.2.0.2a"

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
author="Simon Olofsson",
2121
project_urls={
2222
"Bug Tracker": "https://github.com/dotchetter/Pyttman/issues",
23+
"Changelog": "https://github.com/dotchetter/Pyttman/blob/main/CHANGELOG.md"
2324
},
2425
author_email="[email protected]",
2526
license="MIT",

tests/core/entity_parsing/test_entity_fields/test_entity_parsing.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,37 @@ class IntentClass(ImplementedTestIntent):
6868
grass_color = TextEntityField(suffixes=("grass",))
6969

7070

71+
class PyttmanIntentInternalEntityParserTestTwoTextFieldsWithPrefixes(
72+
PyttmanInternalTestBaseCase
73+
):
74+
process_message = True
75+
mock_message = Message("Adam and Eve were out walking on green grass")
76+
expected_entities = {
77+
"person_one_fail": None,
78+
"person_one": "Adam",
79+
"person_two": "Eve",
80+
"grass_color": "green"
81+
}
82+
83+
class IntentClass(ImplementedTestIntent):
84+
"""
85+
Test a combination of custom Identifier class for a TextEntityField
86+
with valid_strings, message_contains all in combinations.
87+
prefixed should denote that the entity isn't found.
88+
"""
89+
valid_strings = ("Adam", "Eve")
90+
lead = ("walking",)
91+
person_one_fail = TextEntityField(identifier=CapitalizedIdentifier,
92+
valid_strings=valid_strings,
93+
prefixes=("_prefix_",))
94+
person_one = TextEntityField(identifier=CapitalizedIdentifier,
95+
valid_strings=valid_strings)
96+
person_two = TextEntityField(identifier=CapitalizedIdentifier,
97+
valid_strings=valid_strings,
98+
suffixes=("were",))
99+
grass_color = TextEntityField(suffixes=("grass",))
100+
101+
71102
class PyttmanIntentInternalEntityParserTestBookKeeperApp(
72103
PyttmanInternalTestBaseCase
73104
):

0 commit comments

Comments
 (0)