-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrain.py
88 lines (66 loc) · 2.04 KB
/
brain.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
88
import json
import random
from random import randint
import pyttsx3
import speech_recognition as sr
import os
import time
import datetime
data = {}
engine = pyttsx3.init()
engine.setProperty("rate", 150)
def identityName():
with open("memory.json") as json_file:
data = json.load(json_file)
memory = data["identity"]["name"]
return memory
def identityNickname():
with open("memory.json") as json_file:
data = json.load(json_file)
memory = data["identity"]["nickname"]
return memory
def speak(audio):
engine.say(audio)
engine.runAndWait()
def wishMe():
hour = int(datetime.datetime.now().hour)
print(hour)
if hour>= 5 and hour<12:
speak(random_welcome('morning'))
elif hour>= 12 and hour<18:
speak(random_welcome('afternoon'))
else:
speak(random_welcome('evening'))
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language ='en-in')
print(f"User said: {query}\n")
except Exception as e:
print(e)
print("Unable to recognize your voice.")
return "None"
return query
def random_welcome(category):
ctg = category
with open('memory.json') as json_file:
data = json.load(json_file)
memory = data["welcome"][ctg]
random_index = randint(0,len(memory)-1)
return memory[random_index]
def keyword_handler(sentence):
name = identityName()
nickname = identityNickname()
text = sentence.lower()
text = text.split(" ")
if name or nickname in text:
try:
pos = text.index(name)
except:
pos = text.index(nickname)
return pos