私はプログラミングが比較的新しいです。私が作業を始めた最近のプロジェクトは、頻繁に使用するircチャネル用のPythonのチャットボットです。 私の目標の1つは、ボットに、基本的にユーザーとの会話を追跡できるようにすることです。 私は現在会話オブジェクトを使用しています。ユーザーがボットのアドレスを指定すると、新しいconvoオブジェクトが作成され、そのオブジェクトに会話のログ、現在のトピックなどが保存されます。 ユーザーが話すとき、そのメッセージが会話のトピックと一致する場合、そのトピックと新しいトピックに基づいて応答を選択します。Chatbot会話オブジェクト、あなたのアプローチは?
たとえば、ボットが参加し、ユーザーが「こんにちは、ボット」と言うと、会話が作成され、トピックが「挨拶」に設定されます。 ボットはハローを返信し、ユーザーが "What's up?"と尋ねると、ボットはそのトピックを「currentevents」に変更し、「あまりない」またはそれに類するもので返信します。 トピックに関連するトピックがあり、ボットが関連するとマークされていないトピック(質問は例外)に突然変更があった場合、少し混乱して動きが悪くなります。
私の質問は、私の方法は少し過度に複雑で不要な感じです。私はオブジェクトが使用するのに最適なものではないと確信しています。会話とそのトピックを把握するためのもう1つのアプローチは何でしょうか?それは良い方法でも悪いことでも、私はちょうどアイデアと少しのブレーンストーミングを探しています。
これは適切な場所ではないと言われる前に、私はプログラマーの質問をしました.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です。
「オブジェクトは、使い方が最良ではありません。しかし、いくつかのコードを投稿して、どの部分がよりシンプルにできるかを提案することができます。 –
@larsmans:コードを最適化することは、私の元の質問の一部ではありませんが、私はコードを追加しました:)そして、申し訳ありませんが、私はしばらくコンピュータから離れていなければなりませんでした。 – xBlink