package PhoneToWord; use Exporter; @ISA = qw( Exporter ); @EXPORT = qw( ); use strict; use Text::Ispell qw(spellcheck); sub new { my ($class,$num) = @_; $class = ref($class) || $class; my $self = {}; bless $self, $class; my @numbers = ([qw(0)], # No letters for 0 [qw(1)], # ditto [qw(A B C)], [qw(D E F)], [qw(G H I)], [qw(J K L)], [qw(M N O)], [qw(P Q R S)], [qw(T U V)], [qw(W X Y Z)] ); $num =~ s/[- ]//g; my @nums = split //, $num; $self->{THREE} = [$numbers[$nums[0]], $numbers[$nums[1]], $numbers[$nums[2]]]; $self->{FOUR} = [$numbers[$nums[3]], $numbers[$nums[4]], $numbers[$nums[5]], $numbers[$nums[6]]]; $self->{SEVEN} = [$self->{THREE}, $self->{FOUR}]; return $self; } sub first_three { my $self = shift; my @combos = $self->permute(@{$self->{THREE}}); my @retval = $self->get_words(\@combos, @{$self->{THREE}}); return wantarray ? @retval : \@retval; } sub last_four { my $self = shift; my @combos = $self->permute(@{$self->{FOUR}}); my @retval = $self->get_words(\@combos, @{$self->{FOUR}}); return wantarray ? @retval : \@retval; } sub seven { my $self = shift; my @combos = $self->permute(@{$self->{SEVEN}}); my @retval = $self->get_words(\@combos, @{$self->{SEVEN}}); return wantarray ? @retval : \@retval; } sub get_words { my $self = shift; my $combos = shift; my @uses = @_; my @ret; my @words = join " ", $self->show_text($combos, @uses); # spellcheck was spewing out crap I didn't want so # the eval shuts it up. eval { for my $word (spellcheck(@words)) { if ($word->{type} =~ /(?:ok|compound)/) { push @ret, $word->{term}; } } }; return @ret; } sub permute { my $self = shift; my @arrays = @_; my @lengths; for my $array_ref (@arrays) { push @lengths, scalar @$array_ref; } return $self->combine(@lengths); } sub combine { my $self = shift; my $length = shift; my @results; for (0 .. ($length - 1)) { if (@_) { foreach my $result ($self->combine(@_)) { push @results, $_ . $result; } } else { push @results, $_; } } return @results; } sub show_text { my ($self, $combos, @arrays) = @_; my @all; foreach my $combo (@$combos) { my $i = 0; my $text; for my $elem (split'', $combo) { $text .= $arrays[$i++]->[$elem]; } push @all, $text; } return @all; } 1; #### #!/usr/bin/perl -w use strict; use PhoneToWord; my $foo = new PhoneToWord qw(663-7375); my @three = $foo->first_three(); my $four = $foo->last_four(); my @all = $foo->seven(); print join "\n", @three; print "\n"; print join "\n", @$four; print "\n"; print join "\n", @all; # Output (with my dictionary, of course) MOD MOE NOD ONE PERL So, I had no seven letter words, 4 three letter ones, and one 4 letter one. So, this phone number (not mine, so don't call it :) can be ONE-PERL.