Astd minor fix in core.dialog

Assign hardcoded value from MustacheDialogRenderer.render() to variable.
Fix its value to properly accommodate short dialog files.

Remove superfluous check on lines 104-105 of mycroft.dialog.init

====  Tech Notes ====
The new dialog logic, which prevents Mycroft from repeating itself, uses
a list of recently-spoken lines from the skill's .dialog files.
The size of this list is capped, currently at 3. However, .dialog files
can be shorter than 3 entries. An offset is used to accommodate this.

In the previous commit, the offset was wrong. Now it's 2.

====  Documentation Notes ====
If presented with a .dialog file containing 4 or more entries,
Mycroft will avoid saying the same one for at least a few rounds. If the
dialog file contains only 3 entries, Mycroft will avoid saying the same
line twice in a row. If only 1-2 entries are present in a .dialog file,
Mycroft will just pick one and say it, without retaining a history.
pull/2297/head
ChanceNCounter 2019-09-09 16:40:09 -07:00 committed by Åke Forslund
parent 4c8ce58f8e
commit 30bd84bc85
1 changed files with 16 additions and 5 deletions

View File

@ -39,6 +39,15 @@ class MustacheDialogRenderer:
# TODO magic numbers are bad!
self.max_recent_phrases = 3
# We cycle through lines in .dialog files to keep Mycroft from
# repeating the same phrase over and over. However, if a .dialog
# file only contains a few entries, this can cause it to loop.
#
# This offset will override max_recent_phrases on very short .dialog
# files. With the offset at 2, .dialog files with 3 or more lines will
# be managed to avoid repetition, but .dialog files with only 1 or 2
# lines will be unaffected. Dialog should never get stuck in a loop.
self.loop_prevention_offset = 2
def load_template_file(self, template_name, filename):
"""
@ -92,11 +101,12 @@ class MustacheDialogRenderer:
# Get the .dialog file's contents, minus any which have been spoken
# recently.
template_functions = ([t for t in self.templates.get(template_name)
if t not in self.recent_phrases] or
self.templates.get(template_name))
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)]
@ -107,8 +117,9 @@ class MustacheDialogRenderer:
# 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)) - 1):
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