2017-12-24 01:03:26 +01:00
|
|
|
import re
|
|
|
|
|
2017-12-24 02:35:45 +01:00
|
|
|
from platypush.context import get_backend
|
2017-12-24 13:15:37 +01:00
|
|
|
from platypush.message.event import Event, EventMatchResult
|
2017-12-24 01:03:26 +01:00
|
|
|
|
|
|
|
class AssistantEvent(Event):
|
|
|
|
""" Base class for assistant events """
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2017-12-24 02:35:45 +01:00
|
|
|
self._assistant = get_backend('assistant.google')
|
2017-12-24 01:03:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ConversationStartEvent(AssistantEvent):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class ConversationEndEvent(AssistantEvent):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class SpeechRecognizedEvent(AssistantEvent):
|
|
|
|
def __init__(self, phrase, *args, **kwargs):
|
|
|
|
super().__init__(phrase=phrase, *args, **kwargs)
|
|
|
|
self.recognized_phrase = phrase.strip().lower()
|
|
|
|
|
|
|
|
def matches_condition(self, condition):
|
2017-12-24 13:15:37 +01:00
|
|
|
result = EventMatchResult(is_match=False, score=0)
|
|
|
|
|
|
|
|
if not isinstance(self, condition.type): return result
|
2017-12-24 01:03:26 +01:00
|
|
|
|
|
|
|
recognized_tokens = re.split('\s+', self.recognized_phrase.strip().lower())
|
|
|
|
condition_tokens = re.split('\s+', condition.args['phrase'].strip().lower())
|
|
|
|
|
|
|
|
while recognized_tokens and condition_tokens:
|
|
|
|
rec_token = recognized_tokens[0]
|
|
|
|
cond_token = condition_tokens[0]
|
|
|
|
|
|
|
|
if rec_token == cond_token:
|
|
|
|
recognized_tokens.pop(0)
|
|
|
|
condition_tokens.pop(0)
|
2017-12-24 13:15:37 +01:00
|
|
|
result.score += 1
|
2017-12-24 01:03:26 +01:00
|
|
|
elif re.search(cond_token, rec_token):
|
|
|
|
condition_tokens.pop(0)
|
|
|
|
else:
|
|
|
|
m = re.match('^\$([\w\d_])', cond_token)
|
|
|
|
if m:
|
2017-12-24 13:15:37 +01:00
|
|
|
result.parsed_args[cond_token[1:]] = rec_token
|
2017-12-24 01:03:26 +01:00
|
|
|
recognized_tokens.pop(0)
|
|
|
|
condition_tokens.pop(0)
|
2017-12-24 13:15:37 +01:00
|
|
|
result.score += 1
|
2017-12-24 01:03:26 +01:00
|
|
|
else:
|
|
|
|
recognized_tokens.pop(0)
|
|
|
|
|
2017-12-24 13:15:37 +01:00
|
|
|
result.is_match = len(condition_tokens) == 0
|
|
|
|
if result.is_match and self._assistant: self._assistant.stop_conversation()
|
2017-12-24 02:35:45 +01:00
|
|
|
|
2017-12-24 13:15:37 +01:00
|
|
|
return result
|
2017-12-24 01:03:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|