Generate word combinations
Asked Answered
O

5

13

For example this is my text :

$str = 'buy new microsoft windows';

I explode text and list with array :

Array
(
    [0] => buy
    [1] => new
    [2] => microsoft
    [3] => windows
)

I want to generate words in array to something like this:

buy new
buy new windows
buy microsoft 
buy microsoft windows
buy windows
new microsoft
new microsoft windows
new windows
microsoft windows

I tried with foreach and rand but I couldn't generate like showed. Is there any chance to generate just like my request?

Obeisance answered 14/6, 2011 at 11:42 Comment(10)
Are the generated words randomly created? Would windows new microsoft be valid?Schapira
Do you want all possible permutations or just any random combination?Ormazd
@Luzhin Yes they are randomly created.And its not important that be valid.Obeisance
@Ormazd All possible may be better. But if generate like my requet , thats perfect.Obeisance
@NuLLeR what about windows new windows or new new new ?Alena
@Alena No , I dont need they words.Obeisance
Maybe you could explain the logic behind the example you're giving? Because I can't really see any.Ormazd
Is the order important to you?Shapiro
@Ormazd Use this for generate tags from my title post.Obeisance
@Shapiro No its not important , but I want generate like that list.Obeisance
T
15

You can have a look at this PEAR PACKAGE Example usage:

<?php
require_once 'Math/Combinatorics.php';
$words = array('buy', 'new', 'microsoft');
$combinatorics = new Math_Combinatorics;
foreach($combinatorics->permutations($words, 2) as $p) {
  echo join(' ', $p), "\n"; 
}

The output will be:

buy new
new buy
buy microsoft
microsoft buy
new microsoft
microsoft new
Tagmemics answered 14/6, 2011 at 11:49 Comment(4)
I was making my own permutations function example, but this is better. :PDespotic
Is there any chance to generate just like my request?Obeisance
You can try your hands on this PEAR package. It offers a lots of functionsTagmemics
The answer does not match the desired output. See Paulraj's answer below.Candide
O
6
shuffle($array);
echo join(' ', array_slice($array, 0, mt_rand(1, count($array))));

This gives you one random "sentence". Repeat as necessary.

Ormazd answered 14/6, 2011 at 11:50 Comment(1)
Can you explain how can list the outpots?Obeisance
P
6

Found from php.net shuffle doc

function powerSet($in,$minLength = 1) { 
   $count = count($in); 
   $members = pow(2,$count); 
   $return = array(); 
   for ($i = 0; $i < $members; $i++) { 
      $b = sprintf("%0".$count."b",$i); 
      $out = array(); 
      for ($j = 0; $j < $count; $j++) { 
         if ($b{$j} == '1') $out[] = $in[$j]; 
      }
      $out_val = implode(" ", $out); 
      if (count($out) >= $minLength) { 
         $return[] = $out_val; 
      } 
   } 
   return $return; 
}

print_r(powerSet($str_arr));

and the results will be,

Array
(
    [0] => windows
    [1] => microsoft
    [2] => microsoft windows
    [3] => new
    [4] => new windows
    [5] => new microsoft
    [6] => new microsoft windows
    [7] => buy
    [8] => buy windows
    [9] => buy microsoft
    [10] => buy microsoft windows
    [11] => buy new
    [12] => buy new windows
    [13] => buy new microsoft
    [14] => buy new microsoft windows
)
Prepossessing answered 11/4, 2013 at 10:54 Comment(3)
This should be the answer. Such an elegant solution as well.Candide
power sets do not contain inversions, for example, "windows new" is not there.Lilylilyan
@Lilylilyan the question doesnt requires the inversion, so answered to it accordingly.Prepossessing
E
3
function pc_permute($items, $perms = array( )) {
    if (empty($items)) {
        print join(' ', $perms) . "\n";
    } else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
            $newitems = $items;
            $newperms = $perms;
            list($foo) = array_splice($newitems, $i, 1);
            array_unshift($newperms, $foo);
            pc_permute($newitems, $newperms);
        }
    }
}

source: http://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm#phpckbk-CHP-4-EX-6

also check next example on that site


example: pc_permute( explode( ' ', 'buy new microsoft windows' ) );

buy new microsoft windows
new buy microsoft windows
buy microsoft new windows
microsoft buy new windows
new microsoft buy windows
microsoft new buy windows
buy new windows microsoft
new buy windows microsoft
buy windows new microsoft
windows buy new microsoft
new windows buy microsoft
windows new buy microsoft
buy microsoft windows new
microsoft buy windows new
buy windows microsoft new
windows buy microsoft new
microsoft windows buy new
windows microsoft buy new
new microsoft windows buy
microsoft new windows buy
new windows microsoft buy
windows new microsoft buy
microsoft windows new buy
windows microsoft new buy
Each answered 14/6, 2011 at 11:55 Comment(1)
could you add the output to your answer? the comment is a bit hard to read ;)Phelloderm
P
0

For All Possible Combinations I improved @SujitAgarwal Answer :

Download Math_Combinatoeics.

include_once 'Combinatorics.php';

$words = array('buy','microsoft','windows');
$count = count($words);
$return = array();

$combinatorics = new Math_Combinatorics;

for ($num=1; $num <= $count; $num++ ){
  foreach($combinatorics->permutations($words, $num) as $p) {
    $return[] = join(' ', $p);
  }
} 

print_r($return);

Output :

Array
(
  [0] => buy
  [1] => microsoft
  [2] => windows
  [3] => buy microsoft
  [4] => microsoft buy
  [5] => buy windows
  [6] => windows buy
  [7] => microsoft windows
  [8] => windows microsoft
  [9] => buy microsoft windows
  [10] => buy windows microsoft
  [11] => microsoft windows buy
  [12] => microsoft buy windows
  [13] => windows buy microsoft
  [14] => windows microsoft buy
)
Portaltoportal answered 28/8, 2017 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.