def get_file_format_extension(resource_name: Text) -> Text: """ Get the file extension based on training data format. It supports both a folder and a file, and tries to guess the format as follows: - if the resource is a file and has a known format, return this format's extension - if the resource is a folder and all the resources have the same known format, return it's extension - otherwise, default to DEFAULT_FILE_FORMAT (yml). Args: resource_name: The name of the resource, can be a file or a folder. Returns: The resource file format. """ from rasa.shared.nlu.training_data import loading if resource_name is None or not os.path.exists(resource_name): raise AttributeError(f"Resource '{resource_name}' does not exist.") files = rasa.shared.utils.io.list_files(resource_name) file_formats = list(map(lambda f: loading.guess_format(f), files)) if not file_formats: return rasa.shared.data.yaml_file_extension() known_file_formats = { loading.RASA_YAML: rasa.shared.data.yaml_file_extension(), } fformat = file_formats[0] if all(f == fformat for f in file_formats): return known_file_formats.get(fformat, rasa.shared.data.yaml_file_extension()) return rasa.shared.data.yaml_file_extension()
def is_nlu_file(file_path: Text) -> bool: """Checks if a file is a Rasa compatible nlu file. Args: file_path: Path of the file which should be checked. Returns: `True` if it's a nlu file, otherwise `False`. """ return nlu_loading.guess_format(file_path) != nlu_loading.UNK
def is_nlu_file(file_path: Text) -> bool: """Checks if a file is a Rasa compatible nlu file. Args: file_path: Path of the file which should be checked. Returns: `True` if it's a nlu file, otherwise `False`. """ from rasa.shared.nlu.training_data import loading as nlu_loading return nlu_loading.guess_format(file_path) != nlu_loading.UNK
def get_file_format(resource_name: Text) -> Text: from rasa.shared.nlu.training_data import loading if resource_name is None or not os.path.exists(resource_name): raise AttributeError(f"Resource '{resource_name}' does not exist.") files = rasa.shared.utils.io.list_files(resource_name) file_formats = list(map(lambda f: loading.guess_format(f), files)) if not file_formats: return "json" fformat = file_formats[0] if fformat in [loading.MARKDOWN, loading.RASA_YAML] and all( f == fformat for f in file_formats): return fformat return "json"
def test_guess_format_from_non_existing_file_path(): assert guess_format("not existing path") == UNK