예제 #1
0
    def write_training_data(nlu: TrainingData,
                            domain: Domain,
                            config: dict,
                            stories: StoryGraph,
                            rules: StoryGraph = None):
        """
        convert mongo data  to individual files

        :param nlu: nlu data
        :param domain: domain data
        :param stories: stories data
        :param config: config data
        :param rules: rules data
        :return: files path
        """
        temp_path = tempfile.mkdtemp()
        data_path = os.path.join(temp_path, DEFAULT_DATA_PATH)
        os.makedirs(data_path)
        nlu_path = os.path.join(data_path, "nlu.yml")
        domain_path = os.path.join(temp_path, DEFAULT_DOMAIN_PATH)
        stories_path = os.path.join(data_path, "stories.yml")
        config_path = os.path.join(temp_path, DEFAULT_CONFIG_PATH)
        rules_path = os.path.join(data_path, "rules.yml")

        nlu_as_str = nlu.nlu_as_yaml().encode()
        domain_as_str = domain.as_yaml().encode()
        config_as_str = yaml.dump(config).encode()

        Utility.write_to_file(nlu_path, nlu_as_str)
        Utility.write_to_file(domain_path, domain_as_str)
        Utility.write_to_file(config_path, config_as_str)
        YAMLStoryWriter().dump(stories_path, stories.story_steps)
        if rules:
            YAMLStoryWriter().dump(rules_path, rules.story_steps)
        return temp_path
예제 #2
0
    def create(bot: str, use_test_stories: bool = False):
        from kairon import Utility
        from itertools import chain
        from rasa.shared.nlu.training_data.training_data import TrainingData

        bot_home = os.path.join('testing_data', bot)
        Utility.make_dirs(bot_home)
        processor = MongoProcessor()
        intents_and_training_examples = processor.get_intents_and_training_examples(bot)
        aug_training_examples = map(lambda training_data: TestDataGenerator.__prepare_nlu(training_data[0], training_data[1]), intents_and_training_examples.items())
        messages = list(chain.from_iterable(aug_training_examples))
        nlu_data = TrainingData(training_examples=messages)
        stories = processor.load_stories(bot)
        rules = processor.get_rules_for_training(bot)
        stories = stories.merge(rules)
        if stories.is_empty() or nlu_data.is_empty():
            raise AppException('Not enough training data exists. Please add some training data.')

        nlu_as_str = nlu_data.nlu_as_yaml().encode()
        nlu_path = os.path.join(bot_home, "nlu.yml")
        Utility.write_to_file(nlu_path, nlu_as_str)

        if use_test_stories:
            stories_path = os.path.join(bot_home, "test_stories.yml")
        else:
            stories_path = os.path.join(bot_home, "stories.yml")
        YAMLStoryWriter().dump(stories_path, stories.story_steps, is_test_story=use_test_stories)
        return nlu_path, stories_path