def _back_to_the_future( tracker: DialogueStateTracker, again: bool = False) -> Optional[DialogueStateTracker]: """Send Marty to the past to get the new featurization for the future""" idx_of_first_action = None idx_of_second_action = None # we need to find second executed action for e_i, event in enumerate(tracker.applied_events()): # find second ActionExecuted if isinstance(event, ActionExecuted): if idx_of_first_action is None: idx_of_first_action = e_i else: idx_of_second_action = e_i break # use first action, if we went first time and second action, if we went again idx_to_use = idx_of_second_action if again else idx_of_first_action if idx_to_use is None: return # make second ActionExecuted the first one events = tracker.applied_events()[idx_to_use:] if not events: return mcfly_tracker = tracker.init_copy() for e in events: mcfly_tracker.update(e) return mcfly_tracker
def _strip_leading_events_until_action_executed( tracker: DialogueStateTracker, again: bool = False) -> Optional[DialogueStateTracker]: """Truncates the tracker to begin at the next `ActionExecuted` event. Args: tracker: The tracker to truncate. again: When true, truncate tracker at the second action. Otherwise truncate to the first action. Returns: The truncated tracker if there were actions present. If none are found, returns `None`. """ idx_of_first_action = None idx_of_second_action = None applied_events = tracker.applied_events() # we need to find second executed action for e_i, event in enumerate(applied_events): if isinstance(event, ActionExecuted): if idx_of_first_action is None: idx_of_first_action = e_i else: idx_of_second_action = e_i break # use first action, if we went first time and second action, if we went again idx_to_use = idx_of_second_action if again else idx_of_first_action if idx_to_use is None: return None # make second ActionExecuted the first one events = applied_events[idx_to_use:] if not events: return None truncated_tracker = tracker.init_copy() for e in events: truncated_tracker.update(e) return truncated_tracker
def _trim_tracker_by_max_history( tracker: DialogueStateTracker, max_history: Optional[int], ) -> DialogueStateTracker: """Removes events from the tracker until it has `max_history` actions. Args: tracker: Some tracker. max_history: Number of actions to keep. Returns: A new tracker with up to `max_history` actions, or the same tracker if `max_history` is `None`. """ max_applied_events = _get_max_applied_events_for_max_history(tracker, max_history) if not max_applied_events: return tracker applied_events = tracker.applied_events()[-max_applied_events:] new_tracker = tracker.init_copy() for event in applied_events: new_tracker.update(event) return new_tracker
def test_tracker_init_copy(default_domain: Domain): sender_id = "some-id" tracker = DialogueStateTracker(sender_id, default_domain.slots) tracker_copy = tracker.init_copy() assert tracker.sender_id == tracker_copy.sender_id