def new_facade():
    facade = \
        Facade("tests/unit-testing/cli/interactive_commands/toilets.chatette",
               "tests/unit-testing/cli/interactive_commands/", None, False,
               None)
    facade.run_parsing()
    return facade
def get_facade():
    global FACADE
    if FACADE is None:
        FACADE = \
            Facade("tests/unit-testing/cli/interactive_commands/toilets.chatette",
                   "tests/unit-testing/cli/interactive_commands/", None, False,
                   None)
        FACADE.run_parsing()
    return FACADE
Пример #3
0
    def _createDatasetFromTemplate(self):
        with tempfile.TemporaryDirectory() as tmpdirname:
            generator = Facade(master_file_path=self.base_file,
                               output_dir_path=tmpdirname,
                               force_overwriting=True)
            generator.run()

            with open(tmpdirname + '/train/output.json') as dataset:
                dataset = json.load(dataset)
        self.dataset = dataset
Пример #4
0
    def _add_rule(self, unit_type, unit_name, variation_name, rule_str):
        parser = Facade.get_or_create().parser
        rule_tokens = parser.lexer.lex("\t" + rule_str)
        rule = parser._parse_rule(rule_tokens[1:])

        unit = AST.get_or_create()[unit_type][unit_name]
        unit.add_rule(rule, variation_name)

        self.print_wrapper.write(
            "Rule successfully added to " + unit_type.name + " '" + \
            unit_name + "'."
        )
Пример #5
0
def main():
    argument_parser = make_argument_parser()
    if len(sys.argv[1:]) == 0:
        argument_parser.print_help()
        argument_parser.exit()

    args = argument_parser.parse_args()

    if not args.interactive_mode and args.interactive_commands_file is None:
        facade = Facade.get_or_create_from_args(args)
        facade.run()
    else:
        cli = CommandLineInterpreter(args)
        cli.wait_for_input()
Пример #6
0
    def __init__(self, args):
        commands_file_path = args.interactive_commands_file
        self._dont_enter_interactive_mode = True
        if args.input is None:
            self.facade = None
        else:
            self.facade = Facade.get_or_create_from_args(args)

        self.introduce()
        if commands_file_path is not None:
            self._dont_enter_interactive_mode = \
                self._execute_commands_file(commands_file_path)
        else:
            self._dont_enter_interactive_mode = False
Пример #7
0
def get_facade():
    if not Facade.was_instantiated():
        facade = \
            Facade(
                "tests/unit-testing/cli/interactive_commands/toilets.chatette",
                "tests/unit-testing/cli/interactive_commands/", None, False,
                None
            )
        facade.run_parsing()
    return Facade.get_or_create()
Пример #8
0
def new_facade():
    if Facade.was_instantiated():
        print("reset facade")
        facade = \
            Facade.reset_system(
                "tests/unit-testing/cli/interactive_commands/toilets.chatette",
                "tests/unit-testing/cli/interactive_commands/", None, False,
                None
            )
    else:
        print("new facade")
        facade = \
            Facade(
                "tests/unit-testing/cli/interactive_commands/toilets.chatette",
                "tests/unit-testing/cli/interactive_commands/", None, False,
                None
            )
    facade.run_parsing()
    return Facade.get_or_create()
Пример #9
0
    def execute(self):
        """
        Implements the command `parse`,
        parsing a new template file using the current parser.
        """
        if len(self.command_tokens) <= 1:
            self.print_wrapper.error_log("Missing template file path\nUsage: " +
                                         "'parse <file_path>'")
            return
        file_path = self.command_tokens[1]

        if Facade.was_instantiated():
            facade = Facade.get_or_create()
            facade.parse_file(file_path)
        else:
            facade = Facade(file_path)
            facade.run_parsing()
Пример #10
0
def main():
    # pylint: disable=bad-continuation
    argument_parser = argparse.ArgumentParser(
        description="Chatette v"+__version__+" -- " +
                    "Generates NLU datasets from template files",
        epilog="SimGus -- 2018 -- Released under MIT license",
        prog="Chatette",
        add_help=True)

    argument_parser.add_argument("input", type=str,
                                 help="Path to master template file")

    argument_parser.add_argument("-v", "--version", action="version",
                                 version="%(prog)s v"+__version__,
                                 help="Print the version number of the module")

    argument_parser.add_argument("-o", "--out", dest="output", required=False,
                                 type=str, default=None,
                                 help="Output directory path")
    argument_parser.add_argument("-l", "--local", dest="local", required=False,
                                 action="store_true", default=False,
                                 help="Change the base directory for output " +
                                      "files from the current working directory " +
                                      "to the directory containing the template " +
                                      "file")

    argument_parser.add_argument("-a", "--adapter", dest="adapter", required=False,
                                 type=str, default="rasa",
                                 help="Write adapter. Possible values: " +
                                      "['rasa', 'jsonl']")
    argument_parser.add_argument("--base-file", dest="base_filepath", required=False,
                                type=str, default=None,
                                help="Path to base file to extend with examples " +
                                     "and synonyms. Only with Rasa adapter.")

    argument_parser.add_argument("-s", "--seed", dest="seed", required=False,
                                 type=str, default=None,
                                 help="Seed for the random generator " +
                                      "(any string without spaces will work)")
                                      
    argument_parser.add_argument("-i", "--interactive", dest="interactive_mode",
                                 required=False, action="store_true",
                                 default=False,
                                 help="Runs Chatette in interactive mode")
    argument_parser.add_argument("-I", "--interactive-commands-file",
                                 dest="interactive_commands_file",
                                 required=False, default=None, type=str,
                                 help="Path to a file containing interactive " +
                                      "mode commands that will be directly run")
    
    argument_parser.add_argument("-f", "--force", dest="force", required=False,
                                 action="store_true", default=False,
                                 help="Don't ask for confirmation before " +
                                      "overwriting files and folders")

    if len(sys.argv[1:]) == 0:
        argument_parser.print_help()
        argument_parser.exit()

    args = argument_parser.parse_args()

    facade = Facade.get_or_create_from_args(args)
    if not args.interactive_mode and args.interactive_commands_file is None:
        facade.run()
    else:
        cli = CommandLineInterpreter(facade, args.interactive_commands_file)
        cli.wait_for_input()
Пример #11
0
def generate(file):
    facade = Facade.get_or_create(directory_chatette + '/' + file,
                                  directory_chatette + '/output',
                                  adapter_str="rasa")
    facade.run()
Пример #12
0
    def execute(self):
        """
        Implements the command `generate` which generates all possible examples
        of a certain unit, formatted according to a certain adapter.
        """
        if not Facade.was_instantiated():
            self.print_wrapper.error_log(
                "Cannot generate anything. No file was parsed.")
            return
        facade = Facade.get_or_create()
        if len(self.command_tokens) == 1:
            facade.run_generation()
            return
        if len(self.command_tokens) == 2:
            try:
                facade.run_generation(self.command_tokens[1])
            except ValueError:
                self.print_wrapper.write("Unknown adapter: '" +
                                         self.command_tokens[1] + "'")
            return
        if len(self.command_tokens) < 4:
            self.print_wrapper.error_log("Missing some arguments\nUsage: " +
                                         self.usage_str)
            return

        adapter_str = self.command_tokens[1]
        try:
            adapter = create_adapter(adapter_str)
        except ValueError:
            self.print_wrapper.error_log("Unknown adapter '" + adapter_str +
                                         "'.")

        if len(self.command_tokens) == 5:
            try:
                self.nb_examples = int(self.command_tokens[-1])
            except ValueError:
                self.print_wrapper.error_log(
                    "The number of examples to be generated is invalid: " + \
                    "it must be an integer (no other characters allowed)."
                )
                return

        unit_type = \
            CommandStrategy.get_unit_type_from_str(self.command_tokens[2])
        if unit_type is None:
            self.print_wrapper.error_log("Unknown unit type: '" +
                                         str(self.command_tokens[2]) + "'.")
            return

        unit_regex = self.get_regex_name(self.command_tokens[3])
        if unit_regex is None:
            print("no regex")
            try:
                [unit_name, variation_name] = \
                    CommandStrategy.split_exact_unit_name(
                        self.command_tokens[3]
                    )
            except SyntaxError:
                self.print_wrapper.error_log(
                    "Unit identifier couldn't be interpreted. " + \
                    "Did you mean to escape some hashtags '#'?"
                )
                return
            self._generate_unit(adapter, unit_type, unit_name, variation_name)
        else:
            count = 0
            for unit_name in self.next_matching_unit_name(
                    unit_type, unit_regex):
                self._generate_unit(adapter, unit_type, unit_name)
                count += 1
            if count == 0:
                self.print_wrapper.write("No " + unit_type.name + " matched.")
        self.finish_execution()