Пример #1
0
    def is_yaml_nlu_file(filename: Text) -> bool:
        """Checks if the specified file possibly contains NLU training data in YAML.

        Args:
            filename: name of the file to check.

        Returns:
            `True` if the `filename` is possibly a valid YAML NLU file,
            `False` otherwise.
        """
        if not data.is_likely_yaml_file(filename):
            return False

        try:
            content = io_utils.read_yaml_file(filename)

            return any(key in content for key in {KEY_NLU, KEY_RESPONSES})
        except (YAMLError, Warning) as e:
            logger.error(
                f"Tried to check if '{filename}' is an NLU file, but failed to "
                f"read it. If this file contains NLU data, you should "
                f"investigate this error, otherwise it is probably best to "
                f"move the file to a different location. "
                f"Error: {e}")
            return False
Пример #2
0
 def persist_nlg(self, filename: Text) -> None:
     if data.is_likely_yaml_file(filename):
         rasa.nlu.utils.write_to_file(filename, self.nlg_as_yaml())
     elif data.is_likely_markdown_file(filename):
         nlg_serialized_data = self.nlg_as_markdown()
         if nlg_serialized_data:
             rasa.nlu.utils.write_to_file(filename, nlg_serialized_data)
     else:
         ValueError(
             "Unsupported file format detected. Supported file formats are 'md' "
             "and 'yml'.")
Пример #3
0
    def persist_nlu(self, filename: Text = DEFAULT_TRAINING_DATA_OUTPUT_PATH) -> None:

        if data.is_likely_json_file(filename):
            rasa.nlu.utils.write_to_file(filename, self.nlu_as_json(indent=2))
        elif data.is_likely_markdown_file(filename):
            rasa.nlu.utils.write_to_file(filename, self.nlu_as_markdown())
        elif data.is_likely_yaml_file(filename):
            rasa.nlu.utils.write_to_file(filename, self.nlu_as_yaml())
        else:
            ValueError(
                "Unsupported file format detected. Supported file formats are 'json' "
                "and 'md'."
            )
Пример #4
0
def _get_reader(
    filename: Text,
    domain: Domain,
    template_variables: Optional[Dict] = None,
    use_e2e: bool = False,
) -> StoryReader:

    if data.is_likely_markdown_file(filename):
        return MarkdownStoryReader(domain, template_variables, use_e2e,
                                   filename)
    elif data.is_likely_yaml_file(filename):
        return YAMLStoryReader(domain, template_variables, use_e2e, filename)
    else:
        # This is a use case for uploading the story over REST API.
        # The source file has a random name.
        return _guess_reader(filename, domain, template_variables, use_e2e)
Пример #5
0
    def is_domain_file(filename: Text) -> bool:
        """Checks whether the given file path is a Rasa domain file.

        Args:
            filename: Path of the file which should be checked.

        Returns:
            `True` if it's a domain file, otherwise `False`.
        """
        from rasa.data import is_likely_yaml_file

        if not is_likely_yaml_file(filename):
            return False
        try:
            content = rasa.utils.io.read_yaml_file(filename)
            if any(key in content for key in ALL_DOMAIN_KEYS):
                return True
        except YAMLError:
            pass

        return False
Пример #6
0
def test_is_yaml_file(path, is_yaml):
    assert data.is_likely_yaml_file(path) == is_yaml