in reply to Phone number to word conversion

What I would do is build permutations, and test them against a dictionary
#!/usr/bin/perl -- use strict; use warnings; Main( @ARGV ? @ARGV : qw' 123 555 7672676' ); exit(0); sub Main { for my $num (@_) { my @n = MakeN($num); for my $n (@n) { print "permute: $n\n"; print join( "", @$_ ), "\n" for permute( NumberList($n) ); } print "\n"; } ## end for my $num (@_) } ## end sub Main sub MakeN { my $n = join '', @_; my @n; while ( 3 <= length $n ) { push @n, $n; chop $n; } @n = reverse @n; @n; } ## end sub MakeN sub NumberList { my %lettermap = ( 1 => [' '], 2 => [qw' a b c '], 3 => [qw' d e f '], 4 => [qw' g h i '], 5 => [qw' j k l '], 6 => [qw' m n o '], 7 => [qw' p q r s '], 8 => [qw' t u v '], 9 => [qw' w x y z '], 0 => [' '], '*' => [qw' + '], '#' => ['#'], ); return @lettermap{ split //, join '', @_ }; } ## end sub NumberList ## http://perlmonks.org/?node_id=24270# Permutations and combinations sub permute { my $last = pop @_; unless (@_) { return map [$_], @$last; } return map { my $left = $_; map [ @$left, $_ ], @$last } permute(@ +_); } ## end sub permute __END__
I would improve this code by excluding special phone numbers and numbers which can't form English words.

For example 555 or 123 (any permutation of 123) cannot comprise English words.

7672676 aka popcorn used to be a special number (at the beep, the time will be 2:30 pm, PST, beeep) and it was 767 followed by any four numbers, not just corn.

Replies are listed 'Best First'.
Re^2: Phone number to word conversion
by oko1 (Deacon) on Nov 12, 2010 at 02:14 UTC

    So it seems I was groping in the right direction, then - permutation is a reasonable approach. Awesome. Thanks so much for the help!


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
Re^2: Phone number to word conversion
by jethro (Monsignor) on Nov 12, 2010 at 00:08 UTC
    "123 can't comprise an English word" ??

    'beg' is a counter example.

      'beg' maps to '234', not '123'.
      I believe the OP is referring to the words that can be spelled out from a telephone keypad or dial (if those still exist), as used by sending SMS text messages etc, or in the more memorable telephone number/word combinations given out in commercials.

      So "beg" would be 234.

Re^2: Phone number to word conversion
by JavaFan (Canon) on Nov 12, 2010 at 00:39 UTC
    7672676 aka popcorn used to be a special number (at the beep, the time will be 2:30 pm, PST, beeep) and it was 767 followed by any four numbers, not just corn.
    According to Wikipedia, that was the exchange used for the speaking clock in Northern California. Other parts of the US used different exchanges.