我对编程比较陌生,最近我开始做的一个项目是一个python聊天机器人,用于我经常使用的irc频道。 我的目标之一是让机器人能够基本上跟踪它与用户的对话。 我现在正在使用对话对象。当用户为bot寻址时,它会创建一个新的conversa对象,并在该对象中存储会话、当前主题等的日志。 当用户说话时,如果他们的信息与对话的主题相匹配,它会根据他们所说的和新的主题选择一个响应。在
例如,如果bot加入,并且用户说:“你好,bot”。那么将创建对话并将主题设置为“greeting”。 如果用户问:“怎么了?”,bot会将主题更改为“currentevents”,并用“not much”或类似的方式回复。 topic有相关的topic,如果bot注意到一个没有标记为related的主题(questions是例外)突然发生了变化,它会表现得有些困惑和吃惊。在
我的问题是:我觉得我的方法有点过于复杂和不必要。我肯定物体不是最好的东西。另一种跟踪谈话及其主题的方法是什么?不管是好是坏,我只是想找些点子和一些头脑风暴。在
在你说这地方不对之前,我试着问问程序员.stackexchange.com,但我没有收到相关回复,只是有人误解了我。我希望我能在一个更活跃的网站上得到更多的反馈。在某种程度上这就是代码帮助:)
下面是我当前方法的代码。仍然有一些bug,我确信代码的效率还远远不够。欢迎任何关于代码的提示或帮助。在
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\u操作。在
即使在你决定什么对象和如何做之前,有明确的目标在编程中也是很重要的。不幸的是,从我上面读到的,这并不是很清楚。在
所以,首先忘记程序的如何。忘记对象、代码和它们的作用。在
现在想象有人会为你写程序。一个如此优秀的程序员,他们不需要你告诉他们如何编码。下面是一些他们可能会问你的问题。在
这样做了,然后忘记你的护航目标或其他什么,并考虑1,2和4。用笔和纸思考你问题的主要因素,例如对话。唐不只是创建对象。。。你会找到他们的。在
现在想想这些元素之间的关系,看它们是如何相互作用的。i、 e
Bot向主题添加消息,用户向主题添加消息,来自主题的消息发送到日志
这将帮助您找到对象是什么,它们必须做什么,以及需要存储哪些信息。在
说了这么多,我想说你最大的问题是你承担的太多了。首先,对于计算机来说,识别单词并将它们放入主题是相当复杂的,涉及语言学和/或统计学。作为一个新的程序员,我倾向于避免这些领域,因为它们只会让你失望,并在这个过程中扼杀你的动力。从小处开始。。。然后去做大事。试着搞乱图形用户界面编程,然后做一个简单的计算器和东西。。。在
相关问题 更多 >
编程相关推荐