Solved

Getting most common words from array

Posted on 2006-07-01
8
294 Views
Last Modified: 2012-06-21
I need a php script to find repeat words and output it.

Example text submitted from a textarea box:

drink energy
juice drink
hot cakes
fruit baskets
peanut butter and jelly
energy juice
fine wine
berry jelly

should return:

drink energy
juice drink
peanut butter and jelly
energy juice
berry jelly

because energy, juice, jelly and drink were most common, it should return those values from an array and also count it.


0
Comment
Question by:ray-solomon
  • 4
  • 4
8 Comments
 
LVL 29

Expert Comment

by:TeRReF
ID: 17025935
Something like this should work:
<?php

        $words = array('drink energy', 'juice drink', 'hot cakes', 'fruit baskets', 'peanut butter and jelly', 'energy juice', 'fine wine', 'berry jelly');
        $s = implode(' ', $words);
        $singlewords = array_unique(explode(' ', $s));
        //print_r($singlewords);
        //print($s);
        foreach($singlewords as $word) {
                preg_match_all('/'.$word.'/i', $s, $matches);
                $wordcount[$word] = count($matches[0]);
        }
        arsort($wordcount);
        $final_array = array();
        foreach($wordcount as $word=>$count) {
                foreach($words as $match) {
                        if (stripos($match, $word) !== false && !in_array($match, $final_array))
                                $final_array[] = $match;
                }
        }
        print_r($final_array);


?>
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17029212
Thanks TeRRef, but I get this error message:
Fatal error: Call to undefined function: stripos() in /home/...
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 17029227
CHange this line:
                      if (stripos($match, $word) !== false && !in_array($match, $final_array))
to
                      if (strpos(strtolower($match), strtolower($word)) !== false && !in_array($match, $final_array))
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 10

Author Comment

by:ray-solomon
ID: 17031869
Here is what the array contains:

Array ( [0] => drink energy [1] => juice drink [2] => peanut butter and jelly [3] => berry jelly [4] => energy juice [5] => fine wine [6] => fruit baskets [7] => hot cakes )


it should look like this:

Array ( [0] => drink energy [1] => juice drink [2] => peanut butter and jelly [3] => berry jelly [4] => energy juice )


because:
fine wine, fruit baskets and hot cakes do not contain any words that have been repeated two or more times in the array.

Hope that makes sense. BTW, thanks for helping me so far.
0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17061204
Is there a way to make it output the most common words like I showed in my original question?
0
 
LVL 29

Accepted Solution

by:
TeRReF earned 500 total points
ID: 17061358
Sure. Sorry, I overlooked your last comment.
Here you go:

<?php

        $words = array('drink energy', 'juice drink', 'hot cakes', 'fruit baskets', 'peanut butter and jelly', 'energy juice', 'fine wine', 'berry jelly');
        $s = implode(' ', $words);
        $singlewords = array_unique(explode(' ', $s));
        //print_r($singlewords);
        //print($s);
        foreach ($singlewords as $word) {
                preg_match_all('/'.$word.'/i', $s, $matches);
                if (count($matches[0]) > 1)
                        $wordcount[$word] = count($matches[0]);
        }
        arsort($wordcount);
        $final_array = array();
        foreach ($wordcount as $word=>$count) {
                foreach ($words as $match) {
                        if (strpos(strtolower($match), strtolower($word)) !== false && !in_array($match, $final_array))
                                $final_array[] = $match;
                }
        }
        print_r($final_array);


?>

0
 
LVL 10

Author Comment

by:ray-solomon
ID: 17062135
Thank you! Awsome.
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 17062314
You're welcome.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question