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

45 lines
710 B
Perl
Raw Normal View History

2012-05-06 08:13:52 -07:00
package DDG::Goodie::Frequency;
2012-05-07 18:38:06 -07:00
# ABSTRACT: Displays frequency of alphabet character (a-z)
2012-05-06 08:13:52 -07:00
use DDG::Goodie;
2012-05-07 18:38:06 -07:00
triggers start => 'frequency', 'freq';
2012-05-06 08:13:52 -07:00
handle remainder => sub {
2012-05-07 20:37:31 -07:00
if ($_ =~ /^of ([a-z]|all) in (.*)/i)
2012-05-06 08:13:52 -07:00
{
2012-05-07 20:37:31 -07:00
my $collect = lc $1;
my $target_str = lc $2;
2012-05-07 10:56:19 -07:00
my $count = 0;
2012-05-07 20:37:31 -07:00
my %freq;
my @chars = split //, $target_str;
foreach (@chars)
2012-05-06 08:13:52 -07:00
{
2012-05-07 20:37:31 -07:00
if ($_ =~ /[a-z]/)
2012-05-07 10:56:19 -07:00
{
2012-05-07 20:37:31 -07:00
if ($collect =~ /all/) { ++$freq{$_}; }
else { ++$freq{$_} if $_ eq $collect; }
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 18:38:06 -07:00
push @out, join ":", $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;