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:
vylion 2017-09-19 17:58:56 +02:00
parent 13838527bb
commit 810e517757
6 changed files with 34 additions and 21 deletions

Binary file not shown.

Binary file not shown.

View file

@ -19,6 +19,17 @@ class Chatlog(object):
freq = 5 freq = 5
self.freq = freq 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): def add_msg(self, message):
msg = message.split() msg = message.split()
msg.append("!kvl") msg.append("!kvl")
@ -38,9 +49,6 @@ class Chatlog(object):
def get_count(self): def get_count(self):
return len(self.msgs) return len(self.msgs)
def set_freq(self, freq):
self.freq = freq
def to_txt(self): def to_txt(self):
lines = [self.id] lines = [self.id]
lines.append(self.type) lines.append(self.type)

View file

@ -8,7 +8,7 @@ class Markov(object):
self.words = [] self.words = []
if text is None: if text is None:
text = "" text = ""
self.words = text.split() self.words = ("!kvl\n"+text).split()
self.word_size = len(self.words) self.word_size = len(self.words)
self.database() self.database()
@ -26,25 +26,25 @@ class Markov(object):
def database(self): def database(self):
for w1, w2, w3 in self.triples(): for w1, w2, w3 in self.triples():
key = (w1, w2) key = (w1.casefold(), w2.casefold())
if key in self.cache: if key in self.cache:
self.cache[key].append(w3) self.cache[key].append(w3)
else: else:
self.cache[key] = [w3] self.cache[key] = [w3]
def generate_markov_text(self, size=50): def generate_markov_text(self, size=50):
seed = random.randint(0, self.word_size-3) seed = random.randint(0, self.word_size-4)
seed_word, next_word = self.words[seed], self.words[seed+1] seed_word, next_word, next_word2 = self.words[seed], self.words[seed+1], self.words[seed+2]
while "!kvl" in seed_word: while not "!kvl" in seed_word:
seed = random.randint(0, self.word_size-3) seed = random.randint(0, self.word_size-4)
seed_word, next_word = self.words[seed], self.words[seed+1] seed_word, next_word, next_word2 = self.words[seed], self.words[seed+1], self.words[seed+2]
w1, w2 = seed_word, next_word w1, w2 = next_word, next_word2
gen_words = [] gen_words = []
for i in range(size): for i in range(size):
gen_words.append(w1) 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") print("Generated text")
break break
else: 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) return ' '.join(gen_words)

View file

@ -59,7 +59,7 @@ def help(bot, update):
/about - What I'm about. /about - What I'm about.
/help - I send this message. /help - I send this message.
/count - I tell you how many messages from this chat I remember. /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. /speak - Forces me to speak.
""") """)
@ -85,9 +85,9 @@ def get_chatname(chat):
def read(bot, update): def read(bot, update):
global chatlogs global chatlogs
ident = str(update.message.chat.id) chat = update.message.chat
ident = str(chat.id)
if not ident in chatlogs: if not ident in chatlogs:
chat = update.message.chat
title = get_chatname(chat) title = get_chatname(chat)
chatlog = Chatlog(chat.id, chat.type, title) chatlog = Chatlog(chat.id, chat.type, title)
else: else:
@ -96,7 +96,12 @@ def read(bot, update):
if chatlog.get_count()%chatlog.freq == 0: if chatlog.get_count()%chatlog.freq == 0:
msg = chatlog.speak() msg = chatlog.speak()
# TO DO: añadir % de que haga reply en vez de send # TO DO: añadir % de que haga reply en vez de send
bot.sendMessage(chatlog.id, msg) 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) savechat(chatlog)
chatlogs[chatlog.id] = chatlog chatlogs[chatlog.id] = chatlog
@ -110,7 +115,9 @@ def speak(bot, update):
chatlog = Chatlog(chat.id, chat.type, title) chatlog = Chatlog(chat.id, chat.type, title)
else: else:
chatlog = chatlogs[ident] 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() msg = chatlog.speak()
update.message.reply_text(msg) update.message.reply_text(msg)
savechat(chatlog) savechat(chatlog)
@ -148,9 +155,7 @@ def set_freq(bot, update):
try: try:
value = update.message.text.split()[1] value = update.message.text.split()[1]
value = int(value) value = int(value)
if not value > 0: value = chatlogs[ident].set_freq(value)
raise ValueError('Tried to set 0 or negative freq value.')
chatlogs[ident].set_freq(value)
reply = "Frequency of speaking set to " + str(value) reply = "Frequency of speaking set to " + str(value)
except: except:
reply = "Format was confusing; frequency not changed from " + str(chatlogs[ident].freq) reply = "Format was confusing; frequency not changed from " + str(chatlogs[ident].freq)

BIN
velasco.zip Normal file

Binary file not shown.