| 1 | package Acme::Affinity; | ||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:GENE'; | |||||||||
| 3 | |||||||||||
| 4 | # ABSTRACT: Compute the affinity between two people | ||||||||||
| 5 | |||||||||||
| 6 | 2 | our $VERSION = '0.0114'; | |||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use Math::BigRat (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use strictures 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | |||||||||||
| 14 | has questions => ( | ||||||||||
| 15 | is => 'ro', | ||||||||||
| 16 | 4 | ▶ | isa => sub { die 'Not an ArrayRef' unless ref($_[0]) eq 'ARRAY' }, | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||
| 17 | 1 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 2 | required => 1, | |||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | has importance => ( | ||||||||||
| 23 | is => 'ro', | ||||||||||
| 24 | 4 | ▶ | isa => sub { die 'Not a HashRef' unless ref($_[0]) eq 'HASH' }, | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||
| 25 | 1 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 26 | { | ||||||||||
| 27 | 1 | 'irrelevant' => 0, | |||||||||
| 28 | 'a little important' => 1, | ||||||||||
| 29 | 'somewhat important' => 10, | ||||||||||
| 30 | 'very important' => 50, | ||||||||||
| 31 | 'mandatory' => 250, | ||||||||||
| 32 | } | ||||||||||
| 33 | }, | ||||||||||
| 34 | 2 | required => 1, | |||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | |||||||||||
| 38 | has me => ( | ||||||||||
| 39 | is => 'ro', | ||||||||||
| 40 | 4 | ▶ | isa => sub { die 'Not an ArrayRef' unless ref($_[0]) eq 'ARRAY' }, | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||
| 41 | 1 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 2 | required => 1, | |||||||||
| 43 | ); | ||||||||||
| 44 | |||||||||||
| 45 | |||||||||||
| 46 | has you => ( | ||||||||||
| 47 | is => 'ro', | ||||||||||
| 48 | 4 | ▶ | isa => sub { die 'Not an ArrayRef' unless ref($_[0]) eq 'ARRAY' }, | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||
| 49 | 1 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 2 | required => 1, | |||||||||
| 51 | ); | ||||||||||
| 52 | |||||||||||
| 53 | |||||||||||
| 54 | 3 | ▶ | sub score { | ||||||||
|
Subroutine
score: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 55 | 3 | my $self = shift; | |||||||||
| 56 | |||||||||||
| 57 | 3 | my $me_score = _score( $self->me, $self->you, $self->importance ); | |||||||||
| 58 | 3 | my $you_score = _score( $self->you, $self->me, $self->importance ); | |||||||||
| 59 | |||||||||||
| 60 | 3 | my $m = Math::BigRat->new($me_score); | |||||||||
| 61 | 3 | my $y = Math::BigRat->new($you_score); | |||||||||
| 62 | |||||||||||
| 63 | 3 | my $question_count = Math::BigRat->new( scalar @{ $self->me } ); | |||||||||
| 64 | |||||||||||
| 65 | 3 | my $product = $m->bmul($y); | |||||||||
| 66 | |||||||||||
| 67 | 3 | my $score = $product->broot($question_count); | |||||||||
| 68 | |||||||||||
| 69 | 3 | return $score->numify * 100; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 6 | ▶ | sub _score { | ||||||||
|
Subroutine
_score: called CC=4 SCAR=13.9
| |||||||||||
| 73 | 6 | my ( $me, $you, $importance ) = @_; | |||||||||
| 74 | |||||||||||
| 75 | 6 | my $score = 0; | |||||||||
| 76 | 6 | my $total = 0; | |||||||||
| 77 | |||||||||||
| 78 | 6 | for my $i ( 0 .. @$me - 1 ) { | |||||||||
| 79 | 12 | $total += $importance->{ $me->[$i][2] }; | |||||||||
| 80 | |||||||||||
| 81 | 12 | ▶ | if ( $me->[$i][1] eq $you->[$i][0] ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 6 | $score += $importance->{ $me->[$i][2] }; | |||||||||
| 83 | } | ||||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 6 | ▶ | $score /= $total | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | if $total != 0; | ||||||||||
| 88 | |||||||||||
| 89 | 6 | return $score; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 2 | 1; | |||||||||
| 93 | |||||||||||
| 94 | __END__ | ||||||||||