-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
113 lines (81 loc) · 3.04 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
import nltk
from nltk.stem.lancaster import LancasterStemmer
stemmer = LancasterStemmer()
import playsound
import speech_recognition
from gtts import gTTS
import dateutil.parser
import numpy
import random
import google.getEvents as gc
import glob
def speak(text):
tts = gTTS(text=text, lang=glob.lang)
filename = "voice.mp3"
tts.save(filename)
playsound.playsound(filename)
def get_audio():
said = ""
while said == "":
recognizer = speech_recognition.Recognizer()
with speech_recognition.Microphone() as source:
audio = recognizer.listen(source, )
print("It's your turn")
try:
said = recognizer.recognize_google(audio, language=glob.lang)
print("You: " + str(said))
except Exception as e:
print("Please talk to me!")
return said
def bag_of_words(s, words):
bag = [0 for _ in range(len(words))]
s_words = nltk.word_tokenize(s)
s_words = [stemmer.stem(word.lower()) for word in s_words]
for se in s_words:
for i, w in enumerate(words):
if w == se:
bag[i] = 1
return numpy.array(bag)
def intentsChat(inp):
results = glob.model.predict([bag_of_words(inp, glob.words)])[0]
results_index = numpy.argmax(results)
tag = glob.labels[results_index]
if results[results_index] > 0.7:
if tag == 'calendar':
events = gc.get_events(3, glob.service)
if not events:
speak('No upcoming events found.')
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
startFormatted = dateutil.parser.parse(start).strftime('%c')
print(startFormatted + " " + event['summary'])
speak(startFormatted + " " + event['summary'])
glob.new_context = "intents"
else:
for intent in glob.data['intents']:
if intent['tag'] == tag:
responses = intent['responses']
glob.new_context = intent['context_set']
speak(random.choice(responses))
else:
speak("What the fuck are you saying? To me?")
glob.new_context = 'intents'
def changeLanguageChat(inp):
results = glob.model.predict([bag_of_words(inp, glob.words)])[0]
results_index = numpy.argmax(results)
tag = glob.labels[results_index]
if results[results_index] > 0.7:
for intent in glob.data['intents']:
if intent['tag'] == tag:
responses = intent['responses']
glob.new_context = intent['context_set']
glob.lang = tag
speak(random.choice(responses))
else:
speak("I don't know that language, so we will keep talking in the same we are talking now, okay?")
glob.new_context = "intents"
def chat(inp):
if glob.context == "intents":
return intentsChat(inp)
elif glob.context == "changeLanguage":
return changeLanguageChat(inp)