Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 39 additions & 21 deletions examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,74 @@
import speech_recognition
import subprocess
import platform

import datetime
import random

class VoiceChatBot(ChatBot):

def speak(self, text):
if platform.system() == 'Darwin':
# Use Mac's built-in say command to speak the response
cmd = ['say', str(text)]

subprocess.call(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
subprocess.call(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else:
subprocess.run(
'echo "' + str(text) + '" | festival --tts',
shell=True
)
subprocess.run('echo "' + str(text) + '" | festival --tts', shell=True)

def get_response(self, statement=None, **kwargs):
response = super().get_response(statement, **kwargs)

self.speak(response.text)
return response.text # return the text for logging or command handling


# Initialize bot
bot = VoiceChatBot('Example ChatBot')

trainer = ChatterBotCorpusTrainer(bot)

# Train the chat bot with the entire english corpus
trainer.train('chatterbot.corpus.english')

# Randomized greeting
greetings = ["Hello!", "Hi there!", "Hey! How can I help you?"]
bot.speak(random.choice(greetings))

# Initialize speech recognizer
recognizer = speech_recognition.Recognizer()

# File to log conversation
log_file = "chat_log.txt"

# List of simple jokes
jokes = [
"Why did the scarecrow win an award? Because he was outstanding in his field!",
"I told my computer I needed a break, and it said no problem—it needed one too!",
"Why do bees have sticky hair? Because they use honeycombs!"
]

while True:
try:
with speech_recognition.Microphone() as source:
recognizer.adjust_for_ambient_noise(source)
recognizer.adjust_for_ambient_noise(source, duration=1)
print("Listening...")
audio = recognizer.listen(source)

recognizer_function = getattr(recognizer, 'recognize_google')
result = recognizer.recognize_google(audio)
print(f"You said: {result}")

result = recognizer_function(audio)
# Basic commands
if "time" in result.lower():
response_text = f"The current time is {datetime.datetime.now().strftime('%H:%M')}"
bot.speak(response_text)
elif "joke" in result.lower():
response_text = random.choice(jokes)
bot.speak(response_text)
else:
response_text = bot.get_response(statement=result)

bot.get_response(text=result)
# Log conversation
with open(log_file, "a") as f:
f.write(f"User: {result}\nBot: {response_text}\n")

except speech_recognition.UnknownValueError:
bot.speak('I am sorry, I could not understand that.')
bot.speak("I am sorry, I could not understand that.")
except speech_recognition.RequestError as e:
message = 'My speech recognition service has failed. {0}'
bot.speak(message.format(e))
except (KeyboardInterrupt, EOFError, SystemExit):
# Press ctrl-c or ctrl-d on the keyboard to exit
bot.speak("Goodbye!")
break