Page 1 of 1

Help needed to summarize string

Posted: Fri Dec 17, 2010 11:52 am
by Rahul Dev

Code: Select all

<?php

  $common_words = array( " a ", " able ", " about ", "across ", " after ", " all ", " almost ", " also ", " am ", " among ", " an ", " and ", " any ", " are ", " as " , " at ", " be ", " because ", " been ", " but ", " by ", " can ", " cannot ", " could ", " dear ", " did ", " do ", " does ", " either ", " else ", " ever ", " every ", " for ", " from ", " get ", " got ", " had ", " has ", " have ", " he ", " her ", " hers ", " him ", " his ", " how ", " however ", " i ", " if ", " in ", "into ", " is ", " it ", " its ", " just ", " least ", " let ", " like ", " likely ", " may ", " me ", " might ", " most ", " must ", " my ", " neither ", " no ", " nor ", " not ", " of ", " off ", " often ", " on ", " only ", " or ", " other ", " our ", " own ", " rather ", " said ", " say ", " says ", " she ", " should ", " since ", " so ", " some ", " than ", " that ", " the ", " their ", " them ", " then ", " there ", " these ", " they ", " this ", " tis ", " to ", " too ", " us ", " wants ", " was ", " we ", " were ", " what ", " when ", " where ", " which ", " while ", " who ", " whom ", " why ", " will ", " with ", " would ", " yet ", " you ", " your ");

  $title = "Japan knife attacker injures at least 13 people";
  $title = strtolower($title);
  $title_words = array();  
  $new_title = str_replace($common_words, " ", $title);
  $title_array = explode(" ", $new_title);
  foreach($title_array as $word) 
  {
	$title_words[] = $word;
  }
  
	
 

  $text = "Japanese police have arrested a man after a knife attack outside a train station that left at least 13 people injured. The man boarded two buses and attacked passengers - who were predominantly school children - in the city of Toride, some 40km (25 miles) north-east of Tokyo, reports say. Four people were stabbed, and others were injured as they fled, police say. A 27-year-old man has been arrested on suspicion of attempted murder. One of the victims would need to remain in hospital for several weeks, while the others were not seriously injured, said a spokesman for the local fire department. Correspondents say the attack has brought back memories of an incident in central Tokyo in 2008, in which a man armed with a knife killed seven people.";

  $sentence = strtok($text, ".?!");
  $summary = '';
  while ($sentence !== false){
     foreach($title_words as $word) {
        if(strstr($sentence, $word)) {
            $summary .= $sentence . '. ';
        }
     }
     $sentence = strtok(".?!");
  }
    
  echo $summary;
 
?> 
What i'm trying to do here is remove the common words from the $title then for each word in $new_title that appears in a sentence of the $text i should add that sentence to the $summary. But if more than one word from $new_title appears in a sentence that sentence is repeated the number of times the words in the $new_title appears in the text.

how can i make that sentence appear only once?

Re: Help needed to summarize string

Posted: Fri Dec 17, 2010 12:55 pm
by mikecampbell
Use the break keyword after you append the sentence.

Code: Select all

while ($sentence !== false){
     foreach($title_words as $word) {
        if(strstr($sentence, $word)) {
            $summary .= $sentence . '. ';
            break;
        }
     }
     $sentence = strtok(".?!");
  }

Re: Help needed to summarize string

Posted: Fri Dec 17, 2010 5:24 pm
by pickle
Duplicate post. Locking.