homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

extracting a whole sentence
extracting a whole sentence from a given string.

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

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)

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)

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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved