Permalink
Browse files

added regex to prevent false positives

  • Loading branch information...
unlisted committed May 8, 2012
1 parent 5a8b4cb commit a776643a44a2112c801aa834a62b28b2b1a39a79
Showing with 18 additions and 9 deletions.
  1. +11 −5 lib/DDG/Goodie/Frequency.pm
  2. +7 −4 t/Frequency.t
@@ -6,17 +6,23 @@ use DDG::Goodie;
triggers start => 'frequency', 'freq';
handle remainder => sub {
- if ($_)
+ if ($_ =~ /^of ([a-z]|all) in (.*)/i)
{
- my %freq;
+ my $collect = lc $1;
+ my $target_str = lc $2;
my $count = 0;
- my @chars = split //, "\L$_";
+ my %freq;
+ my @chars = split //, $target_str;
+
+
foreach (@chars)
{
- if ($_ =~ /[a-z]/i)
+ if ($_ =~ /[a-z]/)
{
- ++$freq{$_};
+ if ($collect =~ /all/) { ++$freq{$_}; }
+ else { ++$freq{$_} if $_ eq $collect; }
+
++$count;
};
};
View
@@ -12,10 +12,13 @@ ddg_goodie_test(
[qw(
DDG::Goodie::Frequency
)],
- 'frequency test' => test_zci('FREQUENCY: e:1/4 s:1/4 t:2/4'),
- 'frequency testing 1234 ABC!' => test_zci('FREQUENCY: a:1/10 b:1/10 c:1/10 e:1/10 g:1/10 i:1/10 n:1/10 s:1/10 t:2/10'),
- 'frequency Assassins!' => test_zci('FREQUENCY: a:2/9 i:1/9 n:1/9 s:5/9'),
-
+ 'frequency of all in test' => test_zci('FREQUENCY: e:1/4 s:1/4 t:2/4'),
+ 'frequency of all in testing 1234 ABC!' => test_zci('FREQUENCY: a:1/10 b:1/10 c:1/10 e:1/10 g:1/10 i:1/10 n:1/10 s:1/10 t:2/10'),
+ 'frequency of all in Assassins!' => test_zci('FREQUENCY: a:2/9 i:1/9 n:1/9 s:5/9'),
+ 'frequency of a in Atlantic Ocean' => test_zci('FREQUENCY: a:3/13'),
+ 'freq of B in battle' => test_zci('FREQUENCY: b:1/6'),
+ 'freq of s in Spoons' => test_zci('FREQUENCY: s:2/6'),
+
);
done_testing;

0 comments on commit a776643

Please sign in to comment.