Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 57 lines (42 sloc) 1.273 kB
d7d68f3 @unlisted frequency counter plugin
unlisted authored
1 package DDG::Goodie::Frequency;
5a8b4cb @unlisted Update comments
unlisted authored
2 # ABSTRACT: Displays frequency of alphabet character (a-z)
d7d68f3 @unlisted frequency counter plugin
unlisted authored
3
56c5ea4 @mintsoft Goodies: Enabling `use strict` on all goodies
mintsoft authored
4 use strict;
d7d68f3 @unlisted frequency counter plugin
unlisted authored
5 use DDG::Goodie;
6
5a8b4cb @unlisted Update comments
unlisted authored
7 triggers start => 'frequency', 'freq';
d7d68f3 @unlisted frequency counter plugin
unlisted authored
8
812c0e4 @nospampleasemam added metadata to Frequency.pm
nospampleasemam authored
9 primary_example_queries 'frequency of all characters in testing';
10 secondary_example_queries 'frequency of B in battle', 'frequency of all in testing 1234 ABC!';
11 description 'calculate the frequency of characters in a string';
12 name 'Frequency';
13 code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Frequency.pm';
14 category 'calculations';
15 topics 'geek';
dbf47b7 @nospampleasemam various typo/category fixes
nospampleasemam authored
16 attribution github => [ 'http://github.com/unlisted', 'Morgan' ];
812c0e4 @nospampleasemam added metadata to Frequency.pm
nospampleasemam authored
17
d7d68f3 @unlisted frequency counter plugin
unlisted authored
18 handle remainder => sub {
038a3e5 Update Frequency trigger to allow non-all.
yegg authored
19 if ($_ =~ /^of ([a-z]|(?:all ?|)(?:letters|characters|chars|)) in (.+)/i)
d7d68f3 @unlisted frequency counter plugin
unlisted authored
20 {
038a3e5 Update Frequency trigger to allow non-all.
yegg authored
21
a776643 @unlisted added regex to prevent false positives
unlisted authored
22 my $collect = lc $1;
23 my $target_str = lc $2;
038a3e5 Update Frequency trigger to allow non-all.
yegg authored
24
25 # warn qq($collect\t$target_str\n);
26
a1e21f0 @unlisted added decimal frquency/letters
unlisted authored
27 my $count = 0;
a776643 @unlisted added regex to prevent false positives
unlisted authored
28 my %freq;
29 my @chars = split //, $target_str;
30
45d8bfc @unlisted fixed counter, only returns string if input contains chars a-z
unlisted authored
31 foreach (@chars)
d7d68f3 @unlisted frequency counter plugin
unlisted authored
32 {
a776643 @unlisted added regex to prevent false positives
unlisted authored
33 if ($_ =~ /[a-z]/)
a1e21f0 @unlisted added decimal frquency/letters
unlisted authored
34 {
da70710 @mwmiller EOL: remove trailing whitespace.
mwmiller authored
35 if ($collect =~ /all|letters|characters|chars/) { ++$freq{$_}; }
a776643 @unlisted added regex to prevent false positives
unlisted authored
36 else { ++$freq{$_} if $_ eq $collect; }
37
a1e21f0 @unlisted added decimal frquency/letters
unlisted authored
38 ++$count;
39 };
d7d68f3 @unlisted frequency counter plugin
unlisted authored
40 };
da70710 @mwmiller EOL: remove trailing whitespace.
mwmiller authored
41
45d8bfc @unlisted fixed counter, only returns string if input contains chars a-z
unlisted authored
42 my @out;
43 foreach my $key (keys %freq)
44 {
5a8b4cb @unlisted Update comments
unlisted authored
45 push @out, join ":", $key, $freq{$key} . "/" . $count;
45d8bfc @unlisted fixed counter, only returns string if input contains chars a-z
unlisted authored
46 };
47
fb63a8c @nospampleasemam Expanded Frequency trigger
nospampleasemam authored
48 return "Frequency: " . join ' ',sort(@out) if @out;
d7d68f3 @unlisted frequency counter plugin
unlisted authored
49 };
50
51 return;
52 };
53
54 zci is_cached => 1;
55
56 1;
Something went wrong with that request. Please try again.