Jump to content

Searching a string for most keyword occurrences


katlis

Recommended Posts

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.

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

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;
             		
        ?>

$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 />';
}

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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.