Skip to content

Commit c63a26c

Browse files
committed
Refactoring
1 parent e73e881 commit c63a26c

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

pyttman/core/entity_parsing/parsers.py

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def __init__(self,
2828
span: int | typing.Callable = 0,
2929
identifier: Type[Identifier] | None = None,
3030
exclude: typing.Iterable[str] = None,
31+
as_list: bool = False,
3132
**kwargs):
3233

3334
if hasattr(self, "value"):
@@ -45,6 +46,7 @@ def __init__(self,
4546
self.default = default if default is not None else None
4647
self.identifier = identifier
4748
self.span = span
49+
self.as_list = as_list
4850
self._properties_for_evaluation = {
4951
"prefixes": self.prefixes,
5052
"suffixes": self.suffixes,
@@ -131,6 +133,14 @@ def parse_message(self, message: MessageMixin,
131133
self.value = Entity(output.pop())
132134
else:
133135
self.value = Entity(self.default, is_fallback_default=True)
136+
137+
if self.value:
138+
entity = self.value
139+
if isinstance(entity.value, list):
140+
[message.content.remove(i) for i in entity.value]
141+
entity.index_in_message += len(entity.value)
142+
elif isinstance(entity.value, str):
143+
message.content.remove(self.value)
134144
return
135145

136146
if self.truncates_message_in_parsing is False:
@@ -139,7 +149,6 @@ def parse_message(self, message: MessageMixin,
139149
for i, _ in enumerate(message.content):
140150
parsed_entity: Entity = self._identify_value(message,
141151
start_index=i)
142-
143152
# An entity has been identified, and it's unique.
144153
if parsed_entity is not None and memoization.get(
145154
parsed_entity.index_in_message) is None:
@@ -294,43 +303,43 @@ def _identify_value(self, message: MessageMixin,
294303
# for each span iteration as the walk in the message progresses.
295304
# If an Identifier is does not comply with a string, the walk is
296305
# cancelled.
297-
if parsed_entity is not None:
298-
while parsed_entity.value.casefold() in self.exclude:
299-
parsed_entity.index_in_message += 1
300-
# Traverse the message for as long as the current found
301-
# entity is in the 'exclude' tuple. If the end of message
302-
# is reached, quietly break the loop.
303-
try:
304-
parsed_entity.value = message.content[
305-
parsed_entity.index_in_message]
306-
except IndexError:
307-
return None
308-
309-
current_index = parsed_entity.index_in_message
306+
if parsed_entity is None:
307+
return parsed_entity
308+
309+
while parsed_entity.value.casefold() in self.exclude:
310+
parsed_entity.index_in_message += 1
311+
# Traverse the message for as long as the current found
312+
# entity is in the 'exclude' tuple. If the end of message
313+
# is reached, quietly break the loop.
314+
try:
315+
parsed_entity.value = message.content[
316+
parsed_entity.index_in_message]
317+
except IndexError:
318+
return None
310319

311-
for i in range(1, self.span):
312-
try:
313-
current_index += 1
314-
if self.identifier:
315-
identifier_object: Identifier = self.identifier(
316-
start_index=current_index)
317-
# Identifier did not find
318-
span_entity = identifier_object.try_identify_entity(
319-
message)
320-
if span_entity is None or span_entity.index_in_message != current_index:
321-
break
322-
span_value = span_entity.value
323-
else:
324-
span_value = message.content[current_index]
325-
326-
# There are not enough elements in message.content to walk
327-
# as far as the span property requests - abort.
328-
except IndexError:
329-
break
320+
# Now, add words for as long as `span` allows us to iterate.
321+
for i in range(1, self.span):
322+
parsed_entity.index_in_message += 1
323+
try:
324+
if self.identifier:
325+
identifier_object: Identifier = self.identifier(
326+
start_index=parsed_entity.index_in_message)
327+
# Identifier did not find
328+
span_entity = identifier_object.try_identify_entity(
329+
message)
330+
if span_entity is None or span_entity.index_in_message != parsed_entity.index_in_message:
331+
break
332+
span_value = span_entity.value
330333
else:
331-
if span_value not in self.exclude:
332-
print(f"{span_value} is not in {self.exclude} for {self}")
333-
parsed_entity.value += f" {span_value}"
334+
span_value = message.content[parsed_entity.index_in_message]
335+
336+
# There are not enough elements in message.content to walk
337+
# as far as the span property requests - abort.
338+
except IndexError:
339+
break
340+
else:
341+
if span_value not in self.exclude:
342+
parsed_entity.value += f" {span_value}"
334343
return parsed_entity
335344

336345

tests/core/entity_parsing/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class ImplementedTestIntent(Intent):
2020
with the intent to be tested.
2121
"""
2222
def respond(self, message: Message) -> Reply | ReplyStream:
23-
return Reply(f"'{self.__class__.__name__}' matched a message")
23+
return Reply(f"'{self.__class__.__name__}' "
24+
f"matched message: {message}")
2425

2526

2627
class PyttmanInternalTestBaseCase(PyttmanInternalBaseTestCase):

0 commit comments

Comments
 (0)