Better console progress. Delete data command (#109)

* Better console progress. Delete data command

* Reformat
pull/110/head
bobloy 5 years ago committed by GitHub
parent 90d033aa50
commit 31fec3add1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,4 +1,5 @@
import asyncio import asyncio
import os
import pathlib import pathlib
from datetime import datetime, timedelta from datetime import datetime, timedelta
@ -49,7 +50,7 @@ class Chatter(Cog):
self.loop = asyncio.get_event_loop() self.loop = asyncio.get_event_loop()
def _create_chatbot( def _create_chatbot(
self, data_path, similarity_algorithm, similarity_threshold, tagger_language self, data_path, similarity_algorithm, similarity_threshold, tagger_language
): ):
return ChatBot( return ChatBot(
"ChatterBot", "ChatterBot",
@ -72,8 +73,8 @@ class Chatter(Cog):
after = datetime.today() - timedelta(days=(await self.config.guild(ctx.guild).days())) after = datetime.today() - timedelta(days=(await self.config.guild(ctx.guild).days()))
convo_delta = timedelta(minutes=(await self.config.guild(ctx.guild).convo_delta())) convo_delta = timedelta(minutes=(await self.config.guild(ctx.guild).convo_delta()))
def predicate(message: discord.Message): def predicate(msg: discord.Message):
return message.clean_content return msg.clean_content
def new_conversation(msg, sent, out_in, delta): def new_conversation(msg, sent, out_in, delta):
# if sent is None: # if sent is None:
@ -98,7 +99,7 @@ class Chatter(Cog):
try: try:
async for message in channel.history( async for message in channel.history(
limit=None, after=after, oldest_first=True limit=None, after=after, oldest_first=True
).filter( ).filter(
predicate=predicate predicate=predicate
): # type: discord.Message ): # type: discord.Message
@ -131,20 +132,22 @@ class Chatter(Cog):
def _train_english(self): def _train_english(self):
trainer = ChatterBotCorpusTrainer(self.chatbot) trainer = ChatterBotCorpusTrainer(self.chatbot)
try: # try:
trainer.train("chatterbot.corpus.english") trainer.train("chatterbot.corpus.english")
except: # except:
return False # return False
return True return True
def _train(self, data): def _train(self, data):
trainer = ListTrainer(self.chatbot) trainer = ListTrainer(self.chatbot)
try: total = len(data)
for convo in data: # try:
if len(convo) > 1: for c, convo in enumerate(data, 1):
trainer.train(convo) if len(convo) > 1:
except: print(f"{c} / {total}")
return False trainer.train(convo)
# except:
# return False
return True return True
@commands.group(invoke_without_command=False) @commands.group(invoke_without_command=False)
@ -155,6 +158,33 @@ class Chatter(Cog):
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
pass pass
@chatter.command(name="cleardata")
async def chatter_cleardata(self, ctx: commands.Context, confirm: bool = False):
"""
This command will erase all training data and reset your configuration settings
Use `[p]chatter cleardata True`
"""
if not confirm:
await ctx.send(
"Warning, this command will erase all your training data and reset your configuration\n"
"If you want to proceed, run the command again as `[p]chatter cleardata True`"
)
return
async with ctx.typing():
await self.config.clear_all()
self.chatbot = None
await asyncio.sleep(
10
) # Pause to allow pending commands to complete before deleting sql data
if os.path.isfile(self.data_path):
os.remove(self.data_path)
self._create_chatbot(self.data_path, SpacySimilarity, 0.45, ENG_MD)
await ctx.tick()
@chatter.command(name="algorithm") @chatter.command(name="algorithm")
async def chatter_algorithm(self, ctx: commands.Context, algo_number: int): async def chatter_algorithm(self, ctx: commands.Context, algo_number: int):
""" """

Loading…
Cancel
Save