def do_intents_to_graph(intents, slot_names, targets): sentences, replacements = ini_jsgf.split_rules(intents) # Load slot values for slot_name in slot_names: slot_path = slots_dir / slot_name assert slot_path.is_file(), f"Missing slot file at {slot_path}" # Parse each non-empty line as a JSGF sentence slot_values = [] with open(slot_path, "r") as slot_file: for line in slot_file: line = line.strip() if line: sentence = jsgf.Sentence.parse(line) slot_values.append(sentence) # Replace $slot with sentences replacements[f"${slot_name}"] = slot_values if profile.get("intent.replace_numbers", True): # Replace numbers in parsed sentences for intent_sentences in sentences.values(): for sentence in intent_sentences: jsgf.walk_expression(sentence, number_transform, replacements) # Convert to directed graph graph = intents_to_graph(intents, replacements) # Write graph to JSON file json_graph = graph_to_json(graph) with open(targets[0], "w") as graph_file: json.dump(json_graph, graph_file)
def do_intents_to_graph(sentences, slot_names, replacements, targets): # Replace actual numbers if profile.get("intent.replace_numbers", True): # Replace numbers in parsed sentences for intent_sentences in sentences.values(): for sentence in intent_sentences: jsgf.walk_expression(sentence, number_transform, replacements) # Determine whether word casing has to be fixed transform = None if word_casing == "upper": transform = str.upper elif word_casing == "lower": transform = str.lower if transform: def fix_case(word): if isinstance(word, jsgf.Word): word.text = transform(word.text) return word # Fix casing for intent_sentences in sentences.values(): for sentence in intent_sentences: jsgf.walk_expression(sentence, fix_case, replacements) # Convert to directed graph graph = intents_to_graph(sentences, replacements) # Write graph to JSON file json_graph = graph_to_json(graph) with open(targets[0], "w") as graph_file: json.dump(json_graph, graph_file)
async def handle_nlu_train( self, train: NluTrain, site_id: str = "default" ) -> typing.AsyncIterable[ typing.Union[typing.Tuple[NluTrainSuccess, TopicArgs], NluError] ]: """Re-trains NLU system""" try: # Load gzipped graph pickle _LOGGER.debug("Loading %s", train.graph_path) with gzip.GzipFile(train.graph_path, mode="rb") as graph_gzip: intent_graph = nx.readwrite.gpickle.read_gpickle(graph_gzip) # Get JSON intent graph json_graph = rhasspynlu.graph_to_json(intent_graph) if self.nlu_train_url: # Remote NLU server _LOGGER.debug(self.nlu_train_url) async with self.http_session.post( self.nlu_train_url, json=json_graph, ssl=self.ssl_context ) as response: # No data expected in response response.raise_for_status() elif self.nlu_train_command: # Local NLU training command _LOGGER.debug(self.nlu_train_command) proc = await asyncio.create_subprocess_exec( *self.nlu_train_command, stdin=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, ) output, error = await proc.communicate(json.dumps(json_graph).encode()) if output: _LOGGER.debug(output.decode()) if error: _LOGGER.debug(error.decode()) else: _LOGGER.warning("Can't train NLU system. No train URL or command.") # Report success yield (NluTrainSuccess(id=train.id), {"site_id": site_id}) except Exception as e: _LOGGER.exception("handle_nlu_train") yield NluError( error=str(e), context=f"url='{self.nlu_train_url}', command='{self.nlu_train_command}'", site_id=site_id, session_id=train.id, )
def do_intents_to_graph(sentences, slot_names, replacements, targets): # Replace actual numbers if profile.get("intent.replace_numbers", True): # Replace numbers in parsed sentences for intent_sentences in sentences.values(): for sentence in intent_sentences: jsgf.walk_expression(sentence, number_transform, replacements) if word_transform: # Fix casing for intent_sentences in sentences.values(): for sentence in intent_sentences: jsgf.walk_expression(sentence, fix_word_case, replacements) # Convert to directed graph graph = intents_to_graph(sentences, replacements) # Write graph to JSON file json_graph = graph_to_json(graph) with open(targets[0], "w") as graph_file: json.dump(json_graph, graph_file)