Solved

Getting most common words from array

Posted on 2006-07-01
8
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

696 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