'Chatbot-specific answers
I'm creating a chatbot that asks questions and answers questions, but for now it doesn't have a specific function of answering specific questions that it asks itself. I think you can solve this in the lists themselves
import string
import re
import random
class eliza:
def __init__(self):
self.keys = list(map(lambda x:re.compile(x[0], re.IGNORECASE),gPats))
self.values = list(map(lambda x:x[1],gPats))
#----------------------------------------------------------------------
# translate: take a string, replace any words found in dict.keys()
# with the corresponding dict.values()
#----------------------------------------------------------------------
def translate(self,str,dict):
words = str.lower().split()
keys = dict.keys();
for i in range(0,len(words)):
if words[i] in keys:
words[i] = dict[words[i]]
return ' '.join(words)
#----------------------------------------------------------------------
# respond: take a string, a set of regexps, and a corresponding
# set of response lists; find a match, and return a randomly
# chosen response from the corresponding list.
#----------------------------------------------------------------------
def respond(self,str):
# find a match among keys
for i in range(0, len(self.keys)):
match = self.keys[i].match(str)
if match:
# found a match ... stuff with corresponding value
# chosen randomly from among the available options
resp = random.choice(self.values[i])
# we've got a response... stuff in reflected text where indicated
pos = resp.find('%')
while pos > -1:
num = int(resp[pos+1:pos+2])
resp = resp[:pos] + \
self.translate(match.group(num),gReflections) + \
resp[pos+2:]
pos = resp.find('%')
# fix munged punctuation at the end
if resp[-2:] == '?.': resp = resp[:-2] + '.'
if resp[-2:] == '??': resp = resp[:-2] + '?'
return resp
#----------------------------------------------------------------------
# gReflections, a translation table used to convert things you say
# into things the computer says back, e.g. "I am" --> "you are"
#----------------------------------------------------------------------
gReflections = {
"am" : "are",
"was" : "were",
"i" : "you",
"i'd" : "you would",
"i've" : "you have",
"i'll" : "you will",
"my" : "your",
"are" : "am",
"you've": "I have",
"you'll": "I will",
"your" : "my",
"yours" : "mine",
"you" : "me",
"me" : "you"
}
#----------------------------------------------------------------------
# gPats, the main response table. Each element of the list is a
# two-element list; the first is a regexp, and the second is a
# list of possible responses, with group-macros labelled as
# %1, %2, etc.
#----------------------------------------------------------------------
gPats = [
[r'I need (.*)',
[ "Why do you need %1?",
"Would it really help you to get %1?",
"Are you sure you need %1?"]],
]
I'd like to make the chatbot give a specific answer if it's only after it asks itself a specific question. For example:
User: hello
Bot: Hello! What is your favorite color?
User: Red.
Bot: Interesting, I also like red.
I thought about doing something like this, but it doesn't work:
[r'Hello (.*)',
[ "Hello! What is your favorite color?",
[r'%1',
["Interesting, I also like %1.]]]]
]
Solution 1:[1]
This might not be an answer your looking for but the usual way to implement such a chatbot is in a "Call-Response"-manner.
That means the user sends a text to the chatbot and the chatbot tries to categorize the sentence (and its Intent) and by categorizing tries to answer with a tag-correlating (and predefined) answer-sentence. This already would suggest the usage of a small neural network combined with usual chatbot libraries for tokenizing and lemmatizing sentences(creating the word-base of all words). I can recommend some tutorials on that matter (for example tec with tims https://www.techwithtim.net/tutorials/ai-chatbot/part-1/).
What you try implies using such a framework plus memorizing some states for later usage plus temporary session-knowledge about what has been talked to the same user-id and then chain answers together for a somewhat seamless talk with detailed and variable information-exchange.
For your 'chatbot ask questions first part', this is harder to do. You would need to build somewhat of a behavior tree which your chatbot follows. This tree would dictate questions to ask if some actions where made by the user like "If last question was 'Whats your name' + Answer of user => next message to user should be 'What is your favorite color'". This is in generall possible but would require quite some overhead to post here in a single run. I hope that helped.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 4lexKidd |
