-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
185 lines (145 loc) · 4.9 KB
/
main.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import chatterbot
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import speech_recognition as sr
from time import ctime
import time
import os
from gtts import gTTS
import wikipedia
import wolframalpha
from yandex_speech import TTS
import os
class main:
"this class contains all the functions required for the "
APPID="LTQUWQ-YLHV4696XJ";
client = wolframalpha.Client(APPID);
# initializing the chatbots
lamp = ChatBot("Lamp")
lamp.set_trainer(ListTrainer)
lamp = ChatBot(
'lamp',
trainer='chatterbot.trainers.ListTrainer',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
logic_adapters=[
"chatterbot.logic.BestMatch",
"chatterbot.logic.MathematicalEvaluation",
{
'import_path': 'chatterbot.logic.LowConfidenceAdapter',
'threshold': 0.40,
'default_response': 'I am sorry, but I do not understand.'
}
],
#filters=[
#'chatterbot.filters.RepetitiveResponseFilter'
# ],
database='./databaselampit.sqlite3'
)
#VOICE
def speak(audioString):
tts = TTS("ermil", "mp3", "25d87483-720a-46ea-82bd-7f89d4c95bbd",lang='en-US',emotion="good")
tts.generate(audioString+" ")
tts.save()
os.system("mpg321 --stereo speech.mp3 ")
#trainer for the list available in the folder input the filename
def train_file(filename):
#speak("I am learinig and launching myself soon");
path=filename+"";
file = open(path, 'r')
n_lines=0;
for line in file:
n_lines+=1
file.close();
print(n_lines)
count=0
file = open(path, 'r')
for i in range(0,n_lines):
question=file.readline().replace("- ","").strip();
print(question);
answer=file.readline().replace("- ","").strip();
print(answer);
main.lamp.train([question,answer]);
i+=1;
# funtion to know if the bot is calibarted or not
#returns a bool value true means need to calibrate else note required to calibrate ini.txt is 0 if calibrated
def isCalibrated():
file=open("ini.txt",'r')
s=int(file.read());
file.close();
if (s==0):
return False;
else:
return True;
print(isCalibrated());
# function for self calibration
def self_calibrate():
path_Q="calibrate.txt"
Q= open(path_Q, 'r')
A= open("self_calibrate.txt",'w')
n_lines=0;
for line in Q:
n_lines+=1
Q.close();
print(n_lines)
count=0
Q = open(path_Q, 'r')
for i in range(0,n_lines):
question=Q.readline().replace("- ","").strip();
print("Q :"+question);
answer=input("answer")
A.write(question+"\n");
A.write(answer+"\n");
lamp.train([question,answer]);
i+=1;
### add the code to change the initial state to zero once the file had been calibrated
# In[6]:
if(isCalibrated()):
print("calibarting");
#self_Calibrated();
train_file("self_calibrate.txt");
else:
print("dont calibrate");
# In[28]:
# function to call for wikipedia and wolfromalpha
# In[7]:
#fucntion to call wikipedia and wolfrom
def online(ask):
#try and except
try:
#wolf
import wolframalpha
APPID="LTQUWQ-YLHV4696XJ";
client = wolframalpha.Client(APPID);
res=client.query(ask);
answer=next(res.results).text;
#print(answer);
except:
try:
wikipedia.set_lang("en");
answer=wikipedia.summary(ask,sentences =2);
except:
answer="error";
return answer;
#voice recognition functions and its setting up
def recordAudio():
# Record Audio
r = sr.Recognizer()
r.dynamic_energy_threshold = True
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration = 1);
print("Say something!")
audio = r.listen(source)
# Speech recognition using Google Speech Recognition
data = ""
try:
# Uses the default API key
# To use another API key: `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
data = r.recognize_google(audio)
print("You said: " + data)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
#data=r.recognize_sphinx(audio);
#print("Sphinx thinks you said " + data)
return data