-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjarviss.py
178 lines (152 loc) · 6.69 KB
/
jarviss.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import datetime,calendar
import webbrowser
import wikipedia
import speech_recognition as sr
import pyttsx3
import os
import pickle
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import calendar_event as ce
count=0
engine = pyttsx3.init('sapi5') # sapi5 is microsoft speech API
voices = engine.getProperty('voices')
#print(voices[0].id)
engine.setProperty('voice', voices[len(voices)-1].id) # setting voice to engine either voices[0] or [1].
def speak(audio):
engine.say(audio)
engine.runAndWait()
def wishme():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour<12:
speak("Good Morning, Sir!")
elif hour>=12 and hour<=18:
speak("Good Afternoon, Sir!")
else:
speak("good evening, sir!")
speak("How may i help you")
def take_command():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
r.energy_threshold = 350
audio = r.listen(source)
try:
query = r.recognize_google(audio,language='en-in')
print('You said : {}\n'.format(query))
except:
speak("Sorry coud not recognize your voice, Say that again please!")
return "None"
print("Listened Your Command")
return query
def remove_stopwords(query):
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(query)
not_require = ["I","He","She","This","That","Would","would","like","please"]
filtered_sentence = [i for i in word_tokens if not i in not_require]
filtered_sentences = [w for w in filtered_sentence if not w in stop_words]
return filtered_sentences
if __name__ == '__main__':
wishme()
while(1):
query = take_command().lower()
if "wikipedia" in query:
speak("Searching Wikipdia...")
query = query.replace("wikipedia","")
results = wikipedia.summary(query,sentences=2)
speak("According to wikipedia")
print(results)
speak(results)
elif "open youtube" in query:
webbrowser.open("youtube.com")
elif "open google" in query:
webbrowser.open("google.com")
elif "open stackoverflow" in query:
webbrowser.open("stackoverflow.com")
elif "search" in query:
query = query.replace("hello jarvis can you","")
query = query.replace("search","")
if "for me" in query:
query = query.replace("for me","")
url = "https://www.google.co.in/search?q=" +query+ "&oq="+query+"&gs_l=serp.12..0i71l8.0.0.0.6391.0.0.0.0.0.0.0.0..0.0....0...1c..64.serp..0.0.0.UiQhpfaBsuU"
webbrowser.open_new(url)
elif "play movie" in query:
speak("Which movie you like to watch, from available ones")
q= take_command().lower()
video_dir = r"C:\Users\Asus\Downloads\Video"
video_list = os.listdir(video_dir)
query_list = remove_stopwords(q)
print(query_list)
for j in query_list:
for i in video_list:
if i.lower().find(j)>=0:
count+=1
print("i am in")
os.startfile(os.path.join(video_dir,i))
break
else:
continue
if count==0:
speak("Sorry Sir, Video not found!")
elif "the time" in query:
hour = int(datetime.datetime.now().hour)
time = datetime.datetime.now().strftime("%I:%M:%S") # strftime("%I:%M:%S") is for formatting the time. %I is use for 12-hour format and %H is for 24 hour format.
if hour>=12:
speak(f"Sir,the time is {time}PM")
else:
speak(f"Sir,the time is {time}AM")
elif "the date" in query:
date = datetime.date.today()
speak(str(date))
elif "set" in query and "event" in query:
speak("Sure Sir! What's the title of the event?")
summary = take_command().lower()
while(1):
speak("Ok! When is the event?")
start_date_str = take_command().lower()
if int(datetime.datetime.strftime(list(ce.datefinder.find_dates(start_date_str))[0],"%H%S"))==0: # this is done for checking if the previous command contains time as well or not
speak("What's the time of the event?")
start_time_string = take_command().lower()
start_time_str = start_date_str+start_time_string
else:
start_time_str=start_date_str
catch_event = ce.create_event(start_time_str,summary,duration=1)
if catch_event != 'None':
speak("Alright! the respected event has been added to the Google calendar, you will be notify by an email before 24 hours")
break
else:
speak("Sorry, couldn't recognize date and time correctly..")
continue
elif "show" in query and "event" in query:
events_lst = ce.get_event()
if len(events_lst)==1:
speak(f"I have found one event...That is..")
speak(events_lst[0])
elif len(events_lst) > 1:
speak(f"I have found {len(events_lst)} events...The First one is..")
for i in range(len(events_lst)):
speak(events_lst[i])
if i!=(len(events_lst)-1):
speak("Next One is.")
else:
speak("Sorry, You don't have any event in your calendar.")
elif "update" in query and "event" in query:
speak("What's the new title sir!")
summary = take_command().lower()
ce.update_event(summary)
elif "delete" in query and "event" in query:
speak("Are you sure you wanna delete all events?")
clear_str = take_command().lower()
if "yes" in clear_str or "sure" in clear_str:
speak("Alright! Deleting..")
clear_catch = ce.clear_events()
if len(clear_catch)==0:
speak("Events Deleted")
else:
speak("Sorry, Can't delete your events..There Might be some problems in API servers.")
else:
speak("Okay! I haven't done that yet.")
if query.find("exit alexa") or query.find("go alexa") or query.find("take rest") or query.find("turn off")== 0:
break
print("done")