-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathchatbot.py
285 lines (274 loc) · 9.49 KB
/
chatbot.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import random
import datetime
import webbrowser
import pyttsx3
import wikipedia
from pygame import mixer
import pyowm
import config
import speech_recognition as sr
from google_places import (
change_location_query,
filter_sentence,
get_location,
nearby_places,
requests,
)
import pyjokes
from googletrans import Translator
from voice_conf import *
# from speech_recognition.__main__ import r, audio
from intentClassification.intent_classification import IntentClassification
greetings = ['hey there', 'hello', 'hi', 'Hai', 'hey!', 'hey', 'hi there!']
question = ['How are you?', 'How are you doing?', 'What\'s up?']
responses = ['Okay', "I'm fine"]
var1 = ['who made you', 'who created you']
var2 = ['I_was_created_by_Edward_right_in_his_computer.',
'Edward', 'Some_guy_whom_i_never_got_to_know.']
var3 = ['what time is it', 'what is the time', 'time']
var4 = ['who are you', 'what is you name']
var5 = ['date', 'what is the date', 'what date is it', 'tell me the date']
cmd1 = ['open browser', 'open google']
cmd2 = ['play music', 'play songs', 'play a song', 'open music player']
cmd3 = [
'tell a joke',
'tell me a joke',
'say something funny',
'tell something funny']
cmd4 = ['open youtube', 'i want to watch a video']
cmd5 = [
'tell me the weather',
'weather',
'what about the weather',
'what\'s the weather']
cmd6 = ['exit', 'close', 'goodbye', 'nothing', 'catch you later', 'bye']
cmd7 = [
"what is your color",
"what is your colour",
"your color",
"your color?",
]
colrep = [
"Right now its rainbow",
"Right now its transparent",
"Right now its non chromatic",
]
cmd8 = ["what is you favourite colour", "what is your favourite color"]
cmd9 = ["thank you"]
repfr9 = ["youre welcome", "glad i could help you"]
intentClassifier = IntentClassification()
personalized, longitude, latitude = get_location()
stores = []
stores_data = {}
print("hi ", "Setting location through ip bias, Change location?")
change_location = False
language_conf = input('Language(en-US): ')
if language_conf == '':
language_conf = "en-US"
voice_language = getVoiceID(language_conf[:2])
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voice_language)
volume = engine.getProperty('volume')
engine.setProperty('volume', 10.0)
rate = engine.getProperty('rate')
engine.setProperty('rate', rate - 25)
translator = Translator()
while True:
speech_type = input("Speech/Text: ")
if speech_type.lower() != "speech":
translate = input("Type: ")
else:
r = sr.Recognizer()
with sr.Microphone() as source:
t = translator.translate('Say something', dest=language_conf[:2])
print(t.text)
engine.say(t.text)
engine.runAndWait()
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
translate = r.recognize_google(audio, language=language_conf)
print("You said:- " + translate)
except sr.UnknownValueError:
print("Could not understand audio")
engine.say("I didnt get that. Rerun the code")
engine.runAndWait()
intent = intentClassifier.intent_identifier(translate)
print("Intent:", intent)
# TODO:: entity based weather output
if intent == "weather":
print("here")
owm = pyowm.OWM(config.weather_api_key)
observation = owm.weather_at_place("Bangalore, IN")
observation_list = owm.weather_around_coords(12.972442, 77.580643)
w = observation.get_weather()
w.get_wind()
w.get_humidity()
w.get_temperature("celsius")
print(w)
print(w.get_wind())
print(w.get_humidity())
print(w.get_temperature("celsius"))
engine.say(w.get_wind())
engine.runAndWait()
engine.say("humidity")
engine.runAndWait()
engine.say(w.get_humidity())
engine.runAndWait()
engine.say("temperature")
engine.runAndWait()
engine.say(w.get_temperature("celsius"))
engine.runAndWait()
if intent == "music" or intent == "restaurant":
engine.say("please wait")
engine.runAndWait()
print(wikipedia.summary(translate))
engine.say(wikipedia.summary(translate))
engine.runAndWait()
if translate in greetings:
random_greeting = random.choice(greetings)
print(random_greeting)
elif translate.lower() == "yes":
change_location = True
print("Location?")
elif change_location is True:
personalized = change_location_query(translate, config.google_api_key)
change_location = False
elif translate in question:
print("I am fine")
elif translate in var1:
reply = random.choice(var2)
print(reply)
elif translate in cmd9:
print(random.choice(repfr9))
elif translate in cmd7:
print(random.choice(colrep))
print("It keeps changing every micro second")
elif translate in cmd8:
print(random.choice(colrep))
print("It keeps changing every micro second")
elif translate in cmd2:
mixer.init()
mixer.music.load("song.wav")
mixer.music.play()
elif translate in var4:
engine.say("I am a bot, silly")
engine.runAndWait()
elif translate in cmd4:
webbrowser.open("http://www.youtube.com")
elif translate in cmd6:
print("see you later")
exit()
elif translate in cmd5:
print("here")
url = (
"http://api.openweathermap.org/data/2.5/weather?" +
"lat={}&lon={}&appid={}&units={}"
).format(
latitude,
longitude,
config.weather_api_key,
config.weather_temperature_format,
)
r = requests.get(url)
x = r.json()
city = x["name"]
windSpeed = x["wind"]["speed"]
skyDescription = x["weather"][0]["description"]
maxTemperature = x["main"]["temp_max"]
minTemperature = x["main"]["temp_min"]
temp = x["main"]["temp"]
humidity = x["main"]["humidity"]
pressure = x["main"]["pressure"]
# use the above variables based on user needs
print(
"Weather in {} is {} "
"with temperature {} celsius"
", humidity in the air is {} "
"and wind blowing at a speed of {}".format(
city, skyDescription, temp, humidity, windSpeed
)
)
engine.say(
"Weather in {} is {} "
"with temperature {} celsius"
", humidity in the air is {} "
"and wind blowing at a speed of {}".format(
city, skyDescription, temp, humidity, windSpeed
)
)
engine.runAndWait()
elif translate in var3 or translate in var5:
current_time = datetime.datetime.now()
if translate in var3:
print(current_time.strftime("The time is %H:%M"))
engine.say(current_time.strftime("The time is %H:%M"))
engine.runAndWait()
elif translate in var5:
print(current_time.strftime("The date is %B %d, %Y"))
engine.say(current_time.strftime("The date is %B %d %Y"))
engine.runAndWait()
elif translate in cmd1:
webbrowser.open("http://www.google.com")
elif translate in cmd3:
jokrep = pyjokes.get_joke()
print(jokrep)
engine.say(jokrep)
engine.runAndWait()
elif ("them" in translate.split(" ") or
"popular" in translate.split(" ")) and stores:
sorted_stores_data = sorted(
stores_data, key=lambda x: x["rating"], reverse=True
)
sorted_stores = [x["name"] for x in sorted_stores_data][:5]
if "order" in translate:
print("These are the stores: ")
for store in sorted_stores:
print(store)
engine.say(sorted_stores)
engine.runAndWait()
if "popular" in translate:
print("Most popular one is: ", sorted_stores[0])
if "go" in translate:
lat = sorted_stores_data[0]["geometry"]["location"]["lat"]
lng = sorted_stores_data[0]["geometry"]["location"]["lng"]
url = "http://maps.google.com/maps?q={},{}".format(lat, lng)
webbrowser.open_new(url)
engine.say(
"Showing you directions to the store {}".format(
sorted_stores[0]
)
)
engine.runAndWait()
elif (
"stores" in translate.split(" ") or
"food" in translate.split(" ") or
"restaurant" in translate
):
stores = []
stores_data = {}
query = filter_sentence(translate)
stores, stores_data = nearby_places(
config.google_api_key,
personalized.city,
query,
personalized.latitude,
personalized.longitude,
)
print("These are the stores: ")
for store in stores:
print(store)
engine.say(stores)
engine.runAndWait()
print("Where do you want to go:")
engine.say("Where do you want to go:")
engine.runAndWait()
else:
engine.say("please wait")
engine.runAndWait()
print(wikipedia.summary(translate))
engine.say(wikipedia.summary(translate))
engine.runAndWait()
userInput3 = input("or else search in google")
webbrowser.open_new("http://www.google.com/search?q=" + userInput3)