我对编程比较陌生,我最近开始从事的一个项目是 python 中的聊天机器人,用于我经常使用的 irc 频道。我的目标之一是让机器人能够非常基本地跟踪它与用户的对话。我目前正在使用对话对象。当用户寻址机器人时,它会创建一个新的 convo 对象并将对话日志、当前主题等存储在该对象中。当用户说话时,如果他们的消息与对话的主题相匹配,它会根据他们所说的内容和新的主题来选择一个响应。
例如,如果机器人加入,并且用户说:“你好,机器人。” 将创建对话并将主题设置为“问候”。机器人会回击打招呼,如果用户问:“怎么了?”,机器人会将主题更改为“当前事件”,并回复“不多”或类似内容。主题有相关主题,如果机器人注意到一个未标记为相关的主题突然发生变化(问题是例外),它会表现得有点困惑和吃惊。
我的问题是:我觉得我的方法有点过于复杂和不必要。我敢肯定对象不是最好用的东西。跟踪对话及其主题的另一种方法是什么?无论是更好还是更糟,我只是在寻找想法和头脑风暴。
在你说这不是正确的地方之前,我已经尝试在programmers.stackexchange.com上询问,但我没有收到相关回复,只是有人误解了我。我希望我能在更活跃的网站上获得更多反馈。在某种程度上,这是代码帮助:)
这是我当前方法的代码。仍然存在一些错误,我确信代码远非高效。欢迎任何有关代码的提示或帮助。
def __init__(slef):
self.dicti_topics = {"None":["welcomed", "ask", "badbot", "leave"],
"welcomed":["welcomed", "howare", "badbot", "ask", "leave"],
"howare":["greetfinished", "badbot", "leave"]}
self.dicti_lines = {"hello":"welcomed", "howareyou":"howare", "goaway":"leave", "you'rebad":"badbot", "question":"asked"}
self.dicti_responce = dicti["Arriving dicti_responce"]
def do_actions(self):
if len(noi.recv) > 0:
line = False
##set vars
item = noi.recv.pop(0)
#update and trim lastrecv list
noi.lastrecv.append(item)
if len(noi.lastrecv) > 10: noi.lastrecv = noi.lastrecv[1:10]
args = item.split()
channel, user = args[0], args[1].split("!")[0]
message = " ".join(w for w in args[2:])
print "channel:", channel
print "User:", user
print "Message:", message
if re.match("noi", message):
if not user in noi.convos.keys():
noi.convos[user] = []
if not noi.convos[user]:
noi.convos[user] = Conversation(user)
noi.convos[user].channel = channel
line = "What?"
send(channel, line)
if re.match("hello|yo|hey|ohai|ello|howdy|hi", message) and (noi.jointime - time.time() < 20):
print "hello convo created"
if not user in noi.convos.keys():
noi.convos[user] = []
if not noi.convos[user]:
noi.convos[user] = Conversation(user, "welcomed")
noi.convos[user].channel = channel
#if user has an active convo
if user in noi.convos.keys():
##setvars
line = None
convo = noi.convos[user]
topic = convo.topic
#remove punctuation, "noi", and make lowercase
rmsg = message.lower()
for c in [".", ",", "?", "!", ";"]:
rmsg = rmsg.replace(c, "")
#print rmsg
rlist = rmsg.split("noi")
for rmsg in rlist:
rmsg.strip(" ")
#categorize message
if rmsg in ["hello", "yo", "hey", "ohai", "ello", "howdy", "hi"]: rmsg = "hello"
if rmsg in ["how do you do", "how are you", "sup", "what's up"]: rmsg = "howareyou"
if rmsg in ["gtfo", "go away", "shooo", "please leave", "leave"]: rmsg = "goaway"
if rmsg in ["you're bad", "bad bot", "stfu", "stupid bot"]: rmsg = "you'rebad"
#if rmsg in []: rmsg =
#if rmsg in []: rmsg =
#Question handling
r = r'(when|what|who|where|how) (are|is) (.*)'
m = re.match(r, rmsg)
if m:
rmsg = "question"
responce = "I don't know %s %s %s." % (m.group(1), m.group(3), m.group(2))
#dicti_lines -> {message: new_topic}
#if msg has an entry, get the new associated topic
if rmsg in self.dicti_lines.keys():
new_topic = self.dicti_lines[rmsg]
#dicti_topics
relatedtopics = self.dicti_topics[topic]
#if the topic is related, change topic
if new_topic in relatedtopics:
convo.change_topic(new_topic)
noi.convos[user] = convo
#and respond
if new_topic == "leave": line = random.choice(dicti["Confirm"])
if rmsg == "question": line = responce
else: line = random.choice(self.dicti_responce[new_topic])
#otherwise it's confused
else:
line = "Huh?"
if line:
line = line+", %s." % user
send(channel, line)
这是状态机中状态的 do_action。