28 Aug 2020
Chatterbot und Buddha oder Konfuzius - eine einfache Demonstration der Möglichkeiten
Wenn man den Chatterbot erfolgreich installiert hat, hier eine einfache Anwendungsmöglichkeit mit Training anhand Buddhas weiser Aussagen.
Das Test-Program (Python3) ist hoffentlich selbsterklärend. Habt Spass! Und immer dran denken: der Chatterbot lernt mit!
Python-Programm buddha.py
#!/usr/bin/python3.6
#
# basic from chatterbot docs
# extended by chrissie 08.2020
#
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)
# Create a new chat bot
buddha = ChatBot('Buddha',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
database_uri='sqlite:///buddha.sqlite3'
)
# do only training one time
# training will be saved somewhere
# user input will be added to training
training = 0
if training == 1:
# reset database - beware!
#chatbot.storage.drop()
# read buddha fortunes for training
f_lines = []
with open('./101buddhafortune.txt') as f:
for curline in f:
# skip % in fortunes files
if not curline.startswith("%"):
# rstrip removes line endings
f_lines.append(curline.rstrip())
#print (curline.rstrip())
trainer = ListTrainer(chatbot)
trainer.train (f_lines)
# alternative training - beware
#trainer.train([
# "Hi, can I help you?",
# "Sure, I'd like to book a flight to Iceland.",
# "Your flight has been booked."
#])
print('Type something to begin... - CTRL-C quits')
# The following loop will execute each time the user enters input
while True:
try:
user_input = input()
bot_response = chatbot.get_response(user_input)
print("-- Buddha responds: ", end='')
print(bot_response)
# Press ctrl-c or ctrl-d on the keyboard to exit
except (KeyboardInterrupt, EOFError, SystemExit):
break
Demonstration des Programmes
es wurde bereits einmal initial trainiert
Ikkyu Sojun, Meister des Zen-Buddhismus, überwacht diesen Vorgang.
chrissie@fehmarn ~ $ ./buddha.py
Type something to begin... - CTRL-C quits
what is the meaning of life
-- Buddha responds: Peace comes from within. Do not seek it without.
Are you sure?
-- Buddha responds: It is better to do nothing, than to do what is wrong.
For whatever you do, you do to yourself.
Ok. many thanks for your help.
-- Buddha responds: Be vigilant; guard your mind against negative thoughts
Heureka!
Das Programm buddha.py kann man hier downloaden. die Datei 101buddhafortunes.txt kann man wie bereits bekannt hier downloaden.
Budda versus Konfuzius
Jetzt wirds spannend! Lassen wir Buddha und Konfuzius diskutieren.
Achtung! Spätestens ab jetzt muss für jeden Bot ein eigenes sqlite3-File angegeben werden. Sonst schreiben sie alle in das default db.sqlite3 und verhalten sich gleich. Für euch getestet! Der Name macht hier nicht die Unterscheidung!
Dazu verwenden wir buddha-vs-confuzius.py
#!/usr/bin/python3.6
#
# basic from chatterbot docs
# extended by chrissie 08.2020
#
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import time
import random
# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)
# Create a new chat bot
buddha = ChatBot('Buddha',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
database_uri='sqlite:///buddha.sqlite3'
)
confucius = ChatBot('Confucius',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
database_uri='sqlite:///conficius.sqlite3'
)
# read buddha, confucius fortunes
b_lines = []
c_lines = []
with open('./101buddhafortune.txt') as f:
for curline in f:
if not curline.startswith("%"):
b_lines.append(curline.rstrip())
with open('./75-confucius-fortunes.txt') as f:
for curline in f:
if not curline.startswith("%"):
c_lines.append(curline.rstrip())
# do only training one time
# training will be saved somewhere
# user input will be added to training
training = 0
if training == 1:
# reset database - beware!
buddha.storage.drop()
confucius.storage.drop()
buddha_trainer = ListTrainer(buddha)
buddha_trainer.train (b_lines)
confucius_trainer = ListTrainer(confucius)
confucius_trainer.train (c_lines)
exit (1)
# get initial random confucius citication for buddha
c_count = len (c_lines)
c_rnd = random.randint(0, c_count)
c_rnd = c_rnd - 1
# get initial buddha response for the endless loop starting with confucius
buddha_response = buddha.get_response(c_lines[c_rnd])
# The following loop will execute each time the user enters input
while True:
try:
confucius_response = confucius.get_response(buddha_response)
print("+ Confucius says: ", end='')
print(confucius_response)
time.sleep(5)
buddha_response = buddha.get_response(confucius_response)
print("! Buddha says: ", end='')
print(buddha_response)
time.sleep(5)
# Press ctrl-c or ctrl-d on the keyboard to exit
except (KeyboardInterrupt, EOFError, SystemExit):
break
Demonstration des Programmes - Buddha und Konfuzius wurden einmal inital traniert
Über was sie sich jetzt genau unterhalten, da kann ich leider nur spekulieren. Bei philosophischen Diskussionen halte ich mich gerne zurück.
Ikkyu Sojun, Meister des Zen-Buddhismus, sowie Confucius ist zugegen.
[...]
+ Confucius says: The superior man is modest in his speech, but exceeds in his actions.
! Buddha says: An insincere and evil friend is more to be feared than a wild beast; a wild beast may wound your body, but an evil friend will wound your mind.
+ Confucius says: Wisdom, compassion, and courage are the three universally recognized moral qualities of men.
! Buddha says: You will always be getting praise and blame, but do not let either affect the poise of the mind.
+ Confucius says: The will to win, the desire to succeed, the urge to reach your full potential... these are the keys that will unlock the door to personal excellence.
! Buddha says: Speak the truth, do not yield to anger; give, if thou art asked for little; by these three steps thou wilt go near the gods.If you do not change direction, you may end up where you are heading.
[...]
Das Programm buddha-vs-confucius.py kann man hier downloaden. Die Data 75-confucius-fortunes.txt kann man hier downloaden.
Lao Tze will mitdiskutieren.
Ok, schnell eine Erweiterung
#!/usr/bin/python3.6
#
# basic from chatterbot docs
# extended by chrissie 08.2020
#
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import time
import random
# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)
# Create a new chat bot
buddha = ChatBot('Buddha',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
database_uri='sqlite:///buddha.sqlite3'
)
confucius = ChatBot('Confucius',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
database_uri='sqlite:///conficius.sqlite3'
)
laotze = ChatBot('Laotze',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
database_uri='sqlite:///laotze.sqlite3'
)
# read buddha, confucius, lao fortunes
b_lines = []
c_lines = []
l_lines = []
with open('./101buddhafortune.txt') as f:
for curline in f:
if not curline.startswith("%"):
b_lines.append(curline.rstrip())
with open('./75-confucius-fortunes.txt') as f:
for curline in f:
if not curline.startswith("%"):
c_lines.append(curline.rstrip())
with open('./190-lao-fortunes.txt') as f:
for curline in f:
if not curline.startswith("%"):
l_lines.append(curline.rstrip())
# debugging
#print ('\n'.join(map(str, l_lines)))
#print ('\n'.join(map(str, b_lines)))
#print ('\n'.join(map(str, c_lines)))
# do only training one time first run then change training
# training will be saved somewhere
training = 0
if training == 1:
# reset database - beware!
buddha.storage.drop()
confucius.storage.drop()
laotze.storage.drop()
buddha_trainer = ListTrainer(buddha)
buddha_trainer.train (b_lines)
confucius_trainer = ListTrainer(confucius)
confucius_trainer.train (c_lines)
laotze_trainer = ListTrainer(laotze)
laotze_trainer.train (l_lines)
exit (1)
# get intial random citication
c_count = len (c_lines)
c_rnd = random.randint(0, c_count)
c_rnd = c_rnd - 1
# get initial response
buddha_response = buddha.get_response(c_lines[c_rnd])
confucius_response = confucius.get_response(c_lines[c_rnd])
laotze_response = laotze.get_response(c_lines[c_rnd])
# simple test, each bot must respond differently
#print ("rnd: " + str(c_rnd))
#print ("in: " + c_lines[c_rnd])
#print (buddha_response)
#print (confucius_response)
#print (laotze_response)
#exit (1)
# https://chatterbot.readthedocs.io/en/stable/conversations.html
while True:
try:
confucius_response = confucius.get_response(str(laotze_response) + str(buddha_response))
print("+ Confucius says: ", end='')
print(confucius_response)
time.sleep(1)
buddha_response = buddha.get_response(str(confucius_response) + str(laotze_response))
print("! Buddha says : ", end='')
print(buddha_response)
time.sleep(1)
laotze_response = laotze.get_response (str(buddha_response) + str(confucius_response))
print("# Laotze says : ", end='')
print(laotze_response)
time.sleep(1)
# Press ctrl-c or ctrl-d on the keyboard to exit
except (KeyboardInterrupt, EOFError, SystemExit):
break
Test der drei Philosophen
Wer schneidet besser ab? Ich persönlich tippe auf Lao Tze.
[...]
+ Confucius says: The superior man understands what is right; the inferior man understands what will sell.
! Buddha says : The greatest prayer is patience.
# Laotze says : He who controls others may be powerful, but he who has mastered himself is mightier still.
+ Confucius says: It does not matter how slowly you go as long as you do not stop.
! Buddha says : If we could see the miracle of a single flower clearly, our whole life would change.
# Laotze says : All things in the world come from being. And being comes from non-being.
+ Confucius says: >If you look into your own heart, and you find nothing wrong there, what is there to worry about? What is there to fear?
! Buddha says : Every human being is the author of his own health or disease.
# Laotze says : I have just three things to teach: simplicity, patience, compassion. These three are your greatest treasures.
+ Confucius says: Study the past, if you would divine the future.
! Buddha says : Endurance is one of the most difficult disciplines, but it is to the one who endures that the final victory comes.
[...]
Das Programm buddha-vs-confucius-vs-laotze.py kann man hier downloaden. Die Data 190-lao-fortunes.txt kann man hier downloaden.
Websockets-Version
Das Programm websockets-buddha-vs-confucius-vs-laotze.py kann man hier downloaden.