生成单词组合

发布于 2024-11-15 11:42:53 字数 467 浏览 2 评论 0原文

例如,这是我的文本:

$str = 'buy new microsoft windows';

我用数组​​分解文本和列表:

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

我想在数组中生成类似这样的单词:

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

我尝试使用 foreach 和 rand 但无法生成如图所示的内容。有没有机会按照我的要求生成?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

追星践月 2024-11-22 11:42:53

你可以看看这个 PEAR 包
用法示例:

<?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"; 
}

输出将是:

buy new
new buy
buy microsoft
microsoft buy
new microsoft
microsoft new

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
一个人练习一个人 2024-11-22 11:42:53
shuffle($array);
echo join(' ', array_slice($array, 0, mt_rand(1, count($array))));

这会给你一个随机“句子”。根据需要重复。

shuffle($array);
echo join(' ', array_slice($array, 0, mt_rand(1, count($array))));

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

笑饮青盏花 2024-11-22 11:42:53

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

,结果将是,

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
)

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
)
只是我以为 2024-11-22 11:42:53
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);
        }
    }
}

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

还检查​​该站点


示例上的下一个示例: pc_permute(explode('','购买新的微软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
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
空城之時有危險 2024-11-22 11:42:53

对于所有可能的组合,我改进了 @SujitAgarwal 答案:

下载 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);

输出 :

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
)

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
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文