Welcome to WebmasterWorld Guest from 54.197.171.28

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

extracting a whole sentence

extracting a whole sentence from a given string.

   
4:35 pm on Jan 11, 2007 (gmt 0)

5+ Year Member



Assume that we have a long string, say a paragraph like:

"sentence1. sentence2. sentence3... This is the sentence containing keywords. sentence5. sentence6...."

I know how search and locate the keywords. My problem is to extract the complete sentence that contain my keywords. That is to say, I will search the string, find the keyword(s), and extract only the sentence with my keyword(s) in it.

Could you please help me if there is a way to do it? Even a good hint pointing to the right direction will be appreciated.

Thank you in advance.

5:02 pm on Jan 11, 2007 (gmt 0)

10+ Year Member



Well, you could try to go about it using a regular expression and preg_match. Something like /\.\s([A-Z]{1}.*?keyword.*?\.)\s/ (not tested) might work on well-formed sentences that are in the middle of a paragraph (i.e. not first and not last). For the best results you'd have work with a Natural Language Processing Toolkit though, specifically a sentence detector that's been trained on enough representative data.
7:25 pm on Jan 11, 2007 (gmt 0)

10+ Year Member



What about splitting into an array with .?

$arr = explode('.', $string);
for ($i=0; $i<count($arr); $i++)
{
if (strstr('keyword', $arr[$i])!==false)
{
// Do whatever you have to do
}
}

 

Featured Threads

Hot Threads This Week

Hot Threads This Month