def render(self, template_name, context=None, index=None): """ Given a template name, pick a template and render it using the context. If no matching template exists use template_name as template. Args: template_name (str): the name of a template group. context (dict): dictionary representing values to be rendered index (int): optional, the specific index in the collection of templates Returns: str: the rendered string """ context = context or {} if template_name not in self.templates: # When not found, return the name itself as the dialog # This allows things like render("record.not.found") to either # find a translation file "record.not.found.dialog" or return # "record not found" literal. return template_name.replace(".", " ") template_functions = self.templates.get(template_name) if index is None: line = random.choice(template_functions) else: line = template_functions[index % len(template_functions)] # Replace {key} in line with matching values from context line = line.format(**context) line = random.choice(expand_options(line)) return line
def render(self, template_name, context=None, index=None): """ Given a template name, pick a template and render it using the context. If no matching template exists use template_name as template. Args: template_name (str): the name of a template group. context (dict): dictionary representing values to be rendered index (int): optional, the specific index in the collection of templates Returns: str: the rendered string """ context = context or {} if template_name not in self.templates: # When not found, return the name itself as the dialog # This allows things like render("record.not.found") to either # find a translation file "record.not.found.dialog" or return # "record not found" literal. return template_name.replace(".", " ") template_functions = self.templates.get(template_name) if index is None: line = random.choice(template_functions) else: line = template_functions[index % len(template_functions)] # Replace {key} in line with matching values from context line = line.format(**context) line = random.choice(expand_options(line)) return line
def render(self, template_name, context=None, index=None): """ Given a template name, pick a template and render it using the context. If no matching template exists use template_name as template. Tries not to let Mycroft say exactly the same thing twice in a row. Args: template_name (str): the name of a template group. context (dict): dictionary representing values to be rendered index (int): optional, the specific index in the collection of templates Returns: str: the rendered string """ context = context or {} if template_name not in self.templates: # When not found, return the name itself as the dialog # This allows things like render("record.not.found") to either # find a translation file "record.not.found.dialog" or return # "record not found" literal. return template_name.replace('.', ' ') # Get the .dialog file's contents, minus any which have been spoken # recently. template_functions = self.templates.get(template_name) if index is None: template_functions = ([ t for t in template_functions if t not in self.recent_phrases ] or template_functions) line = random.choice(template_functions) else: line = template_functions[index % len(template_functions)] # Replace {key} in line with matching values from context line = line.format(**context) line = random.choice(expand_options(line)) # Here's where we keep track of what we've said recently. Remember, # this is by line in the .dialog file, not by exact phrase self.recent_phrases.append(line) if (len(self.recent_phrases) > min( self.max_recent_phrases, len(self.templates.get(template_name)) - self.loop_prevention_offset)): self.recent_phrases.pop(0) return line
def read_vocab_file(path): """ Read voc file. This reads a .voc file, stripping out empty lines comments and expand parentheses. It retruns each line as a list of all expanded alternatives. Arguments: path (str): path to vocab file. Returns: List of Lists of strings. """ vocab = [] with open(path, 'r', encoding='utf8') as voc_file: for line in voc_file.readlines(): if line.startswith('#') or line.strip() == '': continue vocab.append(expand_options(line.lower())) return vocab
def read_vocab_file(path): """ Read voc file. This reads a .voc file, stripping out empty lines comments and expand parentheses. It retruns each line as a list of all expanded alternatives. Arguments: path (str): path to vocab file. Returns: List of Lists of strings. """ vocab = [] with open(path, 'r', encoding='utf8') as voc_file: for line in voc_file.readlines(): if line.startswith('#') or line.strip() == '': continue vocab.append(expand_options(line)) return vocab
def load_dialog_list(skill, dialog): """ Load dialog from files into a single list. Args: skill (MycroftSkill): skill to load dialog from dialog (list): Dialog names (str) to load Returns: list: Expanded dialog strings """ dialogs = [] try: for d in dialog: for e in skill.dialog_renderer.templates[d]: dialogs += expand_options(e) except Exception as template_load_exception: print(color.FAIL + "Failed to load dialog template " + "'dialog/en-us/" + d + ".dialog'" + color.RESET) raise Exception("Can't load 'excepted_dialog': " "file '" + d + ".dialog'") \ from template_load_exception return dialogs
def load_dialog_list(skill, dialog): """ Load dialog from files into a single list. Args: skill (MycroftSkill): skill to load dialog from dialog (list): Dialog names (str) to load Returns: list: Expanded dialog strings """ dialogs = [] try: for d in dialog: for e in skill.dialog_renderer.templates[d]: dialogs += expand_options(e) except Exception as template_load_exception: print(color.FAIL + "Failed to load dialog template " + "'dialog/en-us/" + d + ".dialog'" + color.RESET) raise Exception("Can't load 'excepted_dialog': " "file '" + d + ".dialog'") \ from template_load_exception return dialogs