ChatterBot Tutorial

หัวข้อ

  1. Read only mode
  2. Setting the storage adapter
  3. Input and output adapters
  4. Specifying logic adapters
  5. 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

2. Setting the storage adapter

ปกติ ChatterBot จะใช้งานอะแดปเตอร์ SQLStorageAdapter เป็นค่า default และฐานข้อมูลเป็น SQLite
แต่เราก็สามารถกำหนดค่าต่างๆได้เอง เช่นกำหนดชื่อไฟล์ฐานข้อมูล

เมื่อรันก็จะได้ไฟล์ฐานข้อมูล 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.

โปรแกรมจะให้เราอินพุทด้วยการพิมพ์เข้าไป และโปรแกรมจะพิมพ์ตอบเป็นเอาท์พุทออกมาเลย เลยลบคำสั่ง 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 เราทำงาน ว่าจะให้ทำงานอยู่ในโดเมนไหนได้บ้าง

สั่งรันแล้ว 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.

ปล. ไหนๆส่วนเทรนก็ไม่ได้ใช้ ก็เลยเอาโค๊ดส่วนเทรนออก โค๊ดเลยสั้นลง

ผลการรัน

[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