Skip to content

Instantly share code, notes, and snippets.

@tastycode
Last active December 31, 2015 16:18
Show Gist options
  • Save tastycode/0b109772f032740d1fcb to your computer and use it in GitHub Desktop.
Save tastycode/0b109772f032740d1fcb to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Usage: echo "lyrics to a song" | ./sweater.rb
# Usage: ./sweater.rb lyrics.txt
# #Input: Billie Jean Lyrics from https://play.google.com/music/preview/Tkjszhsypodj4rcjo3yfxdzfuxi?lyrics=1&utm_source=google&utm_medium=search&utm_campaign=lyrics&pcampaignid=kp-lyrics&u=0#
# #Output
# lee baas roar pike they booty dean sum grey groovy seen
# lye bred dont mined slut glut crew flew jeanne my dram duh dunne
# chew brill trance john huh drawer chin duh crowned
# v fed cai gram uh mun shoo grill vance hon ca lore when huh found
#
# klee cold ne ur fame luz lily sheen says mi caused hey sheen
# wen reverie shed churned smith guise pratt schemed dove freeing uh donne
# through we'll glance con uh lore schwinn uh browned
#
# steeple hallways strolled dea sep prayerful gov tut nu queue
# gland dont so resound faking slung swirls parts
# fanned brother hallways bold leigh li prayerful glove goo yew shove
# panned sci prayerful gov putt lou tue cars duh eye succumbs duh youth
#
# villi bean his swat bligh hover
# Shes bust bay earl few blames mat guy dam ca done
# gut sta slid quiz caught lye hon
# klee his di nam ca mun cut huh sid his lat thai bun
#
# yore sporty braze gland sortie light's
# duh bra buzz bonn cur wide
# slut shoe chan sand thin shes shin command
# thur deems tanned dan's
# bras sep glanced hahn ca ore min ca hound
# oh fake shy wong reprice fussed december gu hallways mink bryce
# Dont pink bryce dont brink spice
require 'ruby_rhymes'
require 'raspell'
def rhyme_line(line)
words = line.split(/[\s\n]+/)
result = words.map do |word|
word.gsub(/[^\w\d]+/,'')
end.reject do |word|
word.strip.length.zero? || word =~ /[,'’]/
end.map do |word|
syllables_to_match = word.to_phrase.syllables
rhymed = word.to_phrase.flat_rhymes.select do |rhymed_word|
rhymed_word.to_phrase.syllables == syllables_to_match
end.select do |rhymed_word|
suggestions = $speller.suggest(rhymed_word)
is_spelled = rhymed_word.downcase == suggestions.first.downcase
end.sample
rhymed || word
end.join(' ')
end
$speller = Aspell.new("en_US")
$speller.suggestion_mode = Aspell::NORMAL
input = ARGF.read.strip
puts(input.lines.map do |line|
rhyme_line(line)
end.to_a.join("\n"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment