From 810e5177571512354604c3226208926215a27236 Mon Sep 17 00:00:00 2001 From: vylion Date: Tue, 19 Sep 2017 17:58:56 +0200 Subject: [PATCH] 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 --- __pycache__/chatlog.cpython-36.pyc | Bin 1963 -> 2207 bytes __pycache__/markov.cpython-36.pyc | Bin 1723 -> 1833 bytes chatlog.py | 14 +++++++++++--- markov.py | 20 ++++++++++---------- velasco.py | 21 +++++++++++++-------- velasco.zip | Bin 0 -> 3706 bytes 6 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 velasco.zip diff --git a/__pycache__/chatlog.cpython-36.pyc b/__pycache__/chatlog.cpython-36.pyc index 2237ed8fa4dd93abeb497b6e41d9fff8f80299b2..201f5dc36a4b327b6aaf6d518404b913a2e82340 100644 GIT binary patch delta 551 zcmYk2&ubGw6vyXfW;feyV$+xgto^Z(8rh(wqIeMxQoQuw50rw7P}c6)Zo6c0c8m}r zN)_=WgokHy_t=Y|BK{kTcg6p}oA2dNJiO1mc|T^p?|qv4IPYJn*Zud`e?0u^j{WG= zwh%oe!hlGfPVtT&U>Z$vbkz?-$lE@c#VFV+wLw3Mg)olaRGVDGusZIi$2lPR(5#%Q5>%G{f106gu z`e`tTvuAoeoxBB7U;5j8y=6>paH4t_&hXzgf&dSEG6gOS8~=;uSMV>qgAN{%tTwuG z3%YnuK5|#7&n)4wyat!>kIdmRMrs9rtLKeHN|oS;o|)mIHs;Q9>nypUoUX8Rvb3)Y zvy2mGk(U0&`QGKdnFWRTlM89eh9^DGbG)jjFs^R>18`e&c#)*YIk3tv@{D=nGoAvCBJ5+<|acs zG3XED#1!$)E5a5&db3(c&K!Z0gZGCM?V1t|B7#q_0ly5p_~dI?#JOKTD$w}v@d~Lb fVS}(qs1vgP5l~Avgsp@+2`Z36`qGsgPh{g4XO%^d diff --git a/__pycache__/markov.cpython-36.pyc b/__pycache__/markov.cpython-36.pyc index 69fd75edca47bf60a8ff7b0bccff7b3211ef7952..0a7c9aa12ca06790923403c67fda5e453622d31c 100644 GIT binary patch delta 633 zcmZvZ%WG3X6vofF^SC#+B-T_-BlIPqH7;D3RY6PfLB&NB6pBS#V&*3FCgw4@)DY$p zFydCi?7J7-32t18JGY99{09o|#g%8;jSCm^8_vvkzL{_4WA<$>Q^{t-dpABk{x;3L zyR-!WhhX3UnvQ5tK`YRU3R6^`!2(RbuFbLP3`SzDb<_^0zjv@=w{ayrirTe7z5A@2 z*nQl<-22z~8OM*va9A}E!vt=_)0UE%BF|BL!tyFix~)Fe~C3288F zB;4QxDdt|9F+nVhbOFfBZ^^+IBV$RCeaD7Ju09>{Jqgr_>H+kRjh{fAaXbHL$0RPo z1Wym@A5(rm4oxsYdWE2>k@Ea-$@N)>dBTXhzZvCrqE4i1Nu*YjXqf!L)$-g->RLy2 zpU-?ZA9fNQxGv$4cevni-r?jmMbF+B3mDqE*jU&LJLGflqN~+4U2vL3I~8{dS1`mp jUL|tO5tlJQ?ycKjqJ$T1UT$K^zL5o6m&>?hU&_tD<2sTd delta 442 zcmXAjy-QqC5XI-b`|&viK2#@Nn^MwQpD0V(u!u zI)&Uem0%%+G^s+06d_frGz$I&mi_}F&dU~a?mctv%>7+Xe6PeSl}d8^?Z?jZv-opk zorsQz=>v621tX`ho$5=q}^%H(#_l?XBH<>aj=mouB z-&YPUaEigfVc;}t$o)0O}>IIv97KiEC3z+tfs>sq$&r>VKJCC?JflUBqE# z*pw79Y`eC9&W7z_AT+ic=3fS3`agY3y%F01Wl@=o-_)tVcyDhc%r=WTEFk1zD@P)5 z`GZob{=hsz;!n(+zc90i{V%f^<}+nh^Z(4t+9M>GLPJzzLUaZZbkOj7b{=(qYS-}8 P|Ft#$$2RcT-`TZ$RaRk6 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 0000000000000000000000000000000000000000..a14f1ea62e07e3abc2fe72f82b1376a997ba6ee2 GIT binary patch literal 3706 zcmaKvXEdDc+J=WQ(G%SaqDSw8ArXC)8KT6f(K04_CtCF0f*>MF)L;aI2%?t=(M1m# z6NKm@QD3oT?X|z1_3m%)^{i_>_x=%*$olvQzOFzHb&%sO7)bSI8}Tt z(i6V8b9;rwi5GmwAPR?S9i)y@eD{_ZI#CnCG=iwHaVf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