File Coverage

blib/lib/Locale/ID/GuessGender/FromFirstName.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package Locale::ID::GuessGender::FromFirstName;
2              
3 3     3   78375 use 5.010001;
  3         13  
  3         145  
4 3     3   17 use strict;
  3         13  
  3         106  
5 3     3   18 use warnings;
  3         6  
  3         118  
6 3     3   2756 use experimental 'smartmatch';
  3         2779  
  3         17  
7              
8             our $VERSION = '0.05'; # VERSION
9             our $DATE = '2014-05-26'; # DATE
10              
11             my @known_algos = qw/common v1_rules google/;
12              
13 3     3   2980 use Locale::ID::GuessGender::FromFirstName::common;
  3         13  
  3         231  
14 3     3   2169 use Locale::ID::GuessGender::FromFirstName::v1_rules;
  3         8  
  3         85  
15 3     3   1827 use Locale::ID::GuessGender::FromFirstName::google;
  0            
  0            
16              
17             our @ISA = qw(Exporter);
18             our @EXPORT_OK = qw(guess_gender);
19              
20             sub guess_gender {
21             my $opts;
22             if (@_ && ref($_[0]) eq 'HASH') {
23             $opts = shift;
24             } else {
25             $opts = {};
26             }
27             die "Please specify at least 1 name" unless @_;
28              
29             # preprocess names
30             my @names;
31             for (@_) {
32             my $name = lc $_;
33             $name =~ s/[^a-z]//g;
34             die "Invalid first name `$_`" unless $name;
35             push @names, $name;
36             }
37              
38             $opts->{try_all} //= 0;
39             $opts->{algos} //= [qw/common v1_rules/];
40             $opts->{min_guess_confidence} //= 0.51;
41             $opts->{algo_opts} //= {};
42              
43             my @res = map {
44             {
45             name => $names[$_],
46             result => undef,
47             algo => undef,
48             algo_res => [],
49             }
50             } 0..$#names;
51             my $i = 0;
52             no strict 'refs';
53             for my $algo (@{ $opts->{algos} }) {
54             die "Unknown algoritm `$algo`, use one of: ".
55             join(", ", @known_algos) unless $algo ~~ @known_algos;
56             $i++;
57             my $func = "Locale::ID::GuessGender::FromFirstName::".
58             $algo . "::guess_gender";
59             my $algo_opts = $opts->{algo_opts}{$algo} // {};
60             my @algo_res = $func->($algo_opts,
61             map { ($opts->{try_all} || !$res[$_]{result}) ?
62             $names[$_] : undef } 0..$#_);
63             for (0..$#algo_res) {
64             next unless $algo_res[$_];
65             $algo_res[$_]{algo} = $algo;
66             $algo_res[$_]{order} = $i;
67             push @{ $res[$_]{algo_res} }, $algo_res[$_];
68             if ($algo_res[$_]{success} &&
69             $algo_res[$_]{guess_confidence} >= $opts->{min_guess_confidence} &&
70             (!$res[$_]{result} || $res[$_]{guess_confidence} < $algo_res[$_]{guess_confidence})) {
71             $res[$_]{result} = $algo_res[$_]{result};
72             $res[$_]{gender_ratio} = $algo_res[$_]{gender_ratio};
73             $res[$_]{guess_confidence} = $algo_res[$_]{guess_confidence};
74             $res[$_]{algo} = $algo;
75             }
76             }
77             }
78              
79             @res;
80             }
81              
82             1;
83             # ABSTRACT: Guess gender of an Indonesian first name
84              
85             __END__