Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    finding the most popular string out of a list of strings

    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!

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,746
    Thanks
    18
    Thanked 155 Times in 146 Posts
    Quote Originally Posted by lli2k5
    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...
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    New Coder
    Join Date
    Aug 2006
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Aug 2006
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually, I would just like to find the most popular word in a set of all strings.

    How could I do that?

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Well that's easy. Just create an associative array using the words as index and the count as value, then sort the array.

    PHP Code:
    $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.

  • #6
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,746
    Thanks
    18
    Thanked 155 Times in 146 Posts
    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:

    PHP Code:
    <?

    // 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...pular_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.
    Last edited by chump2877; 08-30-2006 at 06:35 PM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #7
    New Coder
    Join Date
    Aug 2006
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks guys. Exactly what I was looking for!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •