Discuss Scratch
- Discussion Forums
- » Things I'm Making and Creating
- » My semi-learning chatbot
- mybearworld
-
1000+ posts
My semi-learning chatbot
![](https://assets.scratch.mit.edu/get_image/.%2E/a2ccea02f292738279650595c7c4e72b.png)
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)
Share interesting conversations, I guess?
![](http://cdn.scratch.mit.edu/scratchr2/static/__657b031c7c65caa7b34c40010ebe0dc1__/djangobb_forum/img/smilies/tongue.png)
Last edited by mybearworld (April 3, 2022 18:09:13)
Signatures are the only place where assets links still work.
- mybearworld
-
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 ![](//cdn.scratch.mit.edu/scratchr2/static/__657b031c7c65caa7b34c40010ebe0dc1__/djangobb_forum/img/smilies/tongue.png)
and for debugging my attempts to actually make it useful
![](http://cdn.scratch.mit.edu/scratchr2/static/__657b031c7c65caa7b34c40010ebe0dc1__/djangobb_forum/img/smilies/tongue.png)
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.
- Discussion Forums
- » Things I'm Making and Creating
-
» My semi-learning chatbot