Skip to content

Instantly share code, notes, and snippets.

@dpk
Created June 16, 2012 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpk/2940135 to your computer and use it in GitHub Desktop.
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
#!/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
@dpk
Copy link
Author

dpk commented Jun 16, 2012

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:

$ num2txt -w `txt2num <word>`

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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment