Jump to content

Dynamically Create Every Combination of Words Possible


Recommended Posts

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.

 

 

$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

}

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];

 

}

<?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

<?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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.