Jump to content

Summarize text article in PHP

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Rahul Dev

Rahul Dev

    Newbie

  • Members
  • Pip
  • 5 posts
111

Edited by Rahul Dev, 13 January 2011 - 10:13 PM.


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
You mean something like this? I modified the script to better an example:
<?php
  $title = "This is an example of a sentence in a paragraph";
  $title_array = explode(" ", $title);
  $title_words = array();
  foreach($title_array as $word) {
    if(strlen($word) > 5) {
      $title_words[] = $word;    
    }
  }
  $text = "This is a sentence. This is also a line. This is another line. This an example." ;
  $sentence = strtok($text, ".?!");
  $summary = false;
  while ($sentence !== false){
    if($summary === false) {
      foreach($title_words as $word) {
          if(strstr($sentence, $word)) {
                $summary = $sentence;
          }
      }
   }
    echo $sentence.".";
    $sentence = strtok(".?!");
  }
  
  print '<br/>Summary: ' . $summary . '.';
?>  
This will find the first occurrence of the word in $title_words and store the appropriate sentence into $summary, it will only store the first found sentence within $summary.

Examples:
$text = "This is a sentence. This is also a line. This is another line. This is an example." ;
Summary: This is a sentence.
$text = "These are some words. This is also a line. This is another line. This is an example." ;
Summary: This is an example.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Rahul Dev

Rahul Dev

    Newbie

  • Members
  • Pip
  • 5 posts
111

Edited by Rahul Dev, 13 January 2011 - 10:15 PM.


#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts

Rahul Dev said:

Thanx a lot for the help! That's what i needed. But actually i wanted the sentences to add up in $summary so that i can display it as a paragraph.
Do you mean like this?
This is a sentence. This is also a line. This is another line. This an example.
Summary: This is a sentence.  This an example.   

<?php
  $title = "This is an example of a sentence in a paragraph";
  $title_array = explode(" ", $title);
  $title_words = array();
  foreach($title_array as $word) {
    if(strlen($word) > 5) {
      $title_words[] = $word;    
    }
  }
  $text = "This is a sentence. This is also a line. This is another line. This an example." ;
  $sentence = strtok($text, ".?!");
  $summary = '';
  while ($sentence !== false){
     foreach($title_words as $word) {
        if(strstr($sentence, $word)) {
            $summary .= $sentence . '. ';
        }
     }
    echo $sentence.".";
    $sentence = strtok(".?!");
  }
  
  print '<br/>Summary: ' . $summary;
?> 

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Rahul Dev

Rahul Dev

    Newbie

  • Members
  • Pip
  • 5 posts
111

Edited by Rahul Dev, 13 January 2011 - 10:17 PM.