Summarize Paragraph in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rahul Dev
Forum Newbie
Posts: 18
Joined: Thu Dec 09, 2010 4:54 am

Summarize Paragraph in PHP

Post by Rahul Dev »

First i need to store the Title of the article in a variable then i would check whether sentences in the paragraph contains the words(The word should be greater than 5 characters) in the title. If the sentences contain the words in the title, then i need to store these sentences in another variable $summary which will make a summary of the original paragraph. This is what i've able to do so far:

<?php
$title = "This is an example of a sentence in a paragraph";
$title_array = explode(" ", $title);
$comonword = array();
foreach ($title_array as $word) {
$chr_count = strlen($word);
if ($chr_count > 5)
echo "$word<br />";
}

$text = "This is a sentence. This is also a line. This is another line. This an example." ;
$sentence = strtok($text, ".?!");
while ($sentence !== false){
echo $sentence.".";
$sentence = strtok(".?!");
}

?>

In the above code the words having greater than 5 characters in $title is split and stored in $word and the sentences are stored in $sentence.
What i want to do is check if the sentences contains the words stored if $word then if the sentences contain any of those words, add that sentence to another variable($summary) to produce the summary. Please any1 can help me?
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Summarize Paragraph in PHP

Post by greyhoundcode »

Can we start from the beginning again (I'm pretty thick).

Title:
Catching fish is easy with the right hook

Content:
Catching fish is easy with the right hook, as having the wrong hook is quite simply problematic. This article discusses the pros and cons of various hooks. Quo voluptas nulla pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus, qui blanditiis praesentium voluptatum.

This is yet another paragraph, and there are probably lots more in the article. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores.


So, in the above example, the first five words in the first paragraph match the first five words in the title. Am I correct in thinking that you then wish the first paragraph to be extracted as a summary?

(I'm just trying to be clear about what you want to achieve)
Rahul Dev
Forum Newbie
Posts: 18
Joined: Thu Dec 09, 2010 4:54 am

Re: Summarize Paragraph in PHP

Post by Rahul Dev »

greyhoundcode wrote:Can we start from the beginning again (I'm pretty thick).

Title:
Catching fish is easy with the right hook

Content:
Catching fish is easy with the right hook, as having the wrong hook is quite simply problematic. This article discusses the pros and cons of various hooks. Quo voluptas nulla pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus, qui blanditiis praesentium voluptatum.

This is yet another paragraph, and there are probably lots more in the article. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores.


So, in the above example, the first five words in the first paragraph match the first five words in the title. Am I correct in thinking that you then wish the first paragraph to be extracted as a summary?

(I'm just trying to be clear about what you want to achieve)
What i want to do is that if any sentence in the paragraph contains any of the words in the title then i should extract that sentence and store it in the $summary variable. In the above example $summary would contain the following:
"This is a sentence. This is an example." since the word sentence and example are found in the title and are greater than 5 characters.
jaceinla
Forum Commoner
Posts: 25
Joined: Thu Oct 14, 2010 12:57 pm

Re: Summarize Paragraph in PHP

Post by jaceinla »

You could try getting the entire paragraph, preg_split it up (because of . ! ?) and then with each sentence now in an array, compare the words in your title to each word in your sentence using your foreach method above or a double loop with a multi-dimensional array.
Post Reply