From 6bc735051d38c1f84acaa40d8e724398e50dcc90 Mon Sep 17 00:00:00 2001 From: dakkar Date: Mon, 16 Mar 2020 15:33:39 +0000 Subject: indices can now return confidence levels also, tests pass again --- lib/MaildirIndexer/Index.rakumod | 7 ++++++- lib/MaildirIndexer/Index/ByAddresses.rakumod | 6 +++--- lib/MaildirIndexer/Index/ByRef.rakumod | 6 +++--- lib/MaildirIndexer/Store.rakumod | 3 ++- t/lib/TestIndex.rakumod | 4 ++-- t/store.t | 24 ++++++++++++++++++++---- 6 files changed, 36 insertions(+), 14 deletions(-) diff --git a/lib/MaildirIndexer/Index.rakumod b/lib/MaildirIndexer/Index.rakumod index 8d7ad30..90bc6bd 100644 --- a/lib/MaildirIndexer/Index.rakumod +++ b/lib/MaildirIndexer/Index.rakumod @@ -15,6 +15,11 @@ my class MailboxForEmail is export { has Channel:D $.reply-to is required; } +my class Mailbox is export { + has Str:D $.name is required; + has Numeric:D $.confidence is required; +} + method receive(Channel:D $channel --> Nil) { react { whenever $channel -> $event { @@ -33,4 +38,4 @@ method receive(Channel:D $channel --> Nil) { method add-mail(MaildirIndexer::Email:D $email, Str:D $mailbox --> Nil) { ... } method del-path(IO:D $path, Str:D $mailbox --> Nil) { ... } -method mailbox-for-email(MaildirIndexer::Email:D $email --> Str) { ... } +method mailbox-for-email(MaildirIndexer::Email:D $email --> Mailbox) { ... } diff --git a/lib/MaildirIndexer/Index/ByAddresses.rakumod b/lib/MaildirIndexer/Index/ByAddresses.rakumod index 0b77d15..033f4e9 100644 --- a/lib/MaildirIndexer/Index/ByAddresses.rakumod +++ b/lib/MaildirIndexer/Index/ByAddresses.rakumod @@ -110,14 +110,14 @@ submethod predict-mailbox-given-addresses(@addresses) { return %prediction; } -method mailbox-for-email(MaildirIndexer::Email:D $email --> Str) { - my Str $result; +method mailbox-for-email(MaildirIndexer::Email:D $email --> Mailbox) { + my Mailbox $result; MaildirIndexer::LogTimelineSchema::Index::Find.log: :class('ByAddresses'), -> { my %prediction = self.predict-mailbox-given-addresses($email.addresses); my @most-probable-mailboxes = %prediction.pairs.sort(*.value); - if @most-probable-mailboxes -> $_ { $result = .[*-1].key } + if @most-probable-mailboxes -> $_ { $result = Mailbox.new(:name(.key),:confidence(.value)) with .[*-1] } } return $result; } diff --git a/lib/MaildirIndexer/Index/ByRef.rakumod b/lib/MaildirIndexer/Index/ByRef.rakumod index 6e5b7f5..30ea58c 100644 --- a/lib/MaildirIndexer/Index/ByRef.rakumod +++ b/lib/MaildirIndexer/Index/ByRef.rakumod @@ -35,11 +35,11 @@ method del-path(IO:D $file, Str:D $mailbox --> Nil) { } } -method mailbox-for-email(MaildirIndexer::Email:D $email --> Str) { - my Str $result; +method mailbox-for-email(MaildirIndexer::Email:D $email --> Mailbox) { + my Mailbox $result; MaildirIndexer::LogTimelineSchema::Index::Find.log: :class('ByRef'), -> { for |$email.refs() -> $ref { - with %!mailboxes-for-id{$ref} { $result = .keys.sort.[0] } + with %!mailboxes-for-id{$ref} { $result = Mailbox.new(:name(.keys.sort.[0]),:1confidence) } } } return $result; diff --git a/lib/MaildirIndexer/Store.rakumod b/lib/MaildirIndexer/Store.rakumod index 856baca..51201f2 100644 --- a/lib/MaildirIndexer/Store.rakumod +++ b/lib/MaildirIndexer/Store.rakumod @@ -69,7 +69,8 @@ method mailbox-for-email(MaildirIndexer::Email:D $email --> Str) { MaildirIndexer::LogTimelineSchema::Store::Find.log: { .send($event) for @!index-channels; my @results = $replies.receive() xx @!index-channels; - $result = @results.grep(*.defined).join("\n"); + @results = @results.grep(*.defined).sort(*.confidence); + if @results -> $_ { $result = .[*-1].name } } return $result; } diff --git a/t/lib/TestIndex.rakumod b/t/lib/TestIndex.rakumod index c0b0b03..13043ac 100644 --- a/t/lib/TestIndex.rakumod +++ b/t/lib/TestIndex.rakumod @@ -5,7 +5,7 @@ unit class TestIndex does MaildirIndexer::Index; has %.mails; has $.name = 'test index'; -has @.responses = ( 'foo' xx 10 ); +has @.responses = ( Mailbox.new(:name('foo'),:1confidence) xx 10 ); has atomicint $!seen = 0; has Int $.expect; @@ -30,6 +30,6 @@ method del-path(IO:D $path, Str:D $mailbox --> Nil) { %.mails{$mailbox}{$path}:delete; } -method mailbox-for-email(MaildirIndexer::Email:D $email --> Str) { +method mailbox-for-email(MaildirIndexer::Email:D $email --> Mailbox) { return @!responses.shift; } diff --git a/t/store.t b/t/store.t index bb519b1..b29556c 100644 --- a/t/store.t +++ b/t/store.t @@ -29,8 +29,24 @@ subtest 'indexing' => { subtest 'finding' => { my Channel $file-channel .= new; - my TestIndex $index1 .= new(:responses('1',Str,'1',Str),:name('index1')); - my TestIndex $index2 .= new(:responses('2',Str,Str,Str),:name('index2')); + my TestIndex $index1 .= new( + :responses( + Mailbox.new(:name('1'),:1confidence), + Mailbox, + Mailbox.new(:name('1'),:confidence(0.5)), + Mailbox + ), + :name('index1') + ); + my TestIndex $index2 .= new( + :responses( + Mailbox.new(:name('2'),:confidence(0.5)), + Mailbox, + Mailbox.new(:name('2'),:1confidence), + Mailbox + ), + :name('index2') + ); my MaildirIndexer::Store $store .= new( :$file-channel, @@ -43,8 +59,8 @@ subtest 'finding' => { is-deeply( @responses, - $['1','2','1',Str], - 'indexes are consulted until a defined value', + $['1',Str,'2',Str], + 'indexes are consulted, undefs ignored, highest confidence wins', ); } -- cgit v1.2.3