joywealth Posted July 27, 2011 Share Posted July 27, 2011 Hello Everyone, I seek for help on this project I am working on 1. The project requires me to explode a string (Usually a large text file) into individual sentences. 2. Pick/Select from the array of sentences only the sentences that have at least a specified number of words. And discard the rest of the array as not useful 3. Search my database for a string where any or as many of the sentences are contained. 4. And then display the result... //Finished getting string info //Begin the checking process $str = trim($row_getPublication['postcontent']); // Initialize string from the Body of the Gotten Publication array_map('trim',explode(".",$str)); // Explode String $publication_split = array_map('trim',explode(".",$str)); // assign exploded string to variable if (isset($row_getPublication['account_id'])) { $colname2_checkPublication = $row_getPublication['account_id']; } foreach ($publication_split as $each){ mysql_select_db($database_conNote, $conNote); $query_checkPublication = sprintf("SELECT * FROM publications WHERE postcontent LIKE %s AND account_id != %s", GetSQLValueString("%" . $each . "%", "text"),GetSQLValueString($colname2_checkPublication, "text")); } $checkPublication = mysql_query($query_checkPublication, $conNote) or die(mysql_error()); $row_checkPublication = mysql_fetch_assoc($checkPublication); $totalRows_checkPublication = mysql_num_rows($checkPublication); Quote Link to comment Share on other sites More sharing options...
Nodral Posted July 27, 2011 Share Posted July 27, 2011 And which bit do you need help with, or do you require someone to do all your homework for you? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 27, 2011 Share Posted July 27, 2011 I'll help with half: 1. The project requires me to explode a string (Usually a large text file) into individual sentences. Assuming the sentences are seperated by a line break (\n) in the text file: $file = file_get_contents('filename.txt'); $sentences = explolde("\n",$file); 2. Pick/Select from the array of sentences only the sentences that have at least a specified number of words. And discard the rest of the array as not useful numberofwords = 5; // pick sentences with 5 words foreach($sentences as $k=>$sentence){ $words = explode(" ",$sentence); if(sizeof($word) != $numberofwords) unset($sentences[$k]); } Quote Link to comment Share on other sites More sharing options...
joywealth Posted July 27, 2011 Author Share Posted July 27, 2011 It wasn't an homework sir. I am actually developing a application and I am just having a problem with that part. The code I posted was the part giving me problem. And Please... The database is not a text file... I called it as well from database as a string. $row[] Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 27, 2011 Share Posted July 27, 2011 One problem that comes to mind is this: What delimits the sentences, and how can you be sure it is only used to delimit sentences? Quote Link to comment Share on other sites More sharing options...
joywealth Posted July 27, 2011 Author Share Posted July 27, 2011 Delimeter could be a period(.) or <p> or </p> because it is an html text string. And Is this query alright with $publication_split% as the final array to work with? $checkNow = "SELECT * FROM `publications` WHERE `postcontent` LIKE \'%$publication_split%\' LIMIT 0, 30 "; Quote Link to comment Share on other sites More sharing options...
joywealth Posted July 27, 2011 Author Share Posted July 27, 2011 I tried this as well but it didn't work. The result was: Notice: Undefined variable: QcheckNow in C:\xampp\htdocs\writecheck.php on line 128 Query was empty $arr_1 = array_map('trim',explode(".",$postcontent)); // assign exploded string to variable array. $c=0; $arr_2=array(); foreach ($arr_1 as $words) { $temp_array=explode(" ",$words); //say am only interested to sentences with less than 3 words if(count($temp_array)<4){ $arr_2[$c]=$words; $c=$c+1; } } $i=0; $QcheckNow = "SELECT * FROM `publications` WHERE `postcontent`"; foreach($arr_2 as $final){ if ($i != 0) { $QcheckNow .= " LIKE "; } $QcheckNow .= "\'%$final%\' LIMIT 0, 30 "; $i++; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.