Contents
หัวข้อ
- ติดตั้ง ChatterBot จาก PyPi
- ตรวจสอบเวอร์ชันที่ติดตั้ง
- การอัพเกรดให้เป็นเวอร์ชันล่าสุด
- ทดลองสร้าง chat bot
- สร้าง chat bot รองรับภาษาไทย
1. ติดตั้ง ChatterBot จาก PyPi
[code]
pip install chatterbot
[/code]
2. ตรวจสอบเวอร์ชันที่ติดตั้ง
[code]
> python -m chatterbot –version
0.8.4
[/code]
3. การอัพเกรดให้เป็นเวอร์ชันล่าสุด
[code]
> pip install chatterbot –upgrade
[/code]
4. ทดลองสร้าง chat bot
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") #Training your ChatBot conversation = [ "Hello", "Hi there!", "How are you doing?", "I'm doing great.", "That is good to hear", "Thank you.", "You're welcome." ] chatbot.set_trainer(ListTrainer) chatbot.train(conversation) #Get a response response = chatbot.get_response("Good morning!") print(response) |
พอสั่งรันโปรแกรมก็จะสร้างไฟล์ฐานข้อมูลชื่อ db.sqlite3 ขึ้นมา
แต่สั่งรันแล้วตอบแค่คำตอบเดิมๆ
[code]
================= RESTART: C:/Users/phais/chatterbot/test.py =================
List Trainer: [### ] 14%
List Trainer: [###### ] 29%
List Trainer: [######### ] 43%
List Trainer: [########### ] 57%
List Trainer: [############## ] 71%
List Trainer: [################# ] 86%
List Trainer: [####################] 100%
I’m doing great.
>>>
================= RESTART: C:/Users/phais/chatterbot/test.py =================
List Trainer: [### ] 14%
List Trainer: [###### ] 29%
List Trainer: [######### ] 43%
List Trainer: [########### ] 57%
List Trainer: [############## ] 71%
List Trainer: [################# ] 86%
List Trainer: [####################] 100%
I’m doing great.
>>>
[/code]
ลองเขียนอีกแบบ แต่ได้ผลเหมือนกัน
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", trainer='chatterbot.trainers.ListTrainer' ) #Training your ChatBot chatbot.train([ "Hello", "Hi there!", "How are you doing?", "I'm doing great.", "That is good to hear", "Thank you.", "You're welcome." ]) #Get a response response = chatbot.get_response("Good morning!") print(response) |
5. สร้าง chat bot รองรับภาษาไทย
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#!/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") #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) |
สั่งรันแล้วตอบคำถามเปลี่ยนไปมาได้
[code]
================= RESTART: C:/Users/phais/chatterbot/test.py =================
List Trainer: [##### ] 25%
List Trainer: [########## ] 50%
List Trainer: [############### ] 75%
List Trainer: [####################] 100%
ดีจ้า
>>>
================= RESTART: C:/Users/phais/chatterbot/test.py =================
List Trainer: [##### ] 25%
List Trainer: [########## ] 50%
List Trainer: [############### ] 75%
List Trainer: [####################] 100%
สวัสดี
>>>
================= RESTART: C:/Users/phais/chatterbot/test.py =================
List Trainer: [##### ] 25%
List Trainer: [########## ] 50%
List Trainer: [############### ] 75%
List Trainer: [####################] 100%
ดีจ้า
>>>
[/code]
ปล. ถ้ารันแล้วคำตอบยังเป็นภาษาอังกฤษให้ลบไฟล์ db.sqlite3 ทิ้งแล้วรันใหม่