Discuss Scratch

mybearworld
Scratcher
1000+ posts

My semi-learning chatbot


As you can see, it's awesome!
I programmed it in Python.
If you for some reason want to try it out, put this into Python:
import random, re, sys

GREETING = "hello! how are you?"
RBTMSG = " Robot: "
USRMSG = " User: "
CSLMSG = "Console: "

brain = {}
words = lambda x: tuple(map(str.lower, re.findall(r"\b(\w+?)\b", x)))
def addDict(original, other):
for i in other:
if i not in original: original[i] = 0
original[i] += 1
return original

print(f"{RBTMSG}{GREETING}")
botwords = words(GREETING)
while True:
human = input(USRMSG).lower()
if human.startswith("/"):
if human == "/exit":
sys.exit()
elif human == "/brain":
print(f"{CSLMSG}{brain}")
else:
print(f"{CSLMSG}command not found")
else:
humanwords = words(human)
if human not in brain.keys(): brain[human] = {}
brain[human] = addDict(brain[human], botwords)
matches = []
for i in brain:
matchid = 0
for j in humanwords:
if j.lower() in brain[i]:
matchid += brain[i][j]
matches.append([i, matchid])
best = [[None, 0]]
for k in matches:
if best[0][1] < k[1]:
best = [k]
elif best[0][1] == k[1]:
best.append(k)
if best[0][0] == None: best = best[1:]
response = random.choice(best)[0]
print(f"{RBTMSG}{response}")
botwords = words(response)
It learns from what you reply to it.
Share interesting conversations, I guess?

Last edited by mybearworld (April 3, 2022 18:09:13)


Signatures are the only place where assets links still work.
mybearworld
Scratcher
1000+ posts

My semi-learning chatbot

Added support for /brain command for showing the brain. Probably not that helpful for anybody, but for me who likes seeing the brain
and for debugging my attempts to actually make it useful

Last edited by mybearworld (April 3, 2022 18:18:15)


Signatures are the only place where assets links still work.

Powered by DjangoBB