原文地址:http://www.bugingcode.com/blog/ChatterBot_Dialogue_process.html
創建機器人
部署機器人的各種屬性,根據前面的章節里聊天機器人的各種屬性,對聊天機器人進行相應的配置,創建一個符合自己的機器人。
bot = ChatBot(
'Default Response Example Bot',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
logic_adapters=[
{
'import_path': 'chatterbot.logic.BestMatch'
},
{
'import_path': 'chatterbot.logic.LowConfidenceAdapter',
'threshold': 0.65,
'default_response': 'I am sorry, but I do not understand.'
}
],
trainer='chatterbot.trainers.ListTrainer'
)
logic_adapters
logic_adapters:用來設置所選擇的算法,這里選擇的是chatterbot.logic.BestMatch,也就是最匹配方式,從訓練的對話中找到最相識的語句,根據對話,提供回答。
trainer
trainer:選擇的是chatterbot.trainers.ListTrainer
在trainer中,決定選擇哪種構造方式來創建上下文的關系。
def train(self, conversation):
"""
Train the chat bot based on the provided list of
statements that represents a single conversation.
"""
previous_statement_text = None
for conversation_count, text in enumerate(conversation):
print_progress_bar("List Trainer", conversation_count + 1, len(conversation))
statement = self.get_or_create(text)
if previous_statement_text:
statement.add_response(
Response(previous_statement_text)
)
previous_statement_text = statement.text
self.storage.update(statement)
在ListTrainer中,用上下句來構建一個statement ,statement相當於存儲了一個上下對話的關系,在查找的時候,先找到最合適的上文,下文就是答案了。這就是一個訓練的過程,訓練的這一過程,主要是在構建statement,並把statement放到storage中。
storage_adapter
storage_adapter有幾種可選的方案chatterbot.storage.SQLStorageAdapter,MongoDatabaseAdapter,存儲之前訓練的statement,把statement存儲在數據庫中,默認的數據庫選擇的是本地的sqlite3。
訓練機器人
把語料准備好,就聊天機器人進行訓練,語料的來源比較重要,像之前的小黃鴨語料的來源,主要是來源於眾包,用戶會交小黃鴨怎么去回答問題,語料是重要的一種選擇,一個語料的質量決定了聊天機器人的可玩性。
訓練的過程,就是一個建立statement並存儲的過程,代碼在ListTrainer中都有詳細的體現。
bot.train([
'How can I help you?',
'I want to create a chat bot',
'Have you read the documentation?',
'No, I have not',
'This should help get you started: http://chatterbot.rtfd.org/en/latest/quickstart.html'
])
產生答案
聊天機器人主要的過程是產生答案的過程,而答案的選擇最關鍵的就是算法的實現,之前有介紹過,可玩性比較高的聊天機器人必須擁有不同的算法,對不同的聊天內容給出不一樣的答案,根據輸入選擇最合適的算法,產生最好的答案。在機器人對話中,最常見的問題是一些生活的問題,比如,天氣,時間,笑話等,根據問題,選擇最匹配的算法,給出精彩的答案。
response = bot.get_response('How do I make an omelette?')
get_response的過程
采用的是ChatBot的方法,一開始先得到輸入,並對數據進行過濾,在根據輸入數據選擇算法,得出答案。
def get_response(self, input_item, session_id=None):
"""
Return the bot's response based on the input.
:param input_item: An input value.
:returns: A response to the input.
:rtype: Statement
"""
if not session_id:
session_id = str(self.default_session.uuid)
input_statement = self.input.process_input_statement(input_item)
# Preprocess the input statement
for preprocessor in self.preprocessors:
input_statement = preprocessor(self, input_statement)
statement, response = self.generate_response(input_statement, session_id)
# Learn that the user's input was a valid response to the chat bot's previous output
previous_statement = self.conversation_sessions.get(
session_id
).conversation.get_last_response_statement()
self.learn_response(statement, previous_statement)
self.conversation_sessions.update(session_id, (statement, response, ))
# Process the response output with the output adapter
return self.output.process_response(response, session_id)
算法是如何進行選擇的呢?
在multi_adapter.py 算法選擇中,遍歷了所有我們已經選擇的算法,算法通過 can_process 進行選擇,對輸入生成的statement 進行匹配,並通過confidence來進行評分,而應該還可以進行擴展,通過不同的得分,來選擇算法,最佳匹配。
def process(self, statement):
"""
Returns the output of a selection of logic adapters
for a given input statement.
:param statement: The input statement to be processed.
"""
results = []
result = None
max_confidence = -1
for adapter in self.get_adapters():
if adapter.can_process(statement):
output = adapter.process(statement)
if type(output) == tuple:
warnings.warn(
'{} returned two values when just a Statement object was expected. '
'You should update your logic adapter to return just the Statement object. '
'Make sure that statement.confidence is being set.'.format(adapter.class_name),
DeprecationWarning
)
output = output[1]
results.append((output.confidence, output, ))
self.logger.info(
'{} selected "{}" as a response with a confidence of {}'.format(
adapter.class_name, output.text, output.confidence
)
)
if output.confidence > max_confidence:
result = output
max_confidence = output.confidence
else:
self.logger.info(
'Not processing the statement using {}'.format(adapter.class_name)
)
# If multiple adapters agree on the same statement,
# then that statement is more likely to be the correct response
if len(results) >= 3:
statements = [s[1] for s in results]
count = Counter(statements)
most_common = count.most_common()
if most_common[0][1] > 1:
result = most_common[0][0]
max_confidence = self.get_greatest_confidence(result, results)
result.confidence = max_confidence
return result
ChatterBot的架構和流程基本清楚以后,就是對ChatterBot的擴展,一個好的ChatterBot聊天機器人,還有很多需要完成的地方,比如多輪對話,
我:天氣如何?
機器人:你在位置在那里?
我:廈門
機器人:多雲轉晴,32攝氏度
轉載請標明來之:http://www.bugingcode.com/
更多教程:阿貓學編程