1

我对编程比较陌生,我最近开始从事的一个项目是 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。

4

1 回答 1

1

甚至在你开始决定什么对象和如何做之前,有明确的目标在编程中就很重要。不幸的是,从我上面读到的内容来看,这并不是很清楚。

所以首先忘记程序的方式。忘记对象、代码和它们的作用。

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

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

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

现在从它们如何交互的角度考虑这些元素的关系。IE

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

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

说了这么多,我想说你最大的问题是你承受的比你能咀嚼的多。首先,计算机识别单词并将它们放入主题是非常复杂的,并且涉及语言学和/或统计学。作为一名新程序员,我倾向于避开这些领域,因为它们只会让你失望并在此过程中扼杀你的动力。从小处着手……然后大处。试着搞乱 GUI 编程,然后做一个简单的计算器和东西......

于 2011-07-09T14:50:18.150 回答