< Back
Page 5 of 5
Like this article? We recommend
Listing
01: package PhoneToWord; 02: use strict; 03: use Text::Ispell qw(spellcheck); 04: sub new { 05: my ($class,$num) = @_; 06: my $self = {}; 07: bless $self, $class; 08: my @numbers = ([qw(0)], # No letters for 0 09: [qw(1)], # ditto 10: [qw(A B C)], 11: [qw(D E F)], 12: [qw(G H I)], 13: [qw(J K L)], 14: [qw(M N O)], 15: [qw(P Q R S)], 16: [qw(T U V)], 17: [qw(W X Y Z)] 18: ); 19: $num =~ s/\D//g; 20: my @nums = split //, $num; 21: $self->{THREE} = [$numbers[$nums[0]], $numbers[$nums[1]], $numbers[$nums[2]]]; 22: $self->{FOUR} = [$numbers[$nums[3]], $numbers[$nums[4]], $numbers[$nums[5]], $numbers[$nums[6]]]; 23: $self->{SEVEN} = [$self->{THREE}, $self->{FOUR}]; 24: return $self; 25: } 26: sub first_three { 27: my $self = shift; 28: my @combos = $self->permute(@{$self->{THREE}}); 29: my @retval = $self->get_words(\@combos, @{$self->{THREE}}); 30: return wantarray ? @retval : \@retval; 31: } 32: sub last_four { 33: my $self = shift; 34: my @combos = $self->permute(@{$self->{FOUR}}); 35: my @retval = $self->get_words(\@combos, @{$self->{FOUR}}); 36: return wantarray ? @retval : \@retval; 37: } 38: sub seven { 39: my $self = shift; 40: my @combos = $self->permute(@{$self->{SEVEN}}); 41: my @retval = $self->get_words(\@combos, @{$self->{SEVEN}}); 42: return wantarray ? @retval : \@retval; 43: } 44: sub permute { 45: my $self = shift; 46: my @arrays = @_; 47: my @lengths; 48: for my $array_ref (@arrays) { 49: push @lengths, scalar @$array_ref; 50: } 51: return $self->combine(@lengths); 52: } 53: sub combine { 54: my $self = shift; 55: my $length = shift; 56: my @results; 57: for (0 .. ($length - 1)) { 58: if (@_) { 59: foreach my $result ($self->combine(@_)) { 60: push @results, $_ . $result; 61: } 62: } else { 63: push @results, $_; 64: } 65: } 66: return @results; 67: } 68: sub get_words { 69: my $self = shift; 70: my $combos = shift; 71: my @uses = @_; 72: my @ret; 73: my $words = join " ", $self->show_text($combos, @uses); 74: eval { 75: for my $word (spellcheck($words)) { 76: if ($word->{type} =~ /(?:ok|compound)/) { 77: push @ret, $word->{term}; 78: } 79: } 80: }; 81: return @ret; 82: } 83: sub show_text { 84: my ($self, $combos, @arrays) = @_; 85: my @all; 86: foreach my $combo (@$combos) { 87: my $i = 0; 88: my $text; 89: for my $elem (split'', $combo) { 90: $text .= $arrays[$i++]->[$elem]; 91: } 92: push @all, $text; 93: } 94: return @all; 95: } 96: 1;
Until next time, enjoy!
< Back
Page 5 of 5