Oggi vi presento la realizzazione di un chatbot realizzato utilizzando Python e delle librerie associate.
Qui viene contrassegnato il repository git con i file ed il codice utilizzati: https://github.com/MattiaGatto/chatbot-MATT_AI_BOT.git
![](https://static.wixstatic.com/media/2bf71c_ac10127b08b947bf92d64bd4b28367aa~mv2.jpg/v1/fill/w_980,h_791,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/2bf71c_ac10127b08b947bf92d64bd4b28367aa~mv2.jpg)
LIBRERIE NECESSARIE ALL'UTILIZZO
E' necessario effettuare l'installazione delle seguenti librerie:
!pip install chatterbot==1.0.4
!pip install chatterbot_corpus
!pip install googletrans==3.1.0a0
Importare le seguenti ulteriori librerire:
# Importing Relevant Libraries
import json
import string
import random
import time
time.clock = time.time
import chatterbot
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import googletrans
from googletrans import Translator
from chatterbot.trainers import ChatterBotCorpusTrainer
MATT_AI_BOT.py
def MATT_AI_ChatBot():
my_bot = ChatBot(name='MATT_AI', read_only=True, logic_adapters=[
'chatterbot.logic.MathematicalEvaluation', 'chatterbot.logic.BestMatch'])
corpus_trainer = ChatterBotCorpusTrainer(my_bot)
corpus_trainer.train('chatterbot.corpus.english')
# translator.translate('Hi', dest='it', src='en').text
MATT_AI_bot = corpus_trainer.chatbot
return MATT_AI_bot
question_answers=[
[
['how old are you?','how old are you'],
['I was born in 2022, October 14th, do a calculation yourself']
],
[
['who are you?','who are you','tell me who you are'],
['I am an artificial intelligence, developed by my father Mattia Gatto!']
],
[
['hi','hello', 'bye-bye', 'bye','hi!','hello!', 'bye-bye!', 'bye!'],
['Hi','Hello', 'Bye-Bye', 'Bye'
'Hi,I am an artificial intelligence, developed by my father Mattia Gatto!']
],
[
['how are you?','how are you'],
['I am good.',
'That is good to hear.']
],
[
['thank you','thank you!'],
['You are welcome.']
]
]
def risposta(message, MATT_AI_bot,translator):
try:
trans_quest = translator.translate(str(message.strip()), dest='en', src='it').text
question_answers_bool=False
for i in range (len(question_answers)):
if (trans_quest.lower() in question_answers[i][0]):
question_answers_bool=True
resp = question_answers[i][1][random.randint(0, len(question_answers[i][1])-1)]
if question_answers_bool==False:
resp = MATT_AI_bot.get_response(trans_quest)
trans_resp = translator.translate(str(resp), dest='it', src='en').text
except:
trans_resp='👍'
print(trans_resp)
return str(trans_resp)
def MATT_AI_BOT(MATT_AI_bot):
print("Scrivi 'esci', se non vuoi chattare con il nostro ChatBot.")
while True:
message = input("")
if message == "esci":
break
risposta(message, MATT_AI_bot,translator)
translator = Translator()
# MATT_AI_BOT(MATT_AI_ChatBot())
E' possibile eseguire l'ultimo comando commentato al fine di eseguire il programma su riga di comando.
Definizione di un interfaccia grafica: Chat_bot_GUI.py
Scrivendo il seguendo script Python che sfrutta la libreria tkinter è possibile definire di una piccola interfaccia grafica.
from tkinter import *
import MATT_AI_BOT
import googletrans
from googletrans import Translator
translator = Translator()
MATT_AI_bot=MATT_AI_BOT.MATT_AI_ChatBot()
# GUI
root = Tk()
root.title("MATT_AI_Chatbot")
BG_GRAY = "#ABB2B9"
BG_COLOR = "#17202A"
TEXT_COLOR = "#EAECEE"
FONT = "Helvetica 14"
FONT_BOLD = "Helvetica 13 bold"
root.configure(bg=BG_COLOR)
root.geometry('775x670')
root.resizable(0,0)
# Send function
def send():
send = " You -> " + e.get()
txt.insert(END, "\n" + send)
user = e.get().lower()
txt.insert(END, "\n" + " Matt_AI -> "+ MATT_AI_BOT.risposta(e.get(), MATT_AI_bot,translator))
e.delete(0, END)
def Close():
root.destroy()
lable1 = Label(root, bg=BG_COLOR, fg=TEXT_COLOR, text="Benvenuto chatta con MATT_AI_BOT !", font=FONT_BOLD, pady=15, width=50, height=1).grid(row=0)
txt = Text(root, bg=BG_COLOR, fg=TEXT_COLOR, font=FONT, width=70)
txt.grid(row=1, column=0, columnspan=2)
scrollbar = Scrollbar(txt)
scrollbar.place(relheight=1, relx=0.974)
e = Entry(root, bg="#2C3E50", fg=TEXT_COLOR, font=FONT, width=65)
e.grid(row=2, column=0)
txt.insert(END, "\n" + " Matt_AI -> Ciao, sono un'intelligenza artificiale, sviluppata da mio padre Mattia Gatto!")
send = Button(root, text="Send", font=FONT_BOLD, bg=BG_GRAY,command=send).grid(row=2, column=1)
exit = Button(root, text="Exit", font=FONT_BOLD, bg=BG_GRAY,command=Close).grid(row=3, column=0, pady = 10)
root.mainloop()
Ecco l'output generato:
![](https://static.wixstatic.com/media/2bf71c_4475b29d3ad642a697c07b288ec4b70a~mv2.png/v1/fill/w_957,h_867,al_c,q_90,enc_auto/2bf71c_4475b29d3ad642a697c07b288ec4b70a~mv2.png)
Comments