วิธีใช้งาน
1. ติดตั้ง Anaconda
2. แล้วติดตั้ง Chatterbot ด้วยคำสั่ง
โค้ด: เลือกทั้งหมด
pip install chatterbot
เมื่อป้อนบทสนทนาอย่างง่าย
โค้ด: เลือกทั้งหมด
# -*- coding: utf-8 -*-
from chatterbot import ChatBot
# Create a new chat bot named Charlie
chatbot = ChatBot(
'Charlie',
trainer='chatterbot.trainers.ListTrainer'
)
chatbot.train([
"Hi, can I help you?",
"Sure, I'd to book a flight to Iceland.",
"Your flight has been booked."
])
# Get a response to the input text 'How are you?'
response = chatbot.get_response('I would like to book a flight.')
print(response)
Your flight has been booked.
EX.2
เมื่อป้อนคำถามให้คำนวณทางคณิตศาสตร์ และถามเวลา
โค้ด: เลือกทั้งหมด
# -*- coding: utf-8 -*-
from chatterbot import ChatBot
bot = ChatBot(
"Math & Time Bot",
logic_adapters=[
"chatterbot.logic.MathematicalEvaluation",
"chatterbot.logic.TimeLogicAdapter"
],
input_adapter="chatterbot.input.VariableInputTypeAdapter",
output_adapter="chatterbot.output.OutputAdapter"
)
# Print an example of getting one math based response
response = bot.get_response("What is 4 + 9?")
print(response)
# Print an example of getting one time based response
response = bot.get_response("What time is it?")
print(response)
( 4 + 9 ) = 13
The current time is 10:15 PM