首页 文章

Chatbot会话对象,您的方法?

提问于
浏览
1

我对编程比较陌生,我最近开始研究的一个项目是python中的一个聊天机器人,我经常使用irc Channels . 我的目标之一是让机器人能够非常基本地跟踪它与用户的对话 . 我目前正在使用会话对象 . 当用户对机器人进行寻址时,它会创建一个新的convo对象,并在该对象中存储对话日志,当前主题等 . 当用户说话时,如果他们的消息与对话的主题匹配,则根据他们所说的内容和新主题选择响应 .

例如,如果机器人加入,并且用户说:“你好,机器人 . ”将创建对话并将主题设置为“问候” . 机器人会问回来,如果用户问:“怎么了?”,机器人会将主题改为“currentevents”,回复“不多”或类似 . 主题有相关的主题,如果机器人注意到一个未标记为相关的主题的突然变化(问题是例外),它会稍微混淆并且吃了一惊 .

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

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

  • 1

    在制定决定什么对象和方法之前,有明确的目标在编程中很重要 . 不幸的是,从我上面所读到的,这并不是很清楚 .

    所以首先忘记程序的 how . 忘记对象,代码和他们做的事情 .

    现在想象一下,别人会为你写这个节目 . 一个如此优秀的程序员,他们不需要你告诉他们如何编码 . 这里有一些他们可能会问你的问题 .

    • 一个句子中你的程序的目的是什么?

    • 尽可能简单地向我解释主要条款,IRC,Conversation .

    • 一定能做什么?简短的要点 .

    • 逐步说明如何使用该程序,例如:

    • 我输入

    • 然后说

    • 取决于天气......它给了我一份清单...

    完成这个后,忘记你的convo对象或其他什么,并考虑1,2和4 . 在笔和纸上考虑你的问题的主要元素我,eConversations.Don't只是创建对象...你'我会找到他们的 .

    现在考虑这些元素如何相互作用的关系 . 即

    “Bot向主题添加消息,用户向主题添加消息,主题消息被发送到日志 . ”

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

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

相关问题