def dumps(self, training_data: "TrainingData", **kwargs: Any) -> Text: """Writes Training Data to a string in json format.""" js_entity_synonyms = defaultdict(list) for k, v in training_data.entity_synonyms.items(): if k != v: js_entity_synonyms[v].append(k) formatted_synonyms = [{ "value": value, "synonyms": syns } for value, syns in js_entity_synonyms.items()] formatted_examples = [ example.as_dict_nlu() for example in training_data.training_examples ] return json_to_string( { "rasa_nlu_data": { "common_examples": formatted_examples, "regex_features": training_data.regex_features, "lookup_tables": training_data.lookup_tables, "entity_synonyms": formatted_synonyms, } }, **kwargs, )
def test_is_nlu_file_with_json(): test = { "rasa_nlu_data": { "lookup_tables": [ {"name": "plates", "elements": ["beans", "rice", "tacos", "cheese"]} ] } } directory = tempfile.mkdtemp() file = os.path.join(directory, "test.json") write_text_file(json_to_string(test), file) assert rasa.shared.data.is_nlu_file(file)
def run_cmdline(model_path: Text) -> None: """Loops over CLI input, passing each message to a loaded NLU model.""" agent = Agent.load(model_path) print_success( "NLU model loaded. Type a message and press enter to parse it.") while True: print_success("Next message:") try: message = input().strip() except (EOFError, KeyboardInterrupt): print_info("Wrapping up command line chat...") break result = asyncio.run(agent.parse_message(message)) print(json_to_string(result))
def run_cmdline( model_path: Text, component_builder: Optional["ComponentBuilder"] = None) -> None: interpreter = Interpreter.load(model_path, component_builder) regex_interpreter = RegexInterpreter() print_success( "NLU model loaded. Type a message and press enter to parse it.") while True: print_success("Next message:") message = input().strip() if message.startswith(INTENT_MESSAGE_PREFIX): loop = asyncio.get_event_loop() result = loop.run_until_complete(regex_interpreter.parse(message)) else: result = interpreter.parse(message) print(json_to_string(result))
def run_cmdline( model_path: Text, component_builder: Optional["ComponentBuilder"] = None) -> None: interpreter = Interpreter.load(model_path, component_builder) regex_interpreter = RegexInterpreter() print_success( "NLU model loaded. Type a message and press enter to parse it.") while True: print_success("Next message:") try: message = input().strip() except (EOFError, KeyboardInterrupt): print_info("Wrapping up command line chat...") break if message.startswith(INTENT_MESSAGE_PREFIX): result = rasa.utils.common.run_in_loop( regex_interpreter.parse(message)) else: result = interpreter.parse(message) print(json_to_string(result))
def view(self) -> Text: return json_to_string(self.__dict__, indent=4)
def write_json_to_file(filename: Text, obj: Any, **kwargs: Any) -> None: """Write an object as a json string to a file.""" write_to_file(filename, json_to_string(obj, **kwargs))