Skip to content

Instantly share code, notes, and snippets.

@jamis
Created March 12, 2016 16:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamis/91cb91a5a0106811b8e4 to your computer and use it in GitHub Desktop.
Save jamis/91cb91a5a0106811b8e4 to your computer and use it in GitHub Desktop.
A script for searching a dictionary file for words that are relatable via a simple text substitution
# Inspired by a church billboard that read:
# "When I becomes we, illness becomes welness"
#
# usage:
# > ruby change-x-for-y.rb i we
# illness wellness
# inch wench
# it wet
# ...
a = ARGV.shift or "you must specify two words that are replaceable"
b = ARGV.shift or "you must specify TWO words that are replaceable"
a = a.downcase
b = b.downcase
words = File.readlines("/usr/share/dict/words").map { |w| w.strip.downcase }
dict = words.inject({}) { |h,k| h[k] = k.length; h }
words.each do |word|
next if word == a
word2 = word.gsub(/#{a}/, b)
if word2 != word && dict[word2]
puts "#{word} -> #{word2}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment