Contents
หัวข้อ
- Read only mode
- Setting the storage adapter
- Input and output adapters
- Specifying logic adapters
- Getting a response from your chat bot
1. Read only mode
Your chat bot will learn based on each new input statement it receives. If you want to disable this learning feature after your bot has been trained, you can set read_only=True as a parameter when initializing the bot.
ไม่อยากให้ bot เรียนรู้ละ ปรับให้เป็นโหมด Read only
1 |
chatbot = ChatBot("Jack's BOT", read_only=True) |
2. Setting the storage adapter
ปกติ ChatterBot จะใช้งานอะแดปเตอร์ SQLStorageAdapter เป็นค่า default และฐานข้อมูลเป็น SQLite
แต่เราก็สามารถกำหนดค่าต่างๆได้เอง เช่นกำหนดชื่อไฟล์ฐานข้อมูล
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#!/usr/bin/env python # -*- coding: utf-8 -*- from chatterbot import ChatBot from chatterbot.trainers import ListTrainer #Create a new chat bot chatbot = ChatBot("Jack's BOT", storage_adapter='chatterbot.storage.SQLStorageAdapter', database='./database.sqlite3' ) #Training your ChatBot conversation = [ u"สวัสดี", u"ดีจ้า", u"ทำไรอยู่", u"กินข้าว" ] chatbot.set_trainer(ListTrainer) chatbot.train(conversation) #Get a response response = chatbot.get_response("Good morning!") print(response) |
เมื่อรันก็จะได้ไฟล์ฐานข้อมูล database.sqlite3
3. Input and output adapters
Next, we will add in parameters to specify the input and output terminal adapter. The input terminal adapter simply reads the user’s input from the terminal. The output terminal adapter prints the chat bot’s response.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#!/usr/bin/env python # -*- coding: utf-8 -*- from chatterbot import ChatBot from chatterbot.trainers import ListTrainer #Create a new chat bot chatbot = ChatBot("Jack's BOT", storage_adapter='chatterbot.storage.SQLStorageAdapter', input_adapter='chatterbot.input.TerminalAdapter', output_adapter='chatterbot.output.TerminalAdapter', database='./database.sqlite3' ) #Training your ChatBot conversation = [ u"สวัสดี", u"ดีจ้า", u"ทำไรอยู่", u"กินข้าว" ] chatbot.set_trainer(ListTrainer) chatbot.train(conversation) #Get a response response = chatbot.get_response("Good morning!") |
โปรแกรมจะให้เราอินพุทด้วยการพิมพ์เข้าไป และโปรแกรมจะพิมพ์ตอบเป็นเอาท์พุทออกมาเลย เลยลบคำสั่ง print(response)
ที่บรรทัดสุดท้ายออก
4. Specifying logic adapters
The logic_adapters parameter is a list of logic adapters. In ChatterBot, a logic adapter is a class that takes an input statement and returns a response to that statement.
You can choose to use as many logic adapters as you would like. In this example we will use two logic adapters. The TimeLogicAdapter returns the current time when the input statement asks for it. The MathematicalEvaluation adapter solves math problems that use basic operations.
เหมือนกับเป็นการกำหนดโดเมนที่จะให้ bot เราทำงาน ว่าจะให้ทำงานอยู่ในโดเมนไหนได้บ้าง
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#!/usr/bin/env python # -*- coding: utf-8 -*- from chatterbot import ChatBot #Create a new chat bot chatbot = ChatBot("Jack's BOT", storage_adapter='chatterbot.storage.SQLStorageAdapter', input_adapter='chatterbot.input.TerminalAdapter', output_adapter='chatterbot.output.TerminalAdapter', logic_adapters=[ 'chatterbot.logic.MathematicalEvaluation', 'chatterbot.logic.TimeLogicAdapter' ], database='./database.sqlite3' ) #Get a response response = chatbot.get_response("Good morning!") |
สั่งรันแล้ว bot ก็จะคุยแต่เรื่อง เวลา กับ เลข
[code]
Hi
The current time is 05:30 PM
>>> 5 + 10
15
>>>
[/code]
5. Getting a response from your chat bot
Next, you will want to create a while loop for your chat bot to run in. By breaking out of the loop when specific exceptions are triggered, we can exit the loop and stop the program when a user enters ctrl+c.
ลูปทำงานไปเรื่อยๆ อยากหยุดเมื่อไหร่กด ctrl+c.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#!/usr/bin/env python # -*- coding: utf-8 -*- from chatterbot import ChatBot #Create a new chat bot chatbot = ChatBot("Jack's BOT", storage_adapter='chatterbot.storage.SQLStorageAdapter', input_adapter='chatterbot.input.TerminalAdapter', output_adapter='chatterbot.output.TerminalAdapter', logic_adapters=[ 'chatterbot.logic.MathematicalEvaluation', 'chatterbot.logic.TimeLogicAdapter' ], database='./database.sqlite3' ) #Get a response while True: try: response = chatbot.get_response(None) except(KeyboardInterrupt, EOFError, SystemExit): break |
ปล. ไหนๆส่วนเทรนก็ไม่ได้ใช้ ก็เลยเอาโค๊ดส่วนเทรนออก โค๊ดเลยสั้นลง
ผลการรัน
[code]
Hi
The current time is 05:20 PM
1 + 2
1 + 2 = 3
10 – 4
10 – 4 = 6
1+2
The current time is 05:20 PM
[/code]
ถ้า bot ไม่เข้าใจอินพุทก็จะตอบเป็นเวลา เช่น 1+2
พิมพ์โดยไม่มีช่องว่าง bot ก็เลยไม่รู้จัก
Link