diff --git a/__pycache__/chatlog.cpython-36.pyc b/__pycache__/chatlog.cpython-36.pyc index 2237ed8..201f5dc 100644 Binary files a/__pycache__/chatlog.cpython-36.pyc and b/__pycache__/chatlog.cpython-36.pyc differ diff --git a/__pycache__/markov.cpython-36.pyc b/__pycache__/markov.cpython-36.pyc index 69fd75e..0a7c9aa 100644 Binary files a/__pycache__/markov.cpython-36.pyc and b/__pycache__/markov.cpython-36.pyc differ diff --git a/chatlog.py b/chatlog.py index f57a380..c1c3186 100644 --- a/chatlog.py +++ b/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) diff --git a/markov.py b/markov.py index bc0725f..fe7007a 100644 --- a/markov.py +++ b/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) diff --git a/velasco.py b/velasco.py index 641d1a4..803c72c 100755 --- a/velasco.py +++ b/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) + chat = update.message.chat + ident = str(chat.id) if not ident in chatlogs: - chat = update.message.chat 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 - 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) 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) diff --git a/velasco.zip b/velasco.zip new file mode 100644 index 0000000..a14f1ea Binary files /dev/null and b/velasco.zip differ