RE: How do I do count the occurrence of each word?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> -----Original Message-----
> From: Marco Behnke [mailto:marco@xxxxxxxxxx]
> Sent: 19 August 2012 06:39
> To: php-general@xxxxxxxxxxxxx
> Subject: Re:  How do I do count the occurrence of each word?
> 
> Am 19.08.12 06:59, schrieb tamouse mailing lists:
> > On Sat, Aug 18, 2012 at 6:44 PM, John Taylor-Johnston
> > <jt.johnston@xxxxxxxxxxxxxx> wrote:
> >> I want to parse this text and count the occurrence of each word:
> >>
> >> Sample Output:
> >>
> >> determined = 4
> >> fire = 7
> >> patrol = 3
> >> theft = 6
> >> witness = 1
> >> witnessed = 1
> >>

[...]

> > and then you just run through the words building an associative
> array
> > by incrementing the count of each word as the key to the array:
> >
> > foreach ($words as $word) {
> >     $freq[$word]++;
> > }
> 
> Please an existence check to avoid incrementing not set array keys
> 
> foreach ($words as $word) {
>   if (array_key_exists($word, $freq)) {
>     $freq[$word] = 1;
>   } else {
>     $freq[$word]++;
>   }
> }

Erm...

   $freq = array_count_values($words)

(http://php.net/array_count_values)


Cheers!

Mike

-- 
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,  
Portland PD507, City Campus, Leeds Metropolitan University,
Portland Way, LEEDS,  LS1 3HE,  United Kingdom 
E: m.ford@xxxxxxxxxxxxxx     T: +44 113 812 4730





To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux