Skip to content

Using it inside your own code

SimGus edited this page Sep 2, 2019 · 2 revisions

If you need more specific behavior, you can make calls to parts of Chatette from your own Python code. This page describes the interfaces of those different modules.

Running Chatette as a black box

If you want to call Chatette from your own code without making changes to its behavior or internal state, the simplest solution would be to a system call using the subprocess module, e.g.:

import subprocess
stdout_message = subprocess.check_output(["python", "-m", "chatette", "<options-for-chatette>"])

Using Chatette more specifically inside your code

To make the program have a more specific behavior, you can import a facade of the whole system, and then make calls to its sub-parts. You can create such an object as follows:

from chatette.facade import Facade
facade = Facade(master_file_path, output_dir_path, adapter_str, local, seed)

where master_file_path, output_dir_path, adapter_str and seed are strings and local is a boolean. adapter_str can take the same values as what you would give to the program option --adapter; having local set to True would have the same effect as using the --local program option; and seed should be the same value as what you would give the program option --seed (or None if no seed is provided). Those options are described on this page.

(NB: starting with version 1.4.1, the arguments adapter_str, local and seed are optional.)

The facade can then be used to run the parsing of different files and the generation of the examples, using the following methods:

  • facade.run() executes the parsing and the generation as a whole. This thus has the same effect as making a direct command line call to Chatette, with the options that were given to the constructor of the facade.
  • facade.run_parsing() executes the parsing of the master file that was given to the constructor (but not the generation of examples).
  • facade.parse_file(file_path) will open and parse the file at file_path. Beware that this new file will be considered the master file from now on.
  • facade.run_generation(adapter_str=None) will run the generation of examples and write it to the output directory that was provided to the constructor. If the parsing hasn't been done yet, nothing will be generated. The argument adapter_str can have the same values than that of the program option --adapter. If nothing is provided for this argument, the formatting of the output will be done using the adapter that was provided to the constructor.
  • facade.parser contains the parser that is used by the system to parse files, which itself contains the abstract representation of the information that was parsed (and that can be used to generate examples). facade.parser.intent_definitions, facade.parser.slot_definitions and facade.parser.alias_definitions are list containing respectively the definitions of intents, slots and aliases that were parsed by the parser. You can call the generate_nb_examples(number_of_examples) method on each of the elements of those list to get a list of examples that this unit would generate. Elements from the list of intent definitions also have a generate(max_number_of_examples) method that will generate the number of example that was asked in the template file for this particular intent definition.

It is also possible, but not advised, to import and use the parser, generator and adapter objects directly. Here is an example of how to do so (here to run the parsing, generation and writing of the output file, as they would run if we executed the program as a whole):

from chatette.parsing.parser import Parser
from chatette.generator import Generator
from chatette.adapters.factory import adapter_factory
# Parse the template file
parser = Parser(master_file_path)
parser.parse()
# Generate the training examples
generator = Generator()
training_examples = list(generator.generate_train())
# Write the training examples to the output file
adapter = adapter_factory("rasa")
if training_examples:
    adapter.write(os.path.join(output_dir_path, "train"),
                  train_examples, generator.get_entities_synonyms())

Note that prior to version 1.4.0, the constructor of Parser expected a _io.TextIOWrapper object rather than the path to the master file. For versions higher than 1.4.0, an error will be printed if you give it such an object.

If you need even more precise control over the program, please read the code.