Solved

Getting most common words from array

Posted on 2006-07-01
8
296 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 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