Some Random Thoughts..
Just a place to put some random thoughts..

Hi,

Some days back I had come across this . I had come across the implementation of the same at some months (maybe a year) back. That time I had thought that I could do it if I wanted to. Today I did it. It was not as simple as it looked. Took almost all day to get it right. Gray matter is sleeping!
------------------

#!/usr/bin/perl
$count = 0;
if($#ARGV < 2){
print "Usage ./phonechal.pl \n";
exit;
}
$number = $ARGV[0];
$dict = $ARGV[1];
$min_num = $ARGV[2];

builddicthash($dict);
part($number,"");

sub part {
my $string = shift;
my $prefix = shift;
if(length($string) <= 1){
print_ph("$prefix $string");
$combs[$count++] = "$prefix $string";
return;
}
$prefix1 = $prefix.substr($string, 0, 1);
$string1 = substr($string, 1, length($string) - 1);
part($string1, $prefix1);
if(length($prefix) > 0){
$prefix1 = $prefix." ".substr($string, 0, 1);
$string1 = substr($string, 1, length($string) - 1);
part($string1, $prefix1);
}
$combs[$count++] = "$prefix $string";
print_ph("$prefix $string");
}

sub builddicthash {
my $dict = shift;
open(FILE, $dict);
while($line = ){
@tsplit = split("", lc($line));
chomp($line);
$entry = "";
for($i = 0; $i <= $#tsplit; $i++){
if($tsplit[$i] eq 'a' || $tsplit[$i] eq 'b' || $tsplit[$i] eq 'c'){
$entry = $entry."2";
}
if($tsplit[$i] eq 'd' || $tsplit[$i] eq 'e' || $tsplit[$i] eq 'f'){
$entry = $entry."3";
}
if($tsplit[$i] eq 'g' || $tsplit[$i] eq 'h' || $tsplit[$i] eq 'i'){
$entry = $entry."4";
}
if($tsplit[$i] eq 'j' || $tsplit[$i] eq 'k' || $tsplit[$i] eq 'l'){
$entry = $entry."5";
}
if($tsplit[$i] eq 'm' || $tsplit[$i] eq 'n' || $tsplit[$i] eq 'o'){
$entry = $entry."6";
}
if($tsplit[$i] eq 'p' || $tsplit[$i] eq 'q' || $tsplit[$i] eq 'r'
|| $tsplit[$i] eq 's') {
$entry = $entry."7";
}
if($tsplit[$i] eq 't' || $tsplit[$i] eq 'u' || $tsplit[$i] eq 'v'){
$entry = $entry."8";
}
if($tsplit[$i] eq 'w' || $tsplit[$i] eq 'x' || $tsplit[$i] eq 'y'
|| $tsplit[$i] eq 'z'){
$entry = $entry."9";
}
}
if($#{$dicthash{$entry}} < 0){
$dicthash{$entry}[0] = $line;
}else{
$dicthash{$entry}[$#{$dicthash{$entry}}+1] = $line;
}
}
close(FILE);
}

sub print_ph{
my $no = shift;
my $loop = 1;
my $i, $j, $entry, $subscript, $c, @subscripts, $final, $words=0;
@phsplit = split(" ", $no);
for($i = 0; $i <= $#phsplit; $i++){
$c = $#{$dicthash{$phsplit[$i]}};
if($c >= 0){$loop = $loop * ($c+1)};
}
for($i = 0; $i < $loop; $i++){
$final = "";
$words = 0;
$wrap = 1;
for($j = 0; $j <= $#phsplit; $j++){
$entry = $dicthash{$phsplit[$j]}[$subscripts[$j]];
if(length($entry) > 0){
$final = $final." ".$entry." ";
$words++;
} else {
$final = $final.$phsplit[$j];
}
if($#{$dicthash{$phsplit[$j]}} >= 0){
if($wrap) {
$subscripts[$j]++;
}

if($subscripts[$j] > $#{$dicthash{$phsplit[$j]}}){
$subscripts[$j] %=
($#{$dicthash{$phsplit[$j]}}+1);
$wrap = 1;
}else{
$wrap = 0;
}
}
}
# if($words >= $min_num) {
if(!$added{$final}){
print "$final\n";
$added{$final} = 1;
}
# }
}
}


Yours Sinerely,
Wake up Gray matter!
Hi,

Some days back I had come across this . I had come across the implementation of the same at some months (maybe a year) back. That time I had thought that I could do it if I wanted to. Today I did it. It was not as simple as it looked. Took almost all day to get it right. Gray matter is sleeping!
------------------

#!/usr/bin/perl
$count = 0;
if($#ARGV < 2){
print "Usage ./phonechal.pl \n";
exit;
}
$number = $ARGV[0];
$dict = $ARGV[1];
$min_num = $ARGV[2];

builddicthash($dict);
part($number,"");

sub part {
my $string = shift;
my $prefix = shift;
if(length($string) <= 1){
print_ph("$prefix $string");
$combs[$count++] = "$prefix $string";
return;
}
$prefix1 = $prefix.substr($string, 0, 1);
$string1 = substr($string, 1, length($string) - 1);
part($string1, $prefix1);
if(length($prefix) > 0){
$prefix1 = $prefix." ".substr($string, 0, 1);
$string1 = substr($string, 1, length($string) - 1);
part($string1, $prefix1);
}
$combs[$count++] = "$prefix $string";
print_ph("$prefix $string");
}

sub builddicthash {
my $dict = shift;
open(FILE, $dict);
while($line = ){
@tsplit = split("", lc($line));
chomp($line);
$entry = "";
for($i = 0; $i <= $#tsplit; $i++){
if($tsplit[$i] eq 'a' || $tsplit[$i] eq 'b' || $tsplit[$i] eq 'c'){
$entry = $entry."2";
}
if($tsplit[$i] eq 'd' || $tsplit[$i] eq 'e' || $tsplit[$i] eq 'f'){
$entry = $entry."3";
}
if($tsplit[$i] eq 'g' || $tsplit[$i] eq 'h' || $tsplit[$i] eq 'i'){
$entry = $entry."4";
}
if($tsplit[$i] eq 'j' || $tsplit[$i] eq 'k' || $tsplit[$i] eq 'l'){
$entry = $entry."5";
}
if($tsplit[$i] eq 'm' || $tsplit[$i] eq 'n' || $tsplit[$i] eq 'o'){
$entry = $entry."6";
}
if($tsplit[$i] eq 'p' || $tsplit[$i] eq 'q' || $tsplit[$i] eq 'r'
|| $tsplit[$i] eq 's') {
$entry = $entry."7";
}
if($tsplit[$i] eq 't' || $tsplit[$i] eq 'u' || $tsplit[$i] eq 'v'){
$entry = $entry."8";
}
if($tsplit[$i] eq 'w' || $tsplit[$i] eq 'x' || $tsplit[$i] eq 'y'
|| $tsplit[$i] eq 'z'){
$entry = $entry."9";
}
}
if($#{$dicthash{$entry}} < 0){
$dicthash{$entry}[0] = $line;
}else{
$dicthash{$entry}[$#{$dicthash{$entry}}+1] = $line;
}
}
close(FILE);
}

sub print_ph{
my $no = shift;
my $loop = 1;
my $i, $j, $entry, $subscript, $c, @subscripts, $final, $words=0;
@phsplit = split(" ", $no);
for($i = 0; $i <= $#phsplit; $i++){
$c = $#{$dicthash{$phsplit[$i]}};
if($c >= 0){$loop = $loop * ($c+1)};
}
for($i = 0; $i < $loop; $i++){
$final = "";
$words = 0;
$wrap = 1;
for($j = 0; $j <= $#phsplit; $j++){
$entry = $dicthash{$phsplit[$j]}[$subscripts[$j]];
if(length($entry) > 0){
$final = $final." ".$entry." ";
$words++;
} else {
$final = $final.$phsplit[$j];
}
if($#{$dicthash{$phsplit[$j]}} >= 0){
if($wrap) {
$subscripts[$j]++;
}

if($subscripts[$j] > $#{$dicthash{$phsplit[$j]}}){
$subscripts[$j] %=
($#{$dicthash{$phsplit[$j]}}+1);
$wrap = 1;
}else{
$wrap = 0;
}
}
}
# if($words >= $min_num) {
if(!$added{$final}){
print "$final\n";
$added{$final} = 1;
}
# }
}
}


Yours Sinerely,

posted by rumplestiltskin @ 4:23 pm 0 comments

0 Comments:


Post a Comment