...

View Full Version : finding the most popular string out of a list of strings



lli2k5
08-30-2006, 09:06 AM
Hi,

I have a list of strings and I would like to create a php program that finds the one that is the most popular. I expect that the one that is most popular is the one that has the most strings sharing the same unique words.

Would running this require a lot of processing power? There are about 200 strings with approx 15 words each.

Also, any help on coding this. Does something like this exist already? Greatly appreciated!

chump2877
08-30-2006, 12:20 PM
Hi,

I have a list of strings and I would like to create a php program that finds the one that is the most popular. I expect that the one that is most popular is the one that has the most strings sharing the same unique words.

I don;t quite understand what you want to do...something like this?:

string 1: dogcatdeermouse
string 2: dogcatdeer
string 3: dogcat
string 4: cat

where string 1 is the most popular?

If you could give us an example of how you plan to apply this script, that would help a lot...

lli2k5
08-30-2006, 05:43 PM
Like this:

I ate at mcdonalds yesterday for $5.00
Starbucks has great coffee
Burger King was where I ate yesterday
I spent $5.00 on a meal yesterday at mcdonalds

The most popular string is I ate at mcdonalds yesterday for $5.00 because it appeared twice in the list.

I plan to do it by highlighting unique keywords excluding case.

lli2k5
08-30-2006, 05:46 PM
Actually, I would just like to find the most popular word in a set of all strings.

How could I do that?

Fumigator
08-30-2006, 06:25 PM
Well that's easy. Just create an associative array using the words as index and the count as value, then sort the array.



$theString = array( "I ate at mcdonalds yesterday for $5.00",
"Starbucks has great coffee",
"Burger King was where I ate yesterday",
"I spent $5.00 on a meal yesterday at mcdonalds");

foreach($theString as $strVal) {
$words = explode(" ", $strVal);
foreach ($words as $word) {
if (isset($tallyArray[$word])) {
$tallyArray[$word]++;
} else {
$tallyArray[$word] = 1;
}
}
}

arsort($tallyArray);

print "<pre>\n";
print_r($tallyArray);
print "</pre>\n";


To use a set of keywords like you were talking about previously, you can use the same technique, just use a concatenated index with the keywords like $tallyArray['meal,yesterday,mcdonalds']. You'd of course need to sort the keywords so they appear as the same array every time.

chump2877
08-30-2006, 06:59 PM
Well I see fumigator beat me to it, but this is what I came up for you quickly, to put all of the words and their frequency of use into a HTML table for you, ordered by frequency descending:


<?

// file find_popular_word.php

$string[0] = "I ate at mcdonalds yesterday for $5.00";
$string[1] = "Starbucks has great coffee";
$string[2] = "Burger King was where I ate yesterday";
$string[3] = "I spent $5.00 on a meal yesterday at mcdonalds";

foreach ($string as $val)
{
$val_array = explode(" ",$val);
foreach ($val_array as $value)
{
$string_array[] = $value;
}
}

$old_val_array = array();
foreach ($string_array as $key => $val)
{
if (!in_array($val,$old_val_array))
{
$num = count(array_keys($string_array,$val));
$results[$val] = $num;
$old_val_array[] = $val;
}
}

arsort($results);

print "<table border='1' cellpadding='5'>";
print "<tr><th>Word</th><th>Frequency</th></tr>";

foreach ($results as $key => $val)
{
print "<tr>";
print "<td>".$key."</td>";
print "<td>".$val."</td>";
print "</tr>";
}

print "</table>";

?>

Out put of this is here: http://www.mediamogulsweb.com/client/find_popular_word.php

Fumigators way looks shorter, but I dont have time to check it out right now...

Edit: Got rid of the multi-dimensional array, was not needed...as fumigator said, a regular array will suffice.

lli2k5
08-30-2006, 07:38 PM
thanks guys. Exactly what I was looking for!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum