Masna Posted August 18, 2007 Share Posted August 18, 2007 Hello all. Long time no see. Anyhow, here's the deal... I'm trying to write a function that will begin by splitting a sentence up into an array that contains one value per word in the sentence. It should continue by creating new sentence fragments: one sentence fragment for every combination and permutation of those words. For example, if I have a sentence "I love bees," I need the function to output the following... I love bees I love love I I bees bees I bees love love bees I love bees love bees I bees I love love I bees Basically, either I'm having a programmer's block, or this isn't simple. Any help? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/65623-dynamically-create-every-combination-of-words-possible/ Share on other sites More sharing options...
Masna Posted August 18, 2007 Author Share Posted August 18, 2007 Bump. Quote Link to comment https://forums.phpfreaks.com/topic/65623-dynamically-create-every-combination-of-words-possible/#findComment-327740 Share on other sites More sharing options...
Hypnos Posted August 18, 2007 Share Posted August 18, 2007 Is there a max length of words you'll use it for? Quote Link to comment https://forums.phpfreaks.com/topic/65623-dynamically-create-every-combination-of-words-possible/#findComment-327767 Share on other sites More sharing options...
Masna Posted August 18, 2007 Author Share Posted August 18, 2007 Is there a max length of words you'll use it for? That's my dilemma likewise. There isn't, thus, it all has to be dynamic. Quote Link to comment https://forums.phpfreaks.com/topic/65623-dynamically-create-every-combination-of-words-possible/#findComment-327775 Share on other sites More sharing options...
matthewhaworth Posted August 18, 2007 Share Posted August 18, 2007 $text = "i love bees"; // let's kick the string open $words = explode(" ", $text); // Count the amount of words $amount = count($words); $possib = $amount * $amount; foreach($words as $word) { // ahh.. this is difficult } Quote Link to comment https://forums.phpfreaks.com/topic/65623-dynamically-create-every-combination-of-words-possible/#findComment-327791 Share on other sites More sharing options...
BlueSkyIS Posted August 18, 2007 Share Posted August 18, 2007 the number of possible combinations is n!/(n-r)! where n is the number of words in the collection and r is the number of words taken from the collection. So knowing that the minimum number of words is 1 and the maximum is the total number of words in the sentence: $words = explode(" ",$sentence); for ($i=0;$i<count($words);$i++) { // Make every possible permutation using $words[$i]; } Quote Link to comment https://forums.phpfreaks.com/topic/65623-dynamically-create-every-combination-of-words-possible/#findComment-327793 Share on other sites More sharing options...
Hypnos Posted August 18, 2007 Share Posted August 18, 2007 <?php $input = "I love bees"; $input_arr = explode(' ', $input); $count = count($input_arr); //Find largest possible (Example: 4444 for 4) $max = ''; while(strlen($max) < $count -1){ $max .= $count -1; } for ($index = 0; $index <= base_convert($max, $count, 10); $index++) { $skip = FALSE; $wordnumbers = base_convert($index, 10, $count); for ($indextwo = 0; $indextwo <= $count; $indextwo++) { if(substr_count($wordnumbers, $indextwo) > 1) $skip = TRUE; } if ($skip !== TRUE) { foreach(str_split($wordnumbers) as $arr) { print $input_arr[$arr] . " "; } print "<BR>"; } } ?> I think I came close to making the universe implode, but I think it works. EDIT: It doesn't do every order like yours... It will do: I love bees love I love bees bees I bees love Quote Link to comment https://forums.phpfreaks.com/topic/65623-dynamically-create-every-combination-of-words-possible/#findComment-327798 Share on other sites More sharing options...
matthewhaworth Posted August 18, 2007 Share Posted August 18, 2007 I'm interested in mathematical/programming challenges like this. Anyone know where I could find more? Quote Link to comment https://forums.phpfreaks.com/topic/65623-dynamically-create-every-combination-of-words-possible/#findComment-327802 Share on other sites More sharing options...
Masna Posted August 19, 2007 Author Share Posted August 19, 2007 I got as far as everyone else here has, hehe. I just can't figure out how to dynamically create every combination of words in a given string. Quote Link to comment https://forums.phpfreaks.com/topic/65623-dynamically-create-every-combination-of-words-possible/#findComment-327846 Share on other sites More sharing options...
Hypnos Posted August 19, 2007 Share Posted August 19, 2007 <?php $input = "I love bees"; $input_arr = explode(' ', $input); $count = count($input_arr); //Find largest possible (Example: 4444 for 4) $max = ''; while (strlen($max) < $count) { $max .= $count; } for ($index = 0; $index < base_convert($max, $count +1, 10); $index++) { $wordnumbers = base_convert($index, 10, $count +1); if (strpos($wordnumbers, '0') === FALSE) { $skip = FALSE; for ($indextwo = 0; $indextwo <= $count +1; $indextwo++) { if(substr_count($wordnumbers, $indextwo) > 1) $skip = TRUE; } if ($skip !== TRUE) { foreach (str_split($wordnumbers) as $arr) { print $input_arr[$arr -1] . " "; } print "<BR>"; } } } ?> I love bees I love I bees love I love bees bees I bees love I love bees I bees love love I bees love bees I bees I love bees love I Is that the output you want? Quote Link to comment https://forums.phpfreaks.com/topic/65623-dynamically-create-every-combination-of-words-possible/#findComment-327859 Share on other sites More sharing options...
Masna Posted August 19, 2007 Author Share Posted August 19, 2007 Indeed Hypnos. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/65623-dynamically-create-every-combination-of-words-possible/#findComment-327860 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.