-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstevil.py
87 lines (70 loc) · 2.37 KB
/
stevil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from typing import Callable
import speech_recognition as sr
import pyttsx3
import spacy
import yaml
class Stevil:
recog = sr.Recognizer()
nlp = spacy.load("en_core_web_sm")
def __init__(self,
config_file_path: str = 'config.yaml', # path to the config file
threshold :int = 0.7 # the minimum similarity level for a match
):
self.config = self.parse_config(config_file_path)
self.threshold = threshold
# parse the config file and update the config with a list with language vectors
def parse_config(self,
config_file_path: str # path to the config file
):
config_file = open(config_file_path)
config = yaml.full_load(config_file)
messages = config['messages']
for i in range(len(messages)):
triggers = messages[i]['triggers']
for j in range(len(triggers)):
trigger = messages[i]['triggers'][j]
messages[i]['triggers'][j] = self.nlp(trigger)
return messages
# find the trigger that matches a phrase and return the corresponding message
def find_best_response(self,
phrase: str # what the user said
):
parsed_phrase = self.nlp(phrase)
for message in self.config:
for trigger in message['triggers']:
if parsed_phrase.similarity(trigger) > self.threshold:
return message['response']
# make stevil speak out loud
def speak(self,
phrase: str # what stevil will say
) -> None:
engine = pyttsx3.init()
engine.say(phrase)
engine.runAndWait()
# make stevil listen to the microphone and execute a function
def listen(self,
function: Callable[[str], None] = None # the function to execute
) -> None:
function = self.respond if function == None else function
try:
with sr.Microphone() as mic:
self.recog.adjust_for_ambient_noise(mic, duration=0.2) # adjust the mic
audio = self.recog.listen(mic)
text = self.recog.recognize_google(audio).lower() # use google to recognize audio
print(f'Did you say {text}, Motherf***er?')
function(text)
except sr.RequestError as e:
print("Could not request results; {0}. F*** YOU!".format(e))
except sr.UnknownValueError:
print("Unknown error occurred? Little piece of s***.")
def respond(self,
phrase: str # what the user said
) -> None:
response = self.find_best_response(phrase)
print(f'responding with "{response}"')
self.speak(response)
if __name__ == '__main__':
stevil = Stevil()
# continually listen and respond
while True:
stevil.listen()