katlis Posted March 19, 2009 Share Posted March 19, 2009 Hey guys, I'm trying to figure out a way to loop through an array of keywords, and find out which key has the most keyword occurrences for a string. Here's an example: $string = "the apples on the tree next to the lake are great."; $keywords = array ( "land" => array("tree","apples","dirt") "water" => array("ocean","lake","river") ); $type = ""; So I need a way to output the string $type to "land" for the example, as $string has more keywords for land than water. I appreciate the help. Link to comment https://forums.phpfreaks.com/topic/150214-searching-a-string-for-most-keyword-occurrences/ Share on other sites More sharing options...
cunoodle2 Posted March 19, 2009 Share Posted March 19, 2009 You would want to use a for loop to loop through each item in the array. From them perform a count every time a string turns up in the search. You would use this function to count the individual words... http://us2.php.net/manual/en/function.substr-count.php Link to comment https://forums.phpfreaks.com/topic/150214-searching-a-string-for-most-keyword-occurrences/#findComment-788906 Share on other sites More sharing options...
katlis Posted March 19, 2009 Author Share Posted March 19, 2009 That's sort of what I figured, but I'm not sure how to combine the loop and substr_count to return the right key. Any chance you can provide additional help on this? Link to comment https://forums.phpfreaks.com/topic/150214-searching-a-string-for-most-keyword-occurrences/#findComment-788913 Share on other sites More sharing options...
katlis Posted March 20, 2009 Author Share Posted March 20, 2009 Anyone else? I know cunoodle2 is right, just having a hard time wrapping my head around this one. Haven't messed around with multi-dimensional arrays like this. Link to comment https://forums.phpfreaks.com/topic/150214-searching-a-string-for-most-keyword-occurrences/#findComment-789070 Share on other sites More sharing options...
redarrow Posted March 20, 2009 Share Posted March 20, 2009 There you go boss. i was comparing what words had more matches, then echo the result off the most matches. but you watch a right guru cus my code lol. <?php $string="the apples on the tree next to the lake are great."; $keywords = array( "land"=> array("tree","apples","dirt"), "water"=> array("ocean","lake","river"), ); foreach($keywords as $a){ foreach($a as $b){ if(strstr($string,$b)){ $type=$keywords['water']; }else{ $type=$keywords['land']; } } } $result=implode(' ',$type); echo $result; ?> Link to comment https://forums.phpfreaks.com/topic/150214-searching-a-string-for-most-keyword-occurrences/#findComment-789197 Share on other sites More sharing options...
Mark Baker Posted March 20, 2009 Share Posted March 20, 2009 $string="the apples on the tree next to the lake are great."; $keywords = array( "land" => array("tree","apples","dirt"), "water" => array("ocean","lake","river"), ); $wordCounts = array_count_values(str_word_count($string,1)); print_r($wordCounts); echo'<hr />'; foreach($keywords as $key => $keyTopic) { echo 'KeyTopic is '.$key.'<br />'; echo 'KeyWords are '; print_r($keyTopic); echo'<br />'; echo 'Words are '; print_r(array_intersect_key($wordCounts,array_flip($keyTopic))); echo'<br />'; } Link to comment https://forums.phpfreaks.com/topic/150214-searching-a-string-for-most-keyword-occurrences/#findComment-789251 Share on other sites More sharing options...
katlis Posted March 20, 2009 Author Share Posted March 20, 2009 Thanks a bunch guys, here's how I ended up doing it (with Mark's code)... <?php $string="the apples on the tree apples next to the lake are great."; $keywords = array( "land" => array("tree","apples","dirt"), "water" => array("ocean","lake","river"), ); $wordCounts = array_count_values(str_word_count($string,1)); $results = array(); foreach($keywords as $key => $words) { $terms = array_sum(array_intersect_key($wordCounts,array_flip($words))); $results[] = array($key => $terms); } $findmax = max($results); foreach($findmax as $key => $value) { $category = $key; } echo $category; ?> Works. Best way? Link to comment https://forums.phpfreaks.com/topic/150214-searching-a-string-for-most-keyword-occurrences/#findComment-789594 Share on other sites More sharing options...
Mark Baker Posted March 20, 2009 Share Posted March 20, 2009 $category = key($findmax); instead of foreach($findmax as $key => $value) { $category = $key; } Link to comment https://forums.phpfreaks.com/topic/150214-searching-a-string-for-most-keyword-occurrences/#findComment-789630 Share on other sites More sharing options...
katlis Posted March 20, 2009 Author Share Posted March 20, 2009 Actually, this isn't working. If you change the sentence to: "the apples on the tree next to the lake (not the ocean or river) are great." it still returns land (should return water). Edit: actually anything in the string returns 'land' heh. Link to comment https://forums.phpfreaks.com/topic/150214-searching-a-string-for-most-keyword-occurrences/#findComment-789657 Share on other sites More sharing options...
Mark Baker Posted March 20, 2009 Share Posted March 20, 2009 Actually, this isn't working. If you change the sentence to: "the apples on the tree next to the lake (not the ocean or river) are great." it still returns land (should return water). problem is with max() Take a look at the manual particularly the comments by mick at wireframe dot com Link to comment https://forums.phpfreaks.com/topic/150214-searching-a-string-for-most-keyword-occurrences/#findComment-789671 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.