Exemplo n.º 1
0
user_input = "Type something to begin..."

# To make this Python 2.x compatible, replace the print() with print "enter print text here"
print(user_input)
'''
In this example we use a while loop combined with a try-except statement.
This allows us to have a conversation with the chat bot until we press
ctrl-c or ctrl-d on the keyboard.
'''

while True:
    try:
        '''
        ChatterBot's get_input method uses io adapter to get new input for
        the bot to respond to. In this example, the NoOutputAdapter gets the
        input from the user's terminal. Other io adapters might retrieve input
        differently, such as from various web APIs.
        '''
        user_input = bot.get_input()
        '''
        The get_response method also uses the io adapter to determine how
        the bot's output should be returned. In the case of the NoOutputAdapter,
        the output is not printed to the terminal.
        '''
        bot_input = bot.get_response(user_input)

        print(bot_input)

    except (KeyboardInterrupt, EOFError, SystemExit):
        break
Exemplo n.º 2
0
user_input = "Type something to begin..."

print(user_input)

'''
In this example we use a while loop combined with a try-except statement.
This allows us to have a conversation with the chat bot until we press
ctrl-c or ctrl-d on the keyboard.
'''

while True:
    try:
        '''
        ChatterBot's get_input method uses io adapter to get new input for
        the bot to respond to. In this example, the TerminalAdapter gets the
        input from the user's terminal. Other io adapters might retrieve input
        differently, such as from various web APIs.
        '''
        user_input = bot.get_input()

        '''
        The get_response method also uses the io adapter to determine how
        the bot's output should be returned. In the case of the TerminalAdapter,
        the output is printed to the user's terminal.
        '''
        bot_input = bot.get_response(user_input)

    except (KeyboardInterrupt, EOFError, SystemExit):
        break
# https://python3.wannaphong.com/2015/10/สร้าง-machine-learning-โต้ตอบสนทนา-python.html
__author__ = 'วรรณพงษ์'
from chatterbot import ChatBot

bot = ChatBot(
    "Training Bot Example",
    storage_adapter="chatterbot.adapters.storage.JsonDatabaseAdapter",
    logic_adapter="chatterbot.adapters.logic.ClosestMatchAdapter",
    io_adapter="chatterbot.adapters.io.TerminalAdapter",
    database="th_database.db"
)  # กำหนดค่า ฐานข้อมูลในนี้คือ th_database.db หากไม่มี ระบบจะทำการสร้างใหม่
'''
ให้ Bot พูดคุยสนทนาง่าย ๆ เพื่อช่วยให้มันได้เรียนรู้วิธีการตอบสนองต่อคำสั่งอื่น
กดปุ่ม ctrl-c หรือ ctrl-d เพื่อออกจากการสนทนา
'''
training_data = [
    "สวัสดีค่ะ", "ทำอะไรอยู่ค่ะ", "กำลังรอคำสั่งจากท่านค่ะ", "คุณเป็นใคร",
    "กินข้าวยัง", "ค่ะ"
]

bot.train(training_data)

user_input = ""

while True:
    try:
        user_input = bot.get_input()  # รับข้อความผู้ใช้
        bot_input = bot.get_response(user_input)  # ดึงข้อมูล
    except (KeyboardInterrupt, EOFError, SystemExit):
        break
    "CONSUMER_SECRET": "my-twitter-consumer-secret",
    "ACCESS_TOKEN": "my-access-token",
    "ACCESS_TOKEN_SECRET": "my-access-token-secret"
}
'''


chatbot = ChatBot("ChatterBot",
    storage_adapter="chatterbot.adapters.storage.TwitterAdapter",
    logic_adapter="chatterbot.adapters.logic.ClosestMatchAdapter",
    io_adapter="chatterbot.adapters.io.TerminalAdapter",
    database="../database.db",
    twitter_consumer_key=TWITTER["CONSUMER_KEY"],
    twitter_consumer_secret=TWITTER["CONSUMER_SECRET"],
    twitter_access_token_key=TWITTER["ACCESS_TOKEN"],
    twitter_access_token_secret=TWITTER["ACCESS_TOKEN_SECRET"]
)

user_input = "Type something to begin..."

print(user_input)

while True:
    try:
        user_input = chatbot.get_input()
        bot_input = chatbot.get_response(user_input)

    except (KeyboardInterrupt, EOFError, SystemExit):
        break

bot = ChatBot("Training Bot Example",
    storage_adapter="chatterbot.adapters.storage.JsonDatabaseAdapter",
    logic_adapter="chatterbot.adapters.logic.ClosestMatchAdapter",
    io_adapter="chatterbot.adapters.io.TerminalAdapter",
    database="th_database.db") # กำหนดค่า ฐานข้อมูลในนี้คือ th_database.db หากไม่มี ระบบจะทำการสร้างใหม่

'''
ให้ Bot พูดคุยสนทนาง่าย ๆ เพื่อช่วยให้มันได้เรียนรู้วิธีการตอบสนองต่อคำสั่งอื่น
กดปุ่ม ctrl-c หรือ ctrl-d เพื่อออกจากการสนทนา
'''
training_data = [
    "สวัสดีค่ะ",
    "ทำอะไรอยู่ค่ะ",
    "กำลังรอคำสั่งจากท่านค่ะ",
    "คุณเป็นใคร",
    "กินข้าวยัง",
    "ค่ะ"
]

bot.train(training_data)

user_input = ""

while True:
    try:
        user_input = bot.get_input() # รับข้อความผู้ใช้
        bot_input = bot.get_response(user_input) # ดึงข้อมูล
    except (KeyboardInterrupt, EOFError, SystemExit):
        break