-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathchat(offline).py
More file actions
209 lines (190 loc) · 5.98 KB
/
chat(offline).py
File metadata and controls
209 lines (190 loc) · 5.98 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
######################
# written by facitoo #
######################
#*********************************************************************************************************************#
##########################################
#### offline and need manual training ####
##########################################
import operator
import wikipedia
from tkinter import *
import random
import shelve
window = Tk()
#######--------------------------------------------------------------------------------------------------------------------------------------
MEMORY=['you said nothing!']
CHANGE=['change your name','change name','name change','change nam','change the name','i want to change your name','i dont like your name']
EXITGREETING=['GoodBye!','Sayonara','Will miss you!','Nice talking!','See you later Aligator!','Take care']
QUESTIONS=['what','is','can','how','howcome','will','would','can']
GREETING=['hi','hello','sup','howdy','hie','hlo','hey']
EXIT=['bye','gtg','stop','ttyl','exit']
AGREE=['yes','YES','Y','y','ya','sure','ok','gg']
RELATEDNAME=['your name','what is your name','whats your name']
OOPS=[' sorry i dont got you','i think thats not in my memory','try something else! i am not smart as you','thats not in my database']
#######-------------------------------------------------------------------------------------------------------------------------------------
####-------------------------------------------------------
name=shelve.open("name.txt",flag='c',writeback=True)
try:
NEWNAME=name['nm']
except:
name['nm']='chatter'
NEWNAME=name['nm']
name.close()
####-------------------------------------------------------
###---------------------------------------------
window.title(NEWNAME)
NAME=NEWNAME
result=''
eventtype=int(0)
searchflag=int(0)
found=int(0)
lmode=int(0)
OOPSCOUNTER=int(0)
says=''
key='chatter'
###----------------------------------------------
###----------------------------------------------
messages = Text(window)
messages.pack()
input_user = StringVar()
input_field = Entry(window, text=input_user)
input_field.pack(side=BOTTOM, fill=X)
frame = Frame(window)
messages.insert(INSERT, '%s\n' % (NAME+': hey! i am '+NAME))
###----------------------------------------------
def key_input():
global key,eventtype
key=input_field.get()
new_name=str('you: '+key)
messages.insert(INSERT, '%s\n' % new_name)
input_user.set('')
eventtype=0
return
def changename():
global key,eventtype,NAME
eventtype=1
name=shelve.open("name.txt",flag='w',writeback=True)
input_field.bind("<Return>", Enter_pressed)
if key in EXIT:
messages.insert(INSERT, '%s\n' % str(NAME+': '+random.choice(EXITGREETING)))
exit()
name['nm']=str(key)
name['what is your name']=str(key)
name.close()
NAME=key
return str(NAME+': ok! so my new name is '+NAME)
def search():
global key,eventtype,searchflag
eventtype=1
messages.insert(INSERT, '%s\n' % str(NAME+': what you want me to search?'))
input_field.bind("<Return>", Enter_pressed)
if key in EXIT:
messages.insert(INSERT, '%s\n' % str(NAME+': '+random.choice(EXITGREETING)))
exit()
try:
global result
result=wikipedia.summary(key,sentences=3)
except:
messages.insert(INSERT, '%s\n' % str(NAME+': be more presise!!'))
search()
if searchflag==0 :
messages.insert(INSERT, '%s\n' % str('***************** heres what wiki says *************\n'))
messages.insert(INSERT, '%s\n' % result)
searchflag=1
return
def learn(word):
global key,eventtype
eventtype=1
messages.insert(INSERT, '%s\n' % str(NAME+': would you like me to learn the response for future reference? '))
input_field.bind("<Return>", Enter_pressed)
if key in EXIT:
messages.insert(INSERT, '%s\n' % str(NAME+': '+random.choice(EXITGREETING)))
exit()
if key in AGREE:
messages.insert(INSERT, '%s\n' % str(NAME+': so! what may i say for the next time?'))
reply=input('you: ')
if reply in EXIT:
messages.insert(INSERT, '%s\n' % str(NAME+': '+random.choice(EXITGREETING)))
exit()
LEARNEDGREETING = shelve.open("name.txt",flag='w',writeback=True)
LEARNEDGREETING[word] = str(reply)
LEARNEDGREETING.close()
return str(NAME + ': gotcha! will remember next time')
else:
return str(NAME+': Ok! i just forgot what you said!')
def filter(words):
symbols = "!@#$%"
for letter in words:
if letter in symbols:
words=letter.replace(letter,'')
return words
def check(word):
global OOPSCOUNTER,lmode
s=shelve.open("name.txt")
try:
return str(NAME+': '+s[word])
global found
found=1
except:
if word == 'lrmodon':
lmode=1
return str(NAME+': *learning mode on*')
OOPSCOUNTER=0
elif word == 'lrmodof':
lmode=0
return str(NAME+': *learning mode off*')
OOPSCOUNTER=0
else:
OOPSCOUNTER+=1
if OOPSCOUNTER > 3 and OOPSCOUNTER < 5:
return str(NAME+': stop messing with me!')
elif OOPSCOUNTER>=5:
return str(NAME+': this is not funny anymore!')
OOPSCOUNTER=0
else:
return str(NAME+': '+random.choice(OOPS))
finally:
s.close()
return
def decision(says):
global lmode,found
says=str(says).lower()
says=filter(says)
if says in GREETING:
return str(NAME +': '+ random.choice(GREETING) +' tell me soemthing to do! ')
elif says in EXIT:
return str(NAME+': '+random.choice(EXITGREETING))
exit()
elif says == 'search':
search()
elif says in CHANGE:
messages.insert(INSERT, '%s\n' % str(NAME+': I like my name a lot! but if you want to change you can change it!'))
messages.insert(INSERT, '%s\n' % str(NAME+': what would you like to change my name to'))
return str(changename())
else:
check(says)
if found==0 and lmode == 1:
if says != 'lrmodon':
return str(learn(says))
found=0
return
def Enter_pressed(event):
global eventtype
if eventtype == 1:
key_input()
else:
driver()
def driver():
global says
says=input_field.get()
print('you: '+says)
messages.insert(INSERT, '%s\n' % says)
response=decision(says)
says=str('you: '+says)
messages.insert(INSERT, '%s\n' % response)
messages.see("end")
input_user.set('')
return "break"
input_field.bind("<Return>", Enter_pressed)
frame.pack()
window.mainloop()