聊天机器人会话对象,你的方法?

我正在用 Python 为我经常访问的 IRC 频道开发一个聊天机器人。我的目标之一是让机器人能够非常基本地跟踪它与用户的对话。

目前我正在使用会话对象。当用户与机器人对话时,它会创建一个新的会话对象,并将对话的日志、当前主题等存储在该对象中。当用户发言时,如果他们的消息与会话的主题匹配,它会根据他们所说的内容和新主题选择一个响应。

例如,如果机器人加入,并且用户说:“你好,机器人。”,将会创建会话,并将主题设置为“greeting”。机器人会回复“你好”,如果用户问:“有什么新鲜事?”,机器人会将主题更改为“currentevents”,并回复“没什么”或类似的内容。主题之间有相关联的主题,如果机器人注意到突然更改为未标记为相关的主题(问题是例外),它会表现得有些困惑和吃惊。

我感觉我的方法有点过于复杂和不必要。我确信对象不是最好的使用方式。还有什么其他方法可以跟踪对话及其主题吗?无论是更好还是更坏的方法,我只是在寻找想法和一些头脑风暴。

这是我当前方法的代码。仍然有一些错误,而且我确信代码远非高效。欢迎提供任何关于代码的提示或帮助。

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. 用步骤解释您将如何使用该程序,例如:
    • 我输入
    • 然后它说
    • 取决于天气……它给了我这个列表……

完成此操作后,忘记您的会话对象或其他任何东西,并从 1、2 和 4 的角度进行思考。在纸上思考您的问题的主要元素,即会话。不要仅仅创建对象……你会找到它们的。

现在考虑这些元素之间如何交互的关系。即:

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

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

说了这么多,我想说你最大的问题是你承担了超出你能力范围的事情。首先,对于计算机来说,识别单词并将它们放入主题中是相当复杂的,并且涉及语言学和/或统计学。作为一名新的程序员,我倾向于避免这些领域,因为它们只会让你失望,并在此过程中扼杀你的动力。从小处开始……然后做大。尝试进行 GUI 编程,然后制作一个简单的计算器等等……

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注