gpt4 book ai didi

python - 聊天机器人对话对象,你的方法是什么?

转载 作者:太空宇宙 更新时间:2023-11-03 19:29:35 25 4
gpt4 key购买 nike

我对编程还比较陌生,最近我开始从事的一个项目是一个用 python 编写的聊天机器人,用于我经常访问的 irc channel 。我的目标之一是让机器人能够基本上跟踪它与用户的对话。我目前正在使用对话对象。当用户向机器人发送请求时,它会创建一个新的 convo 对象,并在该对象中存储对话日志、当前主题等。当用户说话时,如果他们的消息与对话主题匹配,它会根据他们所说的内容和新主题选择响应。

例如,如果机器人加入,并且用户说:“你好,机器人。”将创建对话并将主题设置为“问候”。机器人会回问好,如果用户问:“怎么了?”,机器人会将主题更改为“当前事件”,并回复“不多”或类似内容。主题有相关主题,如果机器人注意到未标记为相关的主题突然发生变化(问题除外),它会表现得有点困惑和吃惊。

我的问题是:我觉得我的方法有点过于复杂和不必要。我确信对象并不是最好的使用方式。跟踪对话及其主题的另一种方法是什么?无论是更好还是更糟,我只是在寻找想法和一点头脑 Storm 。

在你说这不是正确的地方之前,我已经尝试在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。

最佳答案

在编程中,即使在决定什么对象以及如何对象之前,拥有明确的目标也很重要。不幸的是,从我上面读到的内容来看,这并不是很清楚。

所以首先忘记程序的如何。忘记对象、代码以及它们的作用。

现在想象一下其他人将为您编写该程序。一个如此优秀的程序员,他们不需要你告诉他们如何编码。以下是他们可能会问您的一些问题。

  1. 用一句话概括您的程序的目的是什么?
  2. 尽可能简单地向我解释主要术语、IRC、对话。
  3. 它必须能够做什么?简短的要点。
  4. 分步骤解释您将如何使用该程序,例如:
    • 我输入
    • 然后它说
    • 根据天气...它给了我一个列表...

完成此操作后,请忘记您的 convo 对象或其他任何内容,并根据 1、2 和 4 进行思考。在笔和纸上思考问题的主要元素,即对话。不要只是创建对象.. .你会找到它们的。

现在考虑这些元素之间的交互方式。即

“机器人将消息添加到主题,用户将消息添加到主题,来自主题的消息被发送到日志。”

这将帮助您找到对象是什么、它们必须做什么以及它们需要存储什么信息。

话虽如此,我想说你最大的问题是你承担的事情超出了你的承受能力。首先,计算机识别单词并将其放入主题中是相当复杂的,并且涉及语言学和/或统计学。作为一名新程序员,我倾向于避开这些领域,因为它们只会让你失望,并在此过程中扼杀你的动力。从小事做起...然后做大事。尝试搞乱 GUI 编程,然后制作一个简单的计算器之类的东西......

关于python - 聊天机器人对话对象,你的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6635245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com