Skip to content
108 changes: 42 additions & 66 deletions chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
from voice_conf import *
# from speech_recognition.__main__ import r, audio

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[10].id)
volume = engine.getProperty('volume')
engine.setProperty('volume', 10.0)
rate = engine.getProperty('rate')

engine.setProperty('rate', rate - 25)

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"]
Expand All @@ -21,7 +30,6 @@
'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']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this is removed? what about then user ask for date.

cmd1 = ['open browser', 'open google']
cmd2 = ['play music', 'play songs', 'play a song', 'open music player']
cmd3 = [
Expand All @@ -30,11 +38,7 @@
'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']
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',
Expand All @@ -50,39 +54,22 @@

repfr9 = ['youre welcome', 'glad i could help you']

personalized, longitude, latitude = get_location()
personalized = 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:
now = datetime.datetime.now()
r = sr.Recognizer()
with sr.Microphone() as source:
t = translator.translate('Say something', dest=language_conf[:2])
print(t.text)
engine.say(t.text)
print("Tell me something:")
engine.say('Say something')
engine.runAndWait()
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
Expand All @@ -93,6 +80,7 @@
print("Could not understand audio")
engine.say('I didnt get that. Rerun the code')
engine.runAndWait()
translate_split = translate.split(" ")
if translate in greetings:
random_greeting = random.choice(greetings)
print(random_greeting)
Expand Down Expand Up @@ -129,50 +117,40 @@
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))
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()
elif translate in var3:
print("Current date and time : ")
print(now.strftime("The time is %H:%M"))
engine.say(now.strftime("The time is %H:%M"))
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:
elif ("them" in translate_split or "popular" in translate_split) and stores:
sorted_stores_data = sorted(
stores_data,
key=lambda x: x['rating'],
Expand All @@ -195,9 +173,7 @@
"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:
elif "stores" in translate_split or "food" in translate_split or "restaurant" in translate:
stores = []
stores_data = {}
query = filter_sentence(translate)
Expand Down