File Coverage

blib/lib/Locale/ID/GuessGender/FromFirstName/google.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Locale::ID::GuessGender::FromFirstName::google;
2              
3 3     3   14 use strict;
  3         5  
  3         82  
4 3     3   15 use warnings;
  3         4  
  3         64  
5 3     3   5208 use REST::Google::Search;
  0            
  0            
6              
7             our $VERSION = '0.05'; # VERSION
8              
9             sub guess_gender {
10             my $opts;
11             if (@_ && ref($_[0]) eq 'HASH') {
12             $opts = shift;
13             } else {
14             $opts = {};
15             }
16             die "Please specify at least 1 name" unless @_;
17              
18             REST::Google::Search->http_referer(
19             "http://search.cpan.org/dist/Locale-ID-GenderGuess-FromFirstName/");
20              
21             my @res;
22             for my $name (@_) {
23             do { push @res, undef; next } unless defined($name);
24             my $res = { success => 0 };
25              
26             {
27             my $r;
28             for my $c ([num_results_bapak => qq["bapak $name"]],
29             [num_results_ibu => qq["ibu $name"]]) {
30             $r = REST::Google::Search->new(q=>$c->[1]);
31             if (!$r) {
32             $res->{error} = "REST::Google::Search returns".
33             " nothing (q=$c->[1])";
34             last;
35             }
36             if ($r->{responseStatus} != 200) {
37             $res->{error} = "REST::Google::Search returns".
38             " HTTP error $r->{responseStatus} (q=$c->[1])";
39             last;
40             }
41             if (!$r->{responseData}) {
42             $res->{error} = "REST::Google::Search does not".
43             " return responseData (q=$c->[1])";
44             last;
45             }
46             $res->{$c->[0]} = $r->{responseData}{cursor}->
47             {estimatedResultCount} // 0;
48             }
49             last if $res->{error};
50             }
51              
52             if (!$res->{error}) {
53             $res->{success} = 1;
54             my ($b, $i) = ($res->{num_results_bapak}, $res->{num_results_ibu});
55             my $tot = $b+$i;
56             my $ratio = (!$b && !$i) ? 0 :
57             ($b > $i ? $b/($b+$i) : $i/($b+$i));
58             my $min_ratio = $tot < 10 ? 2/(1+2) :
59             ($tot < 100 ? 2/(1+2) : $tot < 1000 ? 2/(1+2) : 2/(1+2));
60             $res->{min_gender_ratio} = $min_ratio;
61             $res->{gender_ratio} = $ratio;
62             if (!$tot) {
63             $res->{result} = undef;
64             $res->{guess_confidence} = 0;
65             } else {
66             $res->{result} =
67             $ratio < $min_ratio ? "both" : ($b > $i ? "M" : "F");
68             $res->{guess_confidence} = $tot < 10 ? 0.75 : 0.9;
69             }
70             }
71             push @res, $res;
72             }
73             @res;
74             }
75              
76             1;
77             # ABSTRACT: google
78              
79             __END__