-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAI Bot_VOICE.py
167 lines (138 loc) · 4.58 KB
/
AI Bot_VOICE.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
import re
import os
import sys
import pyttsx
import requests
import mechanize
import speech_recognition
from time import sleep
from chatterbot import ChatBot
net_detect=0 # It sets to 1 if active network connection is found
exit_condition=0
try:
engine=pyttsx.init()
#engine.setProperty('rate', 180)# words per minute
engine.setProperty('voice','HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0')
br=mechanize.Browser()
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False) # Ignore robots.txt
br.set_handle_refresh(False)
#Google demands a user-agent that isn't a robot
br.addheaders=[('User-agent','Firefox')]
AI_Bot = ChatBot("Scofield",
storage_adapter="chatterbot.adapters.storage.MongoDatabaseAdapter",
io_adapters=[
"chatterbot.adapters.io.TerminalAdapter"
],
database="scofield")
def anonmail(name,ques):
net_detect=0
br.open("http://anonymouse.org/anonemail.html")
net_detect=1
br.select_form(nr=0)
br.form['to']='[email protected]' # Enter Your EMail-Id Here
br.form['subject']='AI Bot Upgradation Required'
br.form['text']=str(name+" found the following query unresolved:-\n"+ques)
br.submit()
def whois():
net_detect=0
response=br.open("https://www.google.co.in")
net_detect=1
br.select_form(nr=0)
br.form['q']=ques
br.submit()
src_code=br.response().read()
answer=re.search(r'<span>(.*)',src_code)
answer=answer.group()[:400]
if 'wiki' in answer:
answer=re.search(r'<span>(.*)<a',answer).group(1)
else:
answer=re.search(r'<span>(.*)</span></div></div><div',answer).group(1)
engine.say(answer)
print answer
def whatis():
net_detect=0
response=br.open("https://www.google.co.in")
net_detect=1
br.select_form(nr=0)
br.form['q']=ques
br.submit()
src_code=br.response().read()
spg=1
answer=re.search(r'"_sPg">(.*)',src_code)
if answer==None:
answer=re.search(r'<ol><li>(.*)',src_code)
spg=0
if answer==None:
whois()
return
answer=answer.group()[:400]
if '<b>' in answer:
answer=answer.replace("<b>","")
answer=answer.replace("</b>","")
if spg:
answer=re.search(r'"_sPg">(.*)</div></div><div',answer).group(1)
else:
answer=re.search(r'<ol><li>(.*)</li>',answer).group(1)
engine.say(answer)
print answer
def trained():
answer=AI_Bot.get_response(ques)
engine.say(answer)
print answer
print "Enter your Name:- "
engine.say("Enter your Name")
engine.runAndWait()
name=raw_input()
print "Hello",name
engine.say("Hello"+name)
ques='a'
ques_no=0
print "How can I assist you?"
engine.say("How can I assist you?")
engine.runAndWait()
while True:
#ques=raw_input().strip()#keyboard input
microphone=speech_recognition.Recognizer()
with speech_recognition.Microphone() as source:
audio=microphone.listen(source)
ques=microphone.recognize_google(audio)
print ques
ques=ques.lower()
if ques=='bye':
print "Hoping that you enjoyed having this conversation."
print "Thank You."
exit_condition=1
engine.say("Hoping that you enjoyed having this conversation")
engine.say("Thank You")
engine.runAndWait()
sys.exit(1)
ques_no+=1
if 'who' in ques:
if 'you' in ques:
trained()
else:
whois()
elif 'what' in ques:
if 'name' in ques:
trained()
else:
whatis()
else:
trained()
engine.runAndWait()
except:
if exit_condition==1:
pass
elif net_detect==0:
print "Internet and I aren't talking right now."
print "Try after Sometime. Thank You"
engine.say("Internet and I aren't talking right now.")
engine.say("Try After Sometime. Thank You")
engine.runAndWait()
sleep(1)
else:
trained()
#anonmail(name,ques)#Anonymous mail takes atmost 12 hours to reach.