class MarkovComposer

Public Class Methods

new(config, twitter_client, tweet_finder, emoji_finder) click to toggle source
Calls superclass method Composer::new
# File lib/composers/markov_composer.rb, line 6
def initialize(config, twitter_client, tweet_finder, emoji_finder)
  super(config, twitter_client, tweet_finder, emoji_finder)
  @chain = {}
  reader = TextReader.new(config.input_text_file)
  reader.process_input_text_file
  @beginnings = reader.beginnings
  @lines = reader.lines
  build_chain(@lines.join.split)
end

Public Instance Methods

compose_image_query(query) click to toggle source
# File lib/composers/markov_composer.rb, line 29
def compose_image_query(query)
  "##{@chain.keys.reject { |k| @stop_words.include?(k) }.sample}"
end
compose_tweet() click to toggle source
# File lib/composers/markov_composer.rb, line 16
def compose_tweet
  text = ''
  word = @beginnings.sample
  until text.count('.') == 1 || text.length + word.length + 1 >= @max_length
    text = "#{text}#{word} "
    word = get(word)
  end
  text = clean_up(text)
  tweet = create_tweet(text)
  insert_emoji(tweet) if [true, false].sample
  tweet
end

Private Instance Methods

add(word, next_word) click to toggle source
# File lib/composers/markov_composer.rb, line 41
def add(word, next_word)
  @chain[word] = Hash.new(0) if @chain[word] == nil
  @chain[word][next_word] += 1
end
build_chain(words) click to toggle source
# File lib/composers/markov_composer.rb, line 35
def build_chain(words)
  words.each_with_index do |word, i|
    add(word, words[i + 1]) unless i >= words.size - 2
  end
end
clean_up(text) click to toggle source
# File lib/composers/markov_composer.rb, line 57
def clean_up(text)
  text = text.strip.capitalize
  if text[text.length - 1].match('[A-Za-z]') != nil && text.length < @max_length
    text << %w(. ! ? . .).sample
  end
  text
end
create_tweet(text) click to toggle source
# File lib/composers/markov_composer.rb, line 65
def create_tweet(text)
  hashtags = []
  hashtags << @default_hashtag if @default_hashtag != '' && (text.length + @default_hashtag.length + 2) < @max_length
  tweet = Tweet.new(text, hashtags, @twitter_client)
  if [true, false].sample
    hashtag = @lines.sample.split.reject { |w| w.strip.nil? || /[^A-Za-z]/.match(w.strip) != nil || @stop_words.include?(w.strip) }.sample
    tweet.hashtags << hashtag if hashtag != nil && (tweet.length + hashtag.length + 2) < @max_length
  end
  tweet
end
get(word) click to toggle source
# File lib/composers/markov_composer.rb, line 46
def get(word)
  return '' if @chain[word] == nil
  sum = @chain[word].reduce(0) { |sum, kv| sum += kv[1] }
  random = rand(sum) + 1
  count = 0
  @chain[word].find do |w, i|
    count += i
    count >= random
  end.first
end
hashtag_fits?(tweet, hashtag) click to toggle source
# File lib/composers/markov_composer.rb, line 82
def hashtag_fits?(tweet, hashtag)
  hashtag != nil && (tweet.length + hashtag.length + 1) < @max_length
end
insert_emoji(tweet) click to toggle source
# File lib/composers/markov_composer.rb, line 76
def insert_emoji(tweet)
  if tweet.length < @max_length + " #{127872.chr('utf-8')}".length
    tweet.append(@emoji_finder.find)
  end
end