Homonym translator
Alec Jacobson
March 13, 2011
I wrote a little ruby program that accepts a string and translates as many words as it can to homonyms.
Save this in a file called homotranslate.rb:
#!/usr/local/bin/ruby -w
class Homo
def initialize(filename)
lines = File.open(filename, "r").readlines;
homonyms = lines.collect{|e| e.strip.split(' ')};
@homo_map = Hash.new;
homonyms.each{|h| h.each{|e| @homo_map[e] = h - [e]; }; };
end
def random_translate(str)
str.gsub(/\w+/) do |word|
lw = word.downcase;
if(@homo_map[lw].nil? || word.size == 0)
word;
else
homonym = @homo_map[lw][rand(@homo_map[lw].size)];
if(word[0] >= 'A'[0] and word[0] <= 'Z'[0])
homonym.capitalize!
end
homonym;
end
end
end
end
# Default main program
if $0 == __FILE__
homo = Homo.new("homonyms.txt");
puts homo.random_translate(ARGF.read);
end
__END__
Then run with:
echo "I owe you." | ruby homotranslate.rb
Or just try it online now:
Random homonym translator web app
Source
ASCII list of homonyms, one set per line, space separated