Skip to content

Instantly share code, notes, and snippets.

@mecampbellsoup
Created September 27, 2013 13:51
Show Gist options
  • Select an option

  • Save mecampbellsoup/6728897 to your computer and use it in GitHub Desktop.

Select an option

Save mecampbellsoup/6728897 to your computer and use it in GitHub Desktop.
Tweet shortener!
tweets = ["test", "hey guys, can anyone teach me how to be cool? I really want to be the best at everything, you know what I mean? Tweeting is super fun you guys!!!!", "OMG you guys, you won't believe how sweet my kitten is. My kitten is like super cuddly and too cute to be believed right?", "I'm running out of example tweets for you guys, which is weird, because I'm a writer and this is just writing and I tweet all day. For real, you guys. For real.", "GUISEEEEE this is so fun! I'm tweeting for you guys and this tweet is SOOOO long it's gonna be way more than you would think twitter can handle, so shorten it up you know what I mean? I just can never tell how long to keep typing!", "this tweet shouldn't be shortened to too two"]
substitutes = { 2 => ["to", "two", "too"], 4 => ["for", "four"], "be" => ["b"], "you" => ["u"], "at" => ["@"], "and" => ["&"] }
def check_each_word_against_substitutes(tweets, substitutes)
tweets.each do |tweet|
word_count = 0
if tweet.length > 140
amended_tweet = tweet.split(" ")
amended_tweet.each do |word|
substitutes.each do |k,v|
if v.include?(word)
amended_tweet[word_count] = k.to_s
end
end
word_count += 1
end
if amended_tweet.length > 140
amended_tweet = amended_tweet[0,140]
end
if amended_tweet.join(" ").length > 140
ap amended_tweet = amended_tweet.join(" ")[0,140]
end
else
ap tweet
end
end
end
check_each_word_against_substitutes(tweets, substitutes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment