API

Example

This assumes a corpus file (corpus.txt) and config file (config.yaml).

from twitter_markov import TwitterMarkov

tm = TwitterMarkov('example_screen_name', 'corpus.txt', config_file='config.yaml')
tweet = tm.compose()

# do something more with tweet, or use the Tweepy API in a different way

tm.api.update_status(tweet)

TwitterMarkov

For commands that generate text, the first corpus in the found corpora (or in the config file) will be the default. When using the class with more than corpus, you can specify a corpus with the model keyword argument using the basename of the given file, e.g. “special.txt” for the corpus stored at “dir/special.txt”.

class twitter_markov.TwitterMarkov(screen_name, corpus=None, **kwargs)

Posts markov-generated text to twitter

Parameters:
  • screen_name (str) – Twitter user account
  • corpus (str) – Text file to read to generate text.
  • api (tweepy.API) – API to use to post tweets.
  • dry_run (boolean) – If set, TwitterMarkov won’t actually post tweets.
  • blacklist (Sequence) – A list of words to avoid generating.
check_tweet(text)

Check if a string contains blacklisted words or is similar to a recent tweet.

compose(model=None, max_len=140, **kwargs)

Returns a string generated from “model” (or the default model).

learn_parent(corpus=None, parent=None)

Add recent tweets from the parent account (since the last time self.screen_name tweeted) to the corpus. This is subject to the filters described in bots.yaml.

recently_tweeted

Returns recent tweets from self.screen_name.

reply(status, model=None, **kwargs)

Compose a reply to the given tweepy.Status.

reply_all(model=None, **kwargs)

Reply to all mentions since the last time self.screen_name sent a reply tweet.

tweet(model=None, **kwargs)

Post a tweet composed by “model” (or the default model).

Checking

twitter_markov.checking.construct_tweet_checker(no_retweets=False, no_replies=False, no_badwords=True)

Returns a tweet checker

twitter_markov.checking.construct_tweet_filter(no_mentions=False, no_urls=False, no_media=False, no_hashtags=False, no_symbols=False)

returns a filter for tweet text

twitter_markov.checking.generator(tweets, return_status=None, **kwargs)

Returns a generator that returned a filtered input iterable of tweets or tweet-like objects (tweepy.Status objects or dictionaries).

Parameters:
  • tweets (iterable) –
  • return_status (boolean) – If true, returns entire status with modified test
  • no_retweets (boolean) – Exclude retweets (e.g. strings beginning RT) (default False)
  • no_replies (boolean) – Exclude replies (e.g. strings beginning @screen_name) (default False)
  • no_mentions (boolean) – Filter out mentions (e.g. strings containing @screen_name) (default False)
  • no_badwords (boolean) – Exclude derogatory terms for people (default True)
  • no_urls (boolean) – filter out exclude urls (default False)
  • no_hashtags (boolean) – filter out hashtags (default False)
  • no_media (boolean) – filter out media (twitter objects only) (default False)
  • no_symbols (boolean) – filter out symbols (twitter objects only) (default False)
twitter_markov.checking.reply_checker(tweet)
twitter_markov.checking.rt_checker(tweet)
twitter_markov.checking.wf_checker(tweet)