Skip to content

Instantly share code, notes, and snippets.

@topher6345
Last active March 20, 2016 21:32
Show Gist options
  • Save topher6345/76930eda9bed3f9da32d to your computer and use it in GitHub Desktop.
Save topher6345/76930eda9bed3f9da32d to your computer and use it in GitHub Desktop.
# YADTTMCG
## Yet another Donald Trump Tweet Markov Chain Generator
require 'sinatra'
require 'open-uri'
require 'nokogiri'
def next_word ary
ary[rand(ary.length).to_i][1]
end
def fetch_url
Nokogiri::HTML(open("https://twitter.com/realdonaldtrump"))
end
def scrub_strategy(doc)
doc.css('.tweet-text')
.text
.gsub(/@\w+/, " ")
.gsub(/#\w+/, " ")
.tr('"', ' ')
.gsub(/\ +/, " ")
.gsub('\n', "\n")
.gsub(/pic\.twitter\.com\/\w+/, "")
.gsub('!', "! ")
end
def stems_strategy(file)
pseudo_stems = file.each_cons(3).to_a.shuffle
pseudo_stems << file.each_cons(4).to_a.shuffle
pseudo_stems << file.each_cons(5).to_a.shuffle
pseudo_stems.shuffle.group_by { |word_pair| word_pair[0] }
end
def take_tweet(enum)
output = ""
140.times do
output << enum.next + " " rescue enum.rewind
end
output.chars.take(140).join
end
def make_enum(stems)
Enumerator.new do |e|
word = stems.first.first
while word
e << word
word = next_word(stems[word] || stems.first)
end
end
end
get '/' do
doc = fetch_url
file = scrub_strategy(doc).split(" ")
stems = stems_strategy(file)
enum = make_enum(stems)
take_tweet(enum)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment