-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatbot1.py
120 lines (112 loc) · 4.47 KB
/
chatbot1.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
import random, pickle, os
import os.path
startmes = """Machine Learning Conversational Program by Jake Speiran, 2015. ver 1.0
Hello! In a moment you will begin chatting with Machine Learning Conversational
Program, or MLCP. Everything you say he will learn, and every response you make
he will remember. The goal is that he will someday be able to talk. Type
"#help" to learn more. To quit the program, type "#quit" into the command
prompt.
"""
helpmes = """This is the help message for MLCP.
In order to communicate with the bot, simply type what you want to say into the
input space. When typing please use only lower case characters and no special
characters.
So this:
"You're a real star!"
Becomes this:
"youre a real star"
The reason for this is that otherwise you would have many entries that are
copies of the same word, ie Hey, hey, hey! and Hey all mean the same thing
but would be entered differently.
Sometimes what the bot says can be hard to interpret, but keep trying and
use your imagination.
"""
class bot():
def __init__(self, autosave, deldups, autocount, maxwords, maxresp):
self.autosave = autosave
self.autocount = autocount
self.deldups = deldups
self.maxwords = maxwords
self.maxresp = maxresp
self.known = {}
self.wordcount = 0
self.sescount = 0
os.system("cls")
print(startmes)
if os.path.isfile("known.data"):
self.known = pickle.load(open('known.data', "rb"))
print("Save file loaded!")
else:
print("No save file found.")
print()
for key, value in self.known.items():
self.wordcount += 1
def question(self, x):
self.wordcount += 1
a = "w" + str(self.wordcount)
d = {"name": x, "resp": [x], "uses": 0}
self.known[a] = d
def talk(self):
talking = True
prevres = ""
while talking:
if self.autosave:
self.sescount += 1
if self.sescount >= self.autocount:
self.sescount = 0
pickle.dump(self.known, open('known.data', 'wb'))
print("Saving...")
if self.deldups:
for key, value in self.known.items():
value["resp"] = list(set(value["resp"]))
if len(self.known.keys()) > self.maxwords:
count = 0
for key, value in self.known.items():
count += value["uses"]
for i in range(self.wordcount):
for key, value in self.known.items():
if value["uses"] <= count/self.wordcount:
self.wordcount -= 1
self.known.pop(key, None)
break
for key, value in self.known.items():
if len(value["resp"]) > self.maxresp:
rem = random.choice(value["resp"])
value["resp"].remove(rem)
res = ""
a = input("You: ")
if "#" in a:
if "quit" in a:
pickle.dump(self.known, open('known.data', 'wb'))
print("Saving...")
exit()
if "help" in a:
print(helpmes)
a = ""
data = prevres.split(" ")
inp = a.split(" ")
for x in data:
for key, value in self.known.items():
if x == value["name"]:
value["resp"].extend(inp)
for x in inp:
if a == "":
break
names = []
for key, value in self.known.items():
names.append(value["name"])
if x not in names:
self.question(x)
else:
for key, value in self.known.items():
if x == value["name"]:
xyz = random.randrange(0,4)
for i in range(xyz):
res = res + " {0}".format(random.choice(value["resp"]))
value["uses"] += 1
if res == "":
res = " ..."
print("Bot:{0}".format(res))
prevres = res
sauce = bot(True, True, 25, 1000, 15)
sauce.talk()