#!/home/naiji/mastodon/maritalk-bot/venv/bin/python import sys import numpy as np from mastodon import Mastodon # -------------------------------------------------- def main(): mastodon = Mastodon( access_token = 'token.dat', api_base_url = 'https://your.site/' ) text = open('source.dat', encoding='utf8').read() # ORIGINAL IMPLEMENTATION: # https://gist.github.com/bpshaver/840d53222b72d2612ddf6702ef020274#file-markov_text_sim-py source = text.split() def make_pairs(source): for i in range(len(source)-1): yield (source[i], source[i+1]) pairs = make_pairs(source) word_dict = {} for left_w, right_w in pairs: if left_w in word_dict.keys(): word_dict[left_w].append(right_w) else: word_dict[left_w] = [right_w] first_word = np.random.choice(source) while first_word.islower(): first_word = np.random.choice(source) chain = [first_word] ch = '' while not ch.endswith('.'): ch = np.random.choice(word_dict[chain[-1]]) chain.append(ch) toot = ' '.join(chain) mastodon.status_post(toot, media_ids=None, visibility='unlisted') if __name__ == '__main__': sys.exit(main())