-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatBotEnglish.py
80 lines (66 loc) · 2.29 KB
/
chatBotEnglish.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
import datetime
import speech_recognition as sr
from nltk.chat.util import Chat, reflections
from gtts import gTTS
import playsound
import wikipedia
from wiki import *
import os
#from wikiNepali import *
from mysqlDataFetch import *
pairs =[
["(hello|namaskar|namaste|hey|hi|hola)",["hey there","namaste","hi there"]],
["how is the weather in (.*)?",["the weather in %1 is amazing like always"]],
["(.*) help (.*)",["I can help you"]],
]
chat = Chat(pairs, reflections)
def quiting(lang,userName):
speak("Main",lang)
def speak(speakText, lang):
myobj = gTTS(text=speakText, lang=lang, slow=False)
audio_file ="welcome.mp3"
myobj.save(audio_file)
playsound.playsound(audio_file)
os.remove(audio_file)
def record_audio(userName):
a=True
text=''
while a:
if(text=="next"):
a=False
quiting("hi",userName)
break
r = sr.Recognizer()
with sr.Microphone() as source:
print("Ask Me:")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
if(text=="next"):
a=False
quiting("hi",userName)
break
print("You said:- " + text)
if text!="next":
try:
result = chat.respond(text)
#result = chatAnswer(text)
print("Bot: "+result)
speak(result,"hi")
except:
result = chatAnswer(text)
if result is None:
wikiResultSpeak,resultLang = wikiSearch(text)
print("BOT: "+wikiResultSpeak)
speak(wikiResultSpeak, resultLang)
else:
print("Bot: "+result)
speak(result,"hi")
else:
quiting("hi",userName)
a=False
break
except sr.UnknownValueError:
print("BOT:- I didn't get that")
speak("I didn't get that","hi")
return