Added training function + dir cleanup

This commit is contained in:
BuildTools 2021-01-12 20:36:52 +01:00
parent b733f05293
commit 9f088473ac
2 changed files with 23 additions and 8 deletions

8
.gitignore vendored
View file

@ -1,5 +1,9 @@
# Exclude IDE Files # Exclude IDE Files
.idea .idea/
# Exclude VENV # Exclude VENV
/venv/ venv/
# Exclude ChatLogs
chatlogs/*

23
run.py
View file

@ -12,10 +12,6 @@ config = {"botname": "Sheepy",
"corpus": ["chatterbot.corpus.english", "chatterbot.corpus.uswest"], "corpus": ["chatterbot.corpus.english", "chatterbot.corpus.uswest"],
"token": "NzU2NTgxNjQ0NTY3MTgzMzcw.X2T7kA.J7RthGGYMjgE_ZreLrGo2A3tCvg"} "token": "NzU2NTgxNjQ0NTY3MTgzMzcw.X2T7kA.J7RthGGYMjgE_ZreLrGo2A3tCvg"}
file = open("token", "r")
token = file.read()
file.close()
bot = discord.Client() bot = discord.Client()
chatbot = ChatBot(config["botname"]) chatbot = ChatBot(config["botname"])
@ -32,6 +28,7 @@ def printversion():
async def download(): async def download():
await bot.change_presence(activity=discord.Game(name=" downloading Message History")) await bot.change_presence(activity=discord.Game(name=" downloading Message History"))
os.system("rm *.tmp.yml")
for trainchannel in config["trainchannels"]: for trainchannel in config["trainchannels"]:
channel = await bot.fetch_channel(trainchannel[0]) channel = await bot.fetch_channel(trainchannel[0])
counter = 0 counter = 0
@ -40,10 +37,22 @@ async def download():
messages[counter] = message.content messages[counter] = message.content
counter += 1 counter += 1
messages.reverse() messages.reverse()
with open(str(trainchannel[0]) + ".yml", "w") as logfile: with open("chatlogs/" + str(trainchannel[0]) + ".tmp.yml", "w") as logfile:
yaml.dump(messages, logfile) yaml.dump(messages, logfile)
async def train():
os.system("rm db.sqlite3")
corpustrainer = ChatterBotCorpusTrainer(chatbot)
listtrainer = ListTrainer(chatbot)
for corpus in config["corpus"]:
corpustrainer.train(corpus)
for log in os.listdir("chatlogs"):
with open(log, "r") as logfile:
log = yaml.load(logfile)
listtrainer.train(log)
@bot.event @bot.event
async def on_ready(): async def on_ready():
print("Logged in!") print("Logged in!")
@ -65,6 +74,8 @@ async def on_ready():
await bot.close() await bot.close()
if tasks[0]: if tasks[0]:
await download() await download()
if tasks[1]:
await train()
bot.run(token) bot.run(config["token"])