不确定如何解释。让我们举个例子。说我想分句

“今天是个好日子。”

进入

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做到最好的方法是什么。任何想法都欢迎。

最佳答案

这是一个例子:

$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 />";
    }
}

输出:
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 - PHP中的词组拆分算法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2787038/

10-12 02:20