Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a class assignment that I am working on and not understanding completely. Just would like some help in the right direction.

Directons:
Parameter: A string containing words that are delimited on the left with spaces and on the right with spaces, commas, periods, or question marks
Return value: The three most common words in the string that have more than


PHP
<?php
          function splitter($str) {
                $freq = array();

                $words = preg_split("/[\.,;:!\/]\s*/", $str);

                foreach ($words as $word) {
                    $keys = array_keys($freq);
                    if(in_array($word, $keys))
                    $freq[$word]++;
                    else
                    $freq[$word] = 1;
                }
                return $freq;
            }

            $str = "I like to run or jog for exercise. Then, I like to drink plenty of fluids.";

            $result = splitter($str);
            sort($result);
            foreach ($result as $word) {
                $len = strlen($word);
                if ($len > 3)
                print $word;
                else
                return false;
            }

        ?>



This code returns nothing. I have tried a few variations and just lost at this point.
Posted
Updated 25-Jul-13 10:37am
v2
Comments
Sergey Alexandrovich Kryukov 25-Jul-13 15:57pm    
Don't you think you just need to pay a bit more effort? Do you have an opportunity to run the code under the debugger? Do you have any particular concern?
—SA
Member 12070401 19-Oct-15 10:34am    
Can you please post final program for this question??

1 solution

I would advise to use Regular expressions instead: http://webcheatsheet.com/php/regular_expressions.php[^].

Find all matches each representing a word, and rewrite all the words in a frequency dictionary, so each word would be represented only once, and the value represents the frequency. The keys should be the strings, each representing a word. In PHP, this is easily done with array, because a PHP array is actually an associative container. You should index array with your words and elements should represent frequencies:
http://php.net/manual/en/language.types.array.php[^].

Now, you scan all Regex matches, get all the words one by one and add to array. If the key does not yet exist, you just add new word with the frequency value of 1, if it does exist, you access existing array element and increment the frequency. At the end, you will have the array with all the words and their frequencies. I hope the search for top 3 you can design by yourself.

—SA
 
Share this answer
 
v3
Comments
tristarterror 25-Jul-13 18:47pm    
Thank you so much. I can figure out the search for top 3 but I have been stuck on this one exercise for days and have rewrote it a ton of times. I just needed to logical explanation of how to get the keys and values. Appreciate the help!
Sergey Alexandrovich Kryukov 25-Jul-13 18:50pm    
Very good. You are welcome.
Good luck, call again.
—SA
Member 12070401 19-Oct-15 10:35am    
Can you please post final program for this question??
Sergey Alexandrovich Kryukov 19-Oct-15 10:41am    
Sorry, I don't have time for this. If you need further help, I would advise to do the job yourself and ask further questions if you face some problems.
—SA
Espen Harlinn 25-Jul-13 19:12pm    
;-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900