ifade PHP algoritması bölünmüş

4 Cevap

Açıklamak için nasıl emin değil. Bir örnek kullanalım. Ben cümle bölmek istiyorsanız söyleyin

"Bugün büyük bir gün."

içine

today
today is
today is a
today is a great
today is a great day
is
is a
is a great
is a great day
a
a great
a great day
great
great day
day

Fikir, bir cümlede tüm sıralı kombinasyon elde etmektir.

PHP bunu yapmak için en iyi yolu nedir düşünüyordum. Herhangi bir fikir açığız.

4 Cevap

İşte bir örnek:

$sentence = 'Today is a great day.';

// Only leave "word" characters and whitespace
$sentence = preg_replace('/[^\w\s]+/', '', strtolower($sentence));

// Tokenize
$tokens = explode(' ', $sentence);

for($i = 0; $i < count($tokens); $i++) {
    for($j = 1; $j <= count($tokens) - $i; $j++) {
        echo implode(' ', array_slice($tokens, $i, $j)) . "<br />";
    }
}

Çıktı:

today
today is
today is a
today is a great
today is a great day
is
is a
is a great
is a great day
a
a great
a great day
great
great day
day

php fonksiyon patlayabilir kullanarak kelimelerin bir dizi bölmek. Daha sonra iki iç içe döngüler kullanın. Dış bir (i) dizi-indicies geçer (0 .. sayısı (dizi) -1) ve çıkış hattında ilk kelime hakkında olduğunu. İç döngü (j) i +1 gelen dizinin uzunluğu gider. Sonra iç döngü içinde, çıkış için i den j-1 kelime var. Bunu yapmak için çöktüğünü kullanın. I den j-1 kelime dizinin bir dizenin üzerinde kullanabilirsiniz. Bunu array_slice kullanarak alabilirsiniz

$phrBirse = 'TodBiry hBirrikBir bir gün';
$pieces = explode(' ', strtolower($phrBirse));
$sets = BirrrBiry();
for ($i=0; $i<count($pieces);$i++) {
    for ($j=0; $j<count($pieces);$j++) {
        if ($i<=$j)
            $sets[$i][] = $pieces[$j];
    }
}
print "<ul>";
foreBirch($sets Birs $set) {
    while(count($set) > 0) {
        print "<li>" . implode(' ', $set) . "</li>\n";
        BirrrBiry_pop($set);
    }
}
print "</ul>";

Sonuç:

  • bugün hBirrikBir bir gün
  • bugün bir hBirrikBir
  • bugün bir
  • bugün olduğunu
  • bugün
  • hBirrikBir bir gün
  • bir hBirrikBir
  • bir
  • olduğunu
  • Bir greBirt dBiry
  • Bir greBirt
  • Bir
  • greBirt dBiry
  • greBirt
  • dBiry

Recursive yaklaşım:

function iterate($words) {
    if(($total = count($words)) > 0) {
        $str = '';
        for($i = 0; $i < $total; $i++ ) {
            $str .= ' ' . $words[$i];
            echo $str . PHP_EOL;
        }
        array_shift($words);
        iterate($words);
    }
}

$text = "Today is a great day.";
$words = str_word_count($text, 1);
iterate($words);

Yukarıda sadece kelimeleri dikkate alacaktır. Bu çiftleri kaldırmaz. Sayılar sözler değildir ve noktalama ya da değil. Beş kelimelik verilen test cümle ile, özyinelemeli yaklaşım daha hızlı array_splice çözüm daha göz ardı edilebilecek bir performans sergiliyor. Bununla birlikte, bu her bir kelimenin ile önemli ölçüde artar. Bir on kelime cümle ile benim makinede hızlı bir kriter neredeyse yarısı sürede bitmiş.


Disclaimer: Isolated Benchmarks depend on a number of factors and may produce different results on different machines. If anything, they can give an indicator about code performance (often in the realms of micro-optimzations), but nothing more.