From ab6012faa67a80c3cf6a604c52580972c120fd89 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sun, 26 Jan 2020 12:23:21 +0000 Subject: nicer types --- lib/MaildirIndexer/Index/ByAddresses.rakumod | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/MaildirIndexer/Index/ByAddresses.rakumod b/lib/MaildirIndexer/Index/ByAddresses.rakumod index d420c3e..5836cb8 100644 --- a/lib/MaildirIndexer/Index/ByAddresses.rakumod +++ b/lib/MaildirIndexer/Index/ByAddresses.rakumod @@ -9,15 +9,13 @@ use MaildirIndexer::Email; # Algorithm::NaiveBayes::Classifier::Bernoulli class has Array[Str] %!addresses-for-file; -# I'd like to type-constrain these BagHash-es, but the compiler -# currently dies if I try has BagHash $!count-by-address-and-mailbox .= new; has BagHash $!known-addresses .= new; has BagHash $!count-by-mailbox .= new; -has Hash $!p-address-given-mailbox .= new; +has Numeric %!p-address-given-mailbox; has Int $!total-count; -has Hash $!cached-p-given-mailbox .= new; +has Numeric %!cached-p-given-mailbox; has Instant $!last-cached-at .= from-posix(0); constant $NOT-ZERO = 1e-15; @@ -37,10 +35,10 @@ submethod account-for(Str @addresses,Str $mailbox,Int $step) { if ($count) { my Numeric $a = $NOT-ZERO + $count; my Numeric $b = 1 + $!count-by-mailbox{$mailbox}; - $!p-address-given-mailbox{$pair} = $a / $b; + %!p-address-given-mailbox{$pair} = $a / $b; } else { - $!p-address-given-mailbox{$pair} :delete; + %!p-address-given-mailbox{$pair} :delete; } } @@ -50,11 +48,11 @@ submethod account-for(Str @addresses,Str $mailbox,Int $step) { my Numeric $p = $!count-by-mailbox{$mailbox} / $!total-count; for $!known-addresses.keys -> Str $addr { - my $addr-p = $!p-address-given-mailbox{$addr => $mailbox} // $NOT-ZERO; + my $addr-p = %!p-address-given-mailbox{$addr => $mailbox} // $NOT-ZERO; $p *= 1 - $addr-p; } - $!cached-p-given-mailbox{$mailbox} = $p; + %!cached-p-given-mailbox{$mailbox} = $p; } $!last-cached-at = now; } @@ -93,10 +91,10 @@ submethod predict-mailbox-given-addresses(@addresses) { my Numeric %prediction; for $!count-by-mailbox.keys -> Str $mailbox { - my Numeric $p = $!cached-p-given-mailbox{$mailbox} // $NOT-ZERO; + my Numeric $p = %!cached-p-given-mailbox{$mailbox} // $NOT-ZERO; for @addresses -> Str $addr { - my $addr-p = $!p-address-given-mailbox{$addr => $mailbox} // $NOT-ZERO; + my $addr-p = %!p-address-given-mailbox{$addr => $mailbox} // $NOT-ZERO; $p *= $addr-p / ( 1- $addr-p ); } -- cgit v1.2.3