def process_user_utterance(user_utterance: UserUttered) -> OrderedDict: """Converts a single user utterance into an ordered dict. Args: user_utterance: Original user utterance object. Returns: Dict with a user utterance. """ result = CommentedMap() result[KEY_USER_INTENT] = user_utterance.intent["name"] if hasattr(user_utterance, "inline_comment"): result.yaml_add_eol_comment( user_utterance.inline_comment(), KEY_USER_INTENT ) if ( YAMLStoryWriter._text_is_real_message(user_utterance) and user_utterance.text ): result[KEY_USER_MESSAGE] = LiteralScalarString(user_utterance.text) if len(user_utterance.entities): entities = [] for entity in user_utterance.entities: if entity["value"]: entities.append(OrderedDict([(entity["entity"], entity["value"])])) else: entities.append(entity["entity"]) result[KEY_ENTITIES] = entities return result
def _insert_to_cfg(self, cfg: Union[CM, CS], key: str, block: Dict[str, Any], value: Any) -> None: """Private function used to Insert configuration in final config CM/CS block. :param cfg: Block where the new configuration should be placed. :param key: Key for store :param block: Block where of source data :param value: Value """ val = block["properties"][key] p_title = val.get("title", key) p_descr = val.get("description", None) p_required = self._get_required(key, block) if isinstance(cfg, CM): cfg[key] = value cfg.yaml_add_eol_comment(f"[{p_required}], {p_title}, {p_descr}", key=key) if isinstance(cfg, CS): if isinstance(value, dict): new_dict = CM() cfg.append(new_dict) new_dict[key] = value new_dict.yaml_add_eol_comment( f"[{p_required}], {p_title}, {p_descr}", key=key) else: cfg.append(value) cfg.yaml_add_eol_comment( f"[{p_required}], {p_title}, {p_descr}", column=len(cfg) - 1)
def process_action(action: ActionExecuted) -> Optional[OrderedDict]: """Converts a single action into an ordered dict. Args: action: Original action object. Returns: Dict with an action. """ if action.action_name == rasa.shared.core.constants.RULE_SNIPPET_ACTION_NAME: return None result = CommentedMap() if action.action_name: result[KEY_ACTION] = action.action_name elif action.action_text: result[KEY_BOT_END_TO_END_MESSAGE] = action.action_text if hasattr(action, "inline_comment"): if KEY_ACTION in result: result.yaml_add_eol_comment(action.inline_comment(), KEY_ACTION) elif KEY_BOT_END_TO_END_MESSAGE in result: result.yaml_add_eol_comment(action.inline_comment(), KEY_BOT_END_TO_END_MESSAGE) return result
def process_action(action: ActionExecuted) -> Optional[OrderedDict]: """Converts a single action into an ordered dict. Args: action: Original action object. Returns: Dict with an action. """ if action.action_name == rasa.shared.core.constants.RULE_SNIPPET_ACTION_NAME: return None result = CommentedMap() if action.action_name: result[KEY_ACTION] = action.action_name elif action.action_text: result[KEY_BOT_END_TO_END_MESSAGE] = action.action_text if hasattr(action, "inline_comment"): # FIXME: to fix this type issue, WarningPredictedAction needs to # be imported but it's currently outside of `rasa.shared` comment = action.inline_comment() # type: ignore[attr-defined] if KEY_ACTION in result and comment: result.yaml_add_eol_comment(comment, KEY_ACTION) elif KEY_BOT_END_TO_END_MESSAGE in result and comment: result.yaml_add_eol_comment(comment, KEY_BOT_END_TO_END_MESSAGE) return result
def convert_to_yaml_struct(self): x = CommentedMap() a = CommentedMap() x[data.name] = a x.yaml_add_eol_comment('this is the name', 'boby', 11) a['age'] = data.age a.yaml_add_eol_comment('in years', 'age', 11) print('>>>', x.ca.items) return x
def _convert_to_yaml_struct(self, indent=32): x = CommentedMap() for name, field in self._fields.items(): x[name] = field.data lines = field.long_description().splitlines() if lines: first, extra = lines[0], lines[1:] description = os.linesep.join( [first] + [" " * indent + "# " + line for line in extra]) x.yaml_add_eol_comment(description, name, indent) return x
def process_action(action: ActionExecuted) -> OrderedDict: """Converts a single action into an ordered dict. Args: action: Original action object. Returns: Dict with an action. """ result = CommentedMap() result[KEY_ACTION] = action.action_name if hasattr(action, "inline_comment"): result.yaml_add_eol_comment(action.inline_comment(), KEY_ACTION) return result
def process_user_utterance( user_utterance: UserUttered, is_test_story: bool = False ) -> OrderedDict: """Converts a single user utterance into an ordered dict. Args: user_utterance: Original user utterance object. is_test_story: Identifies if the user utterance should be added to the final YAML or not. Returns: Dict with a user utterance. """ result = CommentedMap() if user_utterance.intent_name and not user_utterance.use_text_for_featurization: result[KEY_USER_INTENT] = user_utterance.intent_name if hasattr(user_utterance, "inline_comment"): result.yaml_add_eol_comment( user_utterance.inline_comment(), KEY_USER_INTENT ) if user_utterance.text and ( # We only print the utterance text if it was an end-to-end prediction user_utterance.use_text_for_featurization # or if we want to print a conversation test story. or is_test_story ): result[KEY_USER_MESSAGE] = LiteralScalarString( rasa.shared.core.events.format_message( user_utterance.text, user_utterance.intent_name, user_utterance.entities, ) ) if len(user_utterance.entities) and not is_test_story: entities = [] for entity in user_utterance.entities: if entity["value"]: entities.append(OrderedDict([(entity["entity"], entity["value"])])) else: entities.append(entity["entity"]) result[KEY_ENTITIES] = entities return result
class YAMLRoundtripConfig(MutableConfigFile, MutableAbstractItemAccessMixin, MutableAbstractDictFunctionsMixin): """ Class for YAML-based (roundtrip) configurations """ def __init__(self, owner: Any, manager: "m.StorageManager", path: str, *args: List[Any], **kwargs: Dict[Any, Any]): self.data = CommentedMap() super().__init__(owner, manager, path, *args, **kwargs) def load(self): with open(self.path, "r") as fh: self.data = yaml.round_trip_load(fh, version=(1, 2)) def reload(self): self.unload() self.load() def unload(self): self.data.clear() def save(self): if not self.mutable: raise RuntimeError("You may not modify a defaults file at runtime - check the mutable attribute!") with open(self.path, "w") as fh: yaml.round_trip_dump(self.data, fh) # region: CommentedMap functions def insert(self, pos, key, value, *, comment=None): """ Insert a `key: value` pair at the given position, attaching a comment if provided Wrapper for `CommentedMap.insert()` """ return self.data.insert(pos, key, value, comment) def add_eol_comment(self, comment, *, key=NoComment, column=30): """ Add an end-of-line comment for a key at a particular column (30 by default) Wrapper for `CommentedMap.yaml_add_eol_comment()` """ # Setting the column to None as the API actually defaults to will raise an exception, so we have to # specify one unfortunately return self.data.yaml_add_eol_comment(comment, key=key, column=column) def set_comment_before_key(self, key, comment, *, indent=0): """ Set a comment before a given key Wrapper for `CommentedMap.yaml_set_comment_before_after_key()` """ return self.data.yaml_set_comment_before_after_key( key, before=comment, indent=indent, after=None, after_indent=None ) def set_start_comment(self, comment, indent=0): """ Set the starting comment Wrapper for `CommentedMap.yaml_set_start_comment()` """ return self.data.yaml_set_start_comment(comment, indent=indent) # endregion # region: Dict functions def clear(self): return self.data.clear() def copy(self): return self.data.copy() def get(self, key, default=None): return self.data.get(key, default) def items(self): return self.data.items() def keys(self): return self.data.keys() def pop(self, key, default=None): return self.data.pop(key, default) def popitem(self): return self.data.popitem() def setdefault(self, key, default=None): if key not in self.data: self.data[key] = default return default return self.data[key] def update(self, other): return self.data.update(other) def values(self): return self.data.values() # endregion # Item access functions def __contains__(self, key): """ Wrapper for `dict.__contains__()` """ return self.data.__contains__(key) def __delitem__(self, key): """ Wrapper for `dict.__delitem__()` """ del self.data[key] def __getitem__(self, key): """ Wrapper for `dict.__getitem__()` """ return self.data.__getitem__(key) def __iter__(self): """ Wrapper for `dict.__iter__()` """ return self.data.__iter__() def __len__(self): """ Wrapper for `dict.__len__()` """ return self.data.__len__() def __setitem__(self, key, value): """ Wrapper for `dict.__getitem__()` """ return self.data.__setitem__(key, value)
def to_file(self, filename, overwrite=False): r""" Write the initial conditions information to a file. Parameters ---------- filename : string The file to write the initial conditions information to. overwrite : boolean, optional If True, overwrite a file with the same name. Default: False """ if os.path.exists(filename) and not overwrite: raise RuntimeError(f"{filename} exists and overwrite=False!") from ruamel.yaml.comments import CommentedMap out = CommentedMap() out["basename"] = self.basename out.yaml_add_eol_comment("base name for ICs", key="basename") out["num_halos"] = self.num_halos out.yaml_add_eol_comment("number of halos", key='num_halos') out["profile1"] = self.profiles[0] out.yaml_add_eol_comment("profile for cluster 1", key='profile1') out["center1"] = self.center[0].tolist() out.yaml_add_eol_comment("center for cluster 1", key='center1') out["velocity1"] = self.velocity[0].tolist() out.yaml_add_eol_comment("velocity for cluster 1", key='velocity1') if self.particle_files[0] is not None: out["particle_file1"] = self.particle_files[0] out.yaml_add_eol_comment("particle file for cluster 1", key='particle_file1') if self.num_halos > 1: out["profile2"] = self.profiles[1] out.yaml_add_eol_comment("profile for cluster 2", key='profile2') out["center2"] = self.center[1].tolist() out.yaml_add_eol_comment("center for cluster 2", key='center2') out["velocity2"] = self.velocity[1].tolist() out.yaml_add_eol_comment("velocity for cluster 2", key='velocity2') if self.particle_files[1] is not None: out["particle_file2"] = self.particle_files[1] out.yaml_add_eol_comment("particle file for cluster 2", key='particle_file2') if self.num_halos == 3: out["profile3"] = self.profiles[2] out.yaml_add_eol_comment("profile for cluster 3", key='profile3') out["center3"] = self.center[2].tolist() out.yaml_add_eol_comment("center for cluster 3", key='center3') out["velocity3"] = self.velocity[2].tolist() out.yaml_add_eol_comment("velocity for cluster 3", key='velocity3') if self.particle_files[2] is not None: out["particle_file3"] = self.particle_files[2] out.yaml_add_eol_comment("particle file for cluster 3", key='particle_file3') if self.tot_np.get("dm", 0) > 0: out["num_dm_particles"] = self.tot_np["dm"] out.yaml_add_eol_comment("number of DM particles", key='num_dm_particles') if self.tot_np.get("gas", 0) > 0: out["num_gas_particles"] = self.tot_np["gas"] out.yaml_add_eol_comment("number of gas particles", key='num_gas_particles') if self.tot_np.get("star", 0) > 0: out["num_star_particles"] = self.tot_np["star"] out.yaml_add_eol_comment("number of star particles", key='num_star_particles') if self.mag_file is not None: out["mag_file"] = self.mag_file out.yaml_add_eol_comment("3D magnetic field file", key='mag_file') out["r_max"] = self.r_max out.yaml_add_eol_comment("Maximum radius of particles", key='r_max') yaml = YAML() with open(filename, "w") as f: yaml.dump(out, f)
def process_user_utterance(user_utterance: UserUttered, is_test_story: bool = False) -> OrderedDict: """Converts a single user utterance into an ordered dict. Args: user_utterance: Original user utterance object. is_test_story: Identifies if the user utterance should be added to the final YAML or not. Returns: Dict with a user utterance. """ result = CommentedMap() if user_utterance.intent_name and not user_utterance.use_text_for_featurization: result[KEY_USER_INTENT] = ( user_utterance.full_retrieval_intent_name if user_utterance.full_retrieval_intent_name else user_utterance.intent_name) entities = [] if len(user_utterance.entities) and not is_test_story: for entity in user_utterance.entities: if "value" in entity: if hasattr(user_utterance, "inline_comment_for_entity"): # FIXME: to fix this type issue, WronglyClassifiedUserUtterance # needs to be imported but it's currently outside # of `rasa.shared` for predicted in user_utterance.predicted_entities: # type: ignore[attr-defined] # noqa: E501 if predicted["start"] == entity["start"]: commented_entity = user_utterance.inline_comment_for_entity( # type: ignore[attr-defined] # noqa: E501 predicted, entity) if commented_entity: entity_map = CommentedMap([ (entity["entity"], entity["value"]) ]) entity_map.yaml_add_eol_comment( commented_entity, entity["entity"]) entities.append(entity_map) else: entities.append( OrderedDict([(entity["entity"], entity["value"])])) else: entities.append( OrderedDict([(entity["entity"], entity["value"])])) else: entities.append(entity["entity"]) result[KEY_ENTITIES] = entities if hasattr(user_utterance, "inline_comment"): # FIXME: to fix this type issue, WronglyClassifiedUserUtterance needs to # be imported but it's currently outside of `rasa.shared` comment = user_utterance.inline_comment( # type: ignore[attr-defined] force_comment_generation=not entities) if comment: result.yaml_add_eol_comment(comment, KEY_USER_INTENT) if user_utterance.text and ( # We only print the utterance text if it was an end-to-end prediction user_utterance.use_text_for_featurization # or if we want to print a conversation test story. or is_test_story): result[KEY_USER_MESSAGE] = LiteralScalarString( rasa.shared.core.events.format_message( user_utterance.text, user_utterance.intent_name, user_utterance.entities, )) return result