Fix for Google not always giving an is_finished

This commit is contained in:
Ruben van de Ven 2019-04-02 17:32:01 +02:00
parent f446514ebe
commit 63f6f15d0f

View file

@ -22,9 +22,11 @@ class Utterance(object):
self.startTime = startTime self.startTime = startTime
self.endTime = None self.endTime = None
self.text = "" self.text = ""
self.lastUpdate = startTime
def setText(self, text): def setText(self, text, now):
self.text = text self.text = text
self.lastUpdate = now
def setFinished(self, endTime): def setFinished(self, endTime):
self.endTime = endTime self.endTime = endTime
@ -181,7 +183,21 @@ class Reply(object):
def getLastUtterance(self) -> Utterance: def getLastUtterance(self) -> Utterance:
if not self.hasUtterances(): if not self.hasUtterances():
return None return None
return self.utterances[-1] u = self.utterances[-1] #: :type u: Utterance
# attempt to fix a glitch that google does not always send is_finished
if u.isFinished():
return u
now = self.forMessage.story.timer.getElapsed()
diff = now - u.lastUpdate
if diff > 2: # time in seconds to force silence in utterance
self.forMessage.story.logger.warn(
f"Set finish time for utterance after {diff}s {u.text}"
)
u.setFinished(now)
return u
def getFirstUtterance(self) -> Utterance: def getFirstUtterance(self) -> Utterance:
if not self.hasUtterances(): if not self.hasUtterances():
@ -766,8 +782,9 @@ class Story(object):
self.logger.info("Start speaking") self.logger.info("Start speaking")
self.currentReply= Reply(self.currentMessage) self.currentReply= Reply(self.currentMessage)
utterance = self.currentReply.getActiveUtterance(self.timer.getElapsed()) now = self.timer.getElapsed()
utterance.setText(e['transcript']) utterance = self.currentReply.getActiveUtterance(now)
utterance.setText(e['transcript'], now)
self.hugvey.eventLogger.info("speaking: content {} \"{}\"".format(id(utterance), e['transcript'])) self.hugvey.eventLogger.info("speaking: content {} \"{}\"".format(id(utterance), e['transcript']))
if e['is_final']: if e['is_final']: