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

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderator: mindphp, ผู้ดูแลกระดาน

thelordbank
PHP Full Member
PHP Full Member
โพสต์: 39
ลงทะเบียนเมื่อ: 31/07/2017 10:18 am

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

โพสต์ที่ยังไม่ได้อ่าน โดย thelordbank »

เราสามารถสร้าง 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
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: facebook.com [Crawler] และบุคลทั่วไป 72