class RandomComposer
Public Class Methods
new(config, twitter_client, tweet_finder, emoji_finder)
click to toggle source
Calls superclass method
Composer::new
# File lib/composers/random_composer.rb, line 6 def initialize(config, twitter_client, tweet_finder, emoji_finder) super(config, twitter_client, tweet_finder, emoji_finder) end
Public Instance Methods
compose_image_query(query)
click to toggle source
# File lib/composers/random_composer.rb, line 23 def compose_image_query(query) find_random_word(query) end
compose_tweet()
click to toggle source
# File lib/composers/random_composer.rb, line 10 def compose_tweet tweets = @tweet_finder.find_tweets tweets = tweets.map { |t| normalize(t) } hashtag = find_random_word(tweets.sample) emoji = @emoji_finder.find text = tweets.join([', ', ' - ', '! '].sample) text = filter(text) text = trim(text, hashtag.length) text = replace_a_word(text.split.sample, text) text = replace_a_word(emoji, text) Tweet.new(text.capitalize, [hashtag], @twitter_client) end
Private Instance Methods
extract_one_word(tweet)
click to toggle source
# File lib/composers/random_composer.rb, line 35 def extract_one_word(tweet) tweet.split.reject { |w| /[^A-Za-z0-9]/.match(w.strip) != nil || @stop_words.include?(w.strip) }.sample end
filter(tweet)
click to toggle source
# File lib/composers/random_composer.rb, line 47 def filter(tweet) tweet.split.reject { |w| @blacklist.include?(w.downcase) }.join(' ') end
find_random_word(query)
click to toggle source
# File lib/composers/random_composer.rb, line 29 def find_random_word(query) term = extract_one_word(query) tweet = @tweet_finder.find_tweet(term) extract_one_word(tweet) end
normalize(tweet)
click to toggle source
# File lib/composers/random_composer.rb, line 39 def normalize(tweet) CGI.unescapeHTML(tweet.gsub(/(@([^\s])+|http([^\s])+|([^\s])+\/([^\s])+|)/, '')) .gsub(/\s+/, ' ') .strip .downcase .gsub(/[^[:word:]\s]/, '') end
replace_a_word(replacement, target)
click to toggle source
# File lib/composers/random_composer.rb, line 51 def replace_a_word(replacement, target) split = target.split out = split.select { |w| w.length >= replacement.length }.sample split.map { |w| w == out ? replacement : w }.join(' ') end
trim(tweet, hashtag_length)
click to toggle source
# File lib/composers/random_composer.rb, line 57 def trim(tweet, hashtag_length) if tweet.size > (@max_length - (hashtag_length > 0 ? hashtag_length + 1 : 0)) split = tweet.split split.delete_at(split.length - 1) return trim(split.join(' '), hashtag_length) end tweet end