zeroclickinfo-goodies/lib/DDG/Goodie/Frequency.pm

39 lines
560 B
Perl
Raw Normal View History

2012-05-06 08:13:52 -07:00
package DDG::Goodie::Frequency;
# ABSTRACT: Displays frequency of each character
use DDG::Goodie;
triggers start => 'frequency';
handle remainder => sub {
if ($_)
{
my %freq;
2012-05-07 10:56:19 -07:00
my $count = 0;
2012-05-07 14:32:03 -07:00
my @chars = split //, "\L$_";
foreach (@chars)
2012-05-06 08:13:52 -07:00
{
2012-05-07 10:56:19 -07:00
if ($_ =~ /[a-z]/i)
{
2012-05-07 11:05:37 -07:00
++$freq{$_};
2012-05-07 10:56:19 -07:00
++$count;
};
2012-05-06 08:13:52 -07:00
};
my @out;
foreach my $key (keys %freq)
{
2012-05-07 10:56:19 -07:00
push @out, join " ", (join ":", $key, $freq{$key}), ($freq{$key} / $count);
};
return "FREQUENCY: " . join ' ',sort(@out) if @out;
2012-05-06 08:13:52 -07:00
};
return;
};
zci is_cached => 1;
1;