Created
June 16, 2012 06:01
-
-
Save dpk/2940135 to your computer and use it in GitHub Desktop.
num2txt: convert a number into a lot of possible 'telephone words'. pass `-w` to remove all the non-words according to /usr/share/dict/words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
NUMS = [[], [], ['a', 'b', 'c'], ['d', 'e', 'f'], | |
['g', 'h', 'i'], ['j', 'k', 'l'], ['m', 'n', 'o'], | |
['p', 'q', 'r', 's'], ['t', 'u', 'v'], ['w', 'x', 'y', 'z']] | |
def num2txt str | |
ps = [] | |
str.each_char do |char| | |
if char.match(/\d/) | |
ps << NUMS[char.to_i] | |
else | |
ps << [char] | |
end | |
end | |
words = possibilities ps, ps.length | |
if $onlyindict | |
words & $dict | |
else | |
words | |
end | |
end | |
def possibilities ps, length | |
words = [] | |
ps.each_with_index do |letters, i| | |
mps = ps[(i+1)..-1] | |
if mps.empty? | |
words.push(*letters) if (length == 1) | |
else | |
letters.each do |letter| | |
possibilities(mps, length-1).each do |mp| | |
word = "#{letter}#{mp}" | |
words << word unless (word.length < length) | |
end | |
end | |
end | |
end | |
words | |
end | |
$onlyindict = ((ARGV[0] == '-w') ? (ARGV.shift; true) : false) | |
if $onlyindict | |
$dict = File.read("/usr/share/dict/words").split("\n").map(&:downcase) | |
end | |
if ARGV.empty? | |
while gets | |
puts num2txt $_ | |
end | |
else | |
ARGV.each {|word| puts num2txt word } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fun game: when you have this and txt2num, you can search for hidden meanings in your favourite memorable 1-800 numbers by doing something like this:
Honestly: personally, I haven't found anything interesting yet. But I'll report back when it turns out that some well-known customer support line number spells ASSHOLE or BOOBIES or any of those funny calculator words. Or anything else, really.
(I wrote these to help search for Twilio numbers.)