From ffcd0d70c22e250741a005792bd9b1c88e6702ad Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Wed, 17 Apr 2019 11:58:40 +0200 Subject: [PATCH] OSC required for server and better variable matching for unfinished sentences Fix #31 --- hugvey/story.py | 23 +++++++++++++++++------ requirements.server.txt | 5 ++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/hugvey/story.py b/hugvey/story.py index 22e82d7..34395fb 100644 --- a/hugvey/story.py +++ b/hugvey/story.py @@ -92,9 +92,9 @@ class Message(object): self.variableValues[name] = value - self.logger.warn(f"Set variable, now fetch {name}") + self.logger.warn(f"Set variable, fetch {name}") if not None in self.variableValues.values(): - self.logger.warn(f"now fetch indeed {name}") + self.logger.warn(f"now fetch {name}") asyncio.get_event_loop().create_task(self.getAudioFilePath()) # asyncio.get_event_loop().call_soon_threadsafe(self.getAudioFilePath) self.logger.warn(f"started {name}") @@ -103,7 +103,7 @@ class Message(object): # sort reverse to avoid replacing the wrong variable self.variables.sort(key=len, reverse=True) text = self.text - self.logger.debug(f"Getting text for {self.id}") +# self.logger.debug(f"Getting text for {self.id}") for var in self.variables: self.logger.debug(f"try replacing ${var} with {self.variableValues[var]} in {text}") replacement = self.variableValues[var] if (self.variableValues[var] is not None) else "nothing" #TODO: translate nothing to each language @@ -319,6 +319,8 @@ class Condition(object): if not r or not r.hasUtterances(): return False + capturedVariables = None + if 'regex' in self.vars and len(self.vars['regex']): if 'regexCompiled' not in self.vars: # Compile once, as we probably run it more than once @@ -332,11 +334,12 @@ class Condition(object): return False story.logger.debug('Got match on {}'.format(self.vars['regex'])) + capturedVariables = result.groupdict() + if ('instantMatch' in self.vars and self.vars['instantMatch']) or not r.isSpeaking(): # try to avoid setting variables for intermediate strings - results = result.groupdict() - for captureGroup in results: - story.setVariableValue(captureGroup, results[captureGroup]) + for captureGroup in capturedVariables: + story.setVariableValue(captureGroup, capturedVariables[captureGroup]) if 'instantMatch' in self.vars and self.vars['instantMatch']: story.logger.info(f"Instant match on {self.vars['regex']}, {self.vars}") @@ -358,6 +361,11 @@ class Condition(object): timeSinceReply = story.timer.getElapsed() - r.getLastUtterance().lastUpdateTime story.logger.log(LOG_BS, f"check delay duration is now {replyDuration}, already waiting for {timeSinceReply}, have to wait {delay['waitTime']}") if timeSinceReply > float(delay['waitTime']): + # if variables are captured, only set them the moment the condition matches + if capturedVariables is not None: + for captureGroup in capturedVariables: + story.setVariableValue(captureGroup, capturedVariables[captureGroup]) + return True break # don't check other delays # wait for delay to match @@ -610,6 +618,9 @@ class Story(object): def setVariableValue(self, name, value): if name not in self.variables: self.logger.warn(f"Set variable that is not needed in the story: {name}") + else: + self.logger.debug(f"Set variable {name} to {value}") + self.variableValues[name] = value if name not in self.variables: diff --git a/requirements.server.txt b/requirements.server.txt index 4b9299c..a3c4869 100644 --- a/requirements.server.txt +++ b/requirements.server.txt @@ -7,4 +7,7 @@ requests-threads fabric cutelog tornado -shortuuid \ No newline at end of file +shortuuid +python-osc + +