Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phrase split algorithm in PHP

Tags:

php

Not sure how to explain. Let's use an example. Say I want to split the sentence

"Today is a great day."

into

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

The idea is to get all the sequential combination in a sentence.

I have been thinking what's the best way to do it in PHP. Any idea is welcome.

like image 626
Eric Sim Avatar asked May 07 '10 07:05

Eric Sim


1 Answers

Here's an example:

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

Output:

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
like image 147
Brian McKenna Avatar answered Sep 20 '22 02:09

Brian McKenna