สร้าง Chat bots อย่างง่ายๆ ด้วย Python

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: สร้าง Chat bots อย่างง่ายๆ ด้วย Python

สร้าง Chat bots อย่างง่ายๆ ด้วย Python

โดย thelordbank » 02/08/2017 6:03 pm

เราสามารถสร้าง Chatbot(แช็ด - บ่อด) โดยใช้ไลบรารี่ตัวหนึ่งในภาษา Python ชื่อ Chatterbot(แช็ด - เตอ - บ่อด) ซึ่งมันเป็น machine learning(แมด - ชีน - เลิน - นิ่ง) ที่สำหรับเอาไว้สร้าง chat bots โดยเฉพาะ

วิธีใช้งาน
1. ติดตั้ง Anaconda
2. แล้วติดตั้ง Chatterbot ด้วยคำสั่ง

โค้ด: เลือกทั้งหมด

pip install chatterbot
EX.1
เมื่อป้อนบทสนทนาอย่างง่าย

โค้ด: เลือกทั้งหมด

# -*- 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

ข้างบน