mirror of
https://gitlab.com/vylion/velascobot.git
synced 2025-04-19 21:46:35 +02:00
Uploading Velasco v1.3
- Now starts a message only on the start of a memorized message - Now does markov chain key lookups with casefold for extra flexibility - Now saves on /freq change too - Now updates chat name in chatlog if it changes
This commit is contained in:
parent
13838527bb
commit
810e517757
6 changed files with 34 additions and 21 deletions
Binary file not shown.
Binary file not shown.
14
chatlog.py
14
chatlog.py
|
@ -19,6 +19,17 @@ class Chatlog(object):
|
|||
freq = 5
|
||||
self.freq = freq
|
||||
|
||||
def set_title(self, title):
|
||||
self.title = title
|
||||
|
||||
def set_freq(self, freq):
|
||||
if not freq > 0:
|
||||
raise ValueError('Tried to set 0 or negative freq value.')
|
||||
elif freq > 100000:
|
||||
freq = 100000
|
||||
self.freq = freq
|
||||
return self.freq
|
||||
|
||||
def add_msg(self, message):
|
||||
msg = message.split()
|
||||
msg.append("!kvl")
|
||||
|
@ -38,9 +49,6 @@ class Chatlog(object):
|
|||
def get_count(self):
|
||||
return len(self.msgs)
|
||||
|
||||
def set_freq(self, freq):
|
||||
self.freq = freq
|
||||
|
||||
def to_txt(self):
|
||||
lines = [self.id]
|
||||
lines.append(self.type)
|
||||
|
|
20
markov.py
20
markov.py
|
@ -8,7 +8,7 @@ class Markov(object):
|
|||
self.words = []
|
||||
if text is None:
|
||||
text = ""
|
||||
self.words = text.split()
|
||||
self.words = ("!kvl\n"+text).split()
|
||||
self.word_size = len(self.words)
|
||||
self.database()
|
||||
|
||||
|
@ -26,25 +26,25 @@ class Markov(object):
|
|||
|
||||
def database(self):
|
||||
for w1, w2, w3 in self.triples():
|
||||
key = (w1, w2)
|
||||
key = (w1.casefold(), w2.casefold())
|
||||
if key in self.cache:
|
||||
self.cache[key].append(w3)
|
||||
else:
|
||||
self.cache[key] = [w3]
|
||||
|
||||
def generate_markov_text(self, size=50):
|
||||
seed = random.randint(0, self.word_size-3)
|
||||
seed_word, next_word = self.words[seed], self.words[seed+1]
|
||||
while "!kvl" in seed_word:
|
||||
seed = random.randint(0, self.word_size-3)
|
||||
seed_word, next_word = self.words[seed], self.words[seed+1]
|
||||
w1, w2 = seed_word, next_word
|
||||
seed = random.randint(0, self.word_size-4)
|
||||
seed_word, next_word, next_word2 = self.words[seed], self.words[seed+1], self.words[seed+2]
|
||||
while not "!kvl" in seed_word:
|
||||
seed = random.randint(0, self.word_size-4)
|
||||
seed_word, next_word, next_word2 = self.words[seed], self.words[seed+1], self.words[seed+2]
|
||||
w1, w2 = next_word, next_word2
|
||||
gen_words = []
|
||||
for i in range(size):
|
||||
gen_words.append(w1)
|
||||
if "!kvl" in w2 or not (w1, w2) in self.cache:
|
||||
if "!kvl" in w2 or not (w1.casefold(), w2.casefold()) in self.cache:
|
||||
print("Generated text")
|
||||
break
|
||||
else:
|
||||
w1, w2 = w2, random.choice(self.cache[(w1, w2)])
|
||||
w1, w2 = w2, random.choice(self.cache[(w1.casefold(), w2.casefold())])
|
||||
return ' '.join(gen_words)
|
||||
|
|
19
velasco.py
19
velasco.py
|
@ -59,7 +59,7 @@ def help(bot, update):
|
|||
/about - What I'm about.
|
||||
/help - I send this message.
|
||||
/count - I tell you how many messages from this chat I remember.
|
||||
/freq - Change the frequency of both my messages and the times I save my learned vocabulary.
|
||||
/freq - Change the frequency of both my messages and the times I save my learned vocabulary. (Maximum of 100000)
|
||||
/speak - Forces me to speak.
|
||||
""")
|
||||
|
||||
|
@ -85,9 +85,9 @@ def get_chatname(chat):
|
|||
|
||||
def read(bot, update):
|
||||
global chatlogs
|
||||
ident = str(update.message.chat.id)
|
||||
if not ident in chatlogs:
|
||||
chat = update.message.chat
|
||||
ident = str(chat.id)
|
||||
if not ident in chatlogs:
|
||||
title = get_chatname(chat)
|
||||
chatlog = Chatlog(chat.id, chat.type, title)
|
||||
else:
|
||||
|
@ -96,7 +96,12 @@ def read(bot, update):
|
|||
if chatlog.get_count()%chatlog.freq == 0:
|
||||
msg = chatlog.speak()
|
||||
# TO DO: añadir % de que haga reply en vez de send
|
||||
try:
|
||||
bot.sendMessage(chatlog.id, msg)
|
||||
except TelegramError:
|
||||
chatlog.set_freq(chatlog.freq + 20)
|
||||
if get_chatname(chat) != chatlog.title:
|
||||
chatlog.set_title(get_chatname(chat))
|
||||
savechat(chatlog)
|
||||
|
||||
chatlogs[chatlog.id] = chatlog
|
||||
|
@ -110,7 +115,9 @@ def speak(bot, update):
|
|||
chatlog = Chatlog(chat.id, chat.type, title)
|
||||
else:
|
||||
chatlog = chatlogs[ident]
|
||||
chatlog.add_msg(update.message.text)
|
||||
text = update.message.text.split()
|
||||
if len(text) > 1:
|
||||
chatlog.add_msg(' '.join(text[1:]))
|
||||
msg = chatlog.speak()
|
||||
update.message.reply_text(msg)
|
||||
savechat(chatlog)
|
||||
|
@ -148,9 +155,7 @@ def set_freq(bot, update):
|
|||
try:
|
||||
value = update.message.text.split()[1]
|
||||
value = int(value)
|
||||
if not value > 0:
|
||||
raise ValueError('Tried to set 0 or negative freq value.')
|
||||
chatlogs[ident].set_freq(value)
|
||||
value = chatlogs[ident].set_freq(value)
|
||||
reply = "Frequency of speaking set to " + str(value)
|
||||
except:
|
||||
reply = "Format was confusing; frequency not changed from " + str(chatlogs[ident].freq)
|
||||
|
|
BIN
velasco.zip
Normal file
BIN
velasco.zip
Normal file
Binary file not shown.
Loading…
Reference in a new issue