A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearHow to combine words of a sentence to composed terms? - page 1
User InfoPosts
How to combine words of a sentence to composed terms?#1
I have a sentence, for example


John Doe moved to New York last year.


Now I split the sentence into the single words and I get:


array((John(, (Doe(, (moved(, (to(, (New(, (York(, (last(, (year()


That(s quite easy. But then I want to combine the single words to get all the composed terms. It doesn(t if the composed terms make sense, I want to get all of them, though. The result of that operation should look like this:


John, Doe, John Doe, moved, Doe moved, John Doe moved, to, moved to, Doe moved to ...


The words should be composed to terms to a limit of k parts. In the example above, the limit is 3. So a term can contain 3 words at most.

The problem: How could I code the composition in PHP? It would be great if I had a function which gets a sentence as the input and gives an array with all terms as the output.

I hope you can help me. Thanks in advance!

posted date: 2009-04-12 16:15:00


Re: How to combine words of a sentence to composed terms?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-12 16:15:01


Re: How to combine words of a sentence to composed terms?#3
Every composition will be defined by a starting point and a length - just loop through.PHP won(t help you all the way, but it does have some handy functions.$words = explode(" ", $sentence);for ($start = 0; $start < count($words); $start++) //starting point{ //try all possible lengths //limit = max length //and of course it can(t overflow the string for ($len = 1; $len <= $limit && $len <= count($words)-$start; $len++) { //array_slice gets a chunk of the array, and implode joins it w/ spaces $compositions[] = implode(" ", array_slice($words, $start, $len)); }}

posted date: 2009-04-12 16:26:00


Re: How to combine words of a sentence to composed terms?#4
If you already have the code for splitting the words into an array, this function will let you select the longest you wish your phrases to be, and return to you an array of arrays containing your phrases.function getPhrases($array, $maxTerms = 3) { for($i=0; $i < $maxTerms; $i++) { //Until we(ve generated terms of all lengths for($j = 0; $j < (sizeof($array) - $i); $j++) { //Until we(ve iterated as far through the array as we should go $termArray[] = array(array_slice($array, $j, ($i+1))); //Add this part of the array to the array } } return $termArray;}//Usage example$newarray = explode(" ", "This is a pretty long example sentence");print_r(getPhrases($newarray));

posted date: 2009-04-12 16:41:00


Re: How to combine words of a sentence to composed terms?#5
Thank you very much! A functions which gives an array with the terms as the output. The terms are even ordered by the number of parts (first 1 word, then 2 words, ...). Perfect!

posted date: 2009-04-13 01:41:00


Re: How to combine words of a sentence to composed terms?#6
$t = count($array); for($i=0; $i < $maxTerms; $i++) { for($j = 0, $i1 = $i + 1, $ti = $t - $i; $j < $ti; $j++) { $termArray[] = array(array_slice($array, $j, $i1)); } }

posted date: 2009-04-13 08:47:00


Re: How to combine words of a sentence to composed terms?#7
Sorry, maybe it's a stupid question, but what does your code do, OIS?

posted date: 2009-04-14 10:15:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.