| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 1 | package Krawfish::Corpus::Negation; |
| 2 | use Krawfish::Index::PostingsList; |
| 3 | use Krawfish::Posting::Doc; |
| 4 | use Krawfish::Query::Nothing; |
| 5 | use Krawfish::Log; |
| 6 | use strict; |
| 7 | use warnings; |
| 8 | |
| Akron | 2ea61aa | 2017-06-03 16:30:23 +0200 | [diff] [blame] | 9 | |
| 10 | # TODO: Remove in favor of WithOut! |
| 11 | |
| 12 | |
| 13 | |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 14 | use constant DEBUG => 0; |
| 15 | |
| 16 | # TODO: Support deleted docs |
| 17 | # Use PostingsLive! |
| 18 | |
| 19 | sub new { |
| 20 | my ($class, $index, $query) = @_; |
| 21 | |
| 22 | bless { |
| 23 | query => $query, |
| 24 | doc_id => -1, |
| 25 | last_doc_id => $index->last_doc |
| 26 | }, $class; |
| 27 | }; |
| 28 | |
| 29 | sub _init { |
| 30 | return if $_[0]->{init}++; |
| 31 | if (DEBUG) { |
| 32 | print_log('kq_neg', 'Initialize field') if DEBUG; |
| 33 | }; |
| 34 | $_[0]->{query}->next; |
| 35 | }; |
| 36 | |
| 37 | sub next { |
| 38 | my $self = shift; |
| 39 | $self->_init; |
| 40 | |
| 41 | return unless defined $self->{doc_id}; |
| 42 | |
| 43 | while (1) { |
| 44 | $self->{doc_id}++; |
| 45 | |
| 46 | print_log('kq_neg', 'Check doc id ' . $self->{doc_id}) if DEBUG; |
| 47 | |
| 48 | my $check = $self->_check; |
| 49 | return if $check == 0; |
| 50 | return 1 if $check == 1; |
| 51 | |
| 52 | # Advance negative postings |
| 53 | $self->{query}->next; |
| 54 | }; |
| 55 | |
| 56 | # print_log('vc_neg', 'Next "'.$self->term.'"') if DEBUG; |
| 57 | # |
| 58 | # my $return = $self->{postings}->next; |
| 59 | # if (DEBUG) { |
| 60 | # print_log('field', ' - current is ' . $self->current) if $return; |
| 61 | # print_log('field', ' - no current'); |
| 62 | # }; |
| 63 | # return $return; |
| 64 | }; |
| 65 | |
| 66 | sub _check { |
| 67 | my $self = shift; |
| 68 | |
| 69 | my $next_neg = $self->{query}->current; |
| 70 | |
| 71 | # The current element is negated |
| 72 | if ($next_neg && $self->{doc_id} == $next_neg->doc_id) { |
| 73 | print_log('kq_neg', 'Current doc_id ' . $self->{doc_id} . ' is negated') if DEBUG; |
| 74 | return -1 |
| 75 | } |
| 76 | |
| 77 | # Fine - and not at the end of the index |
| 78 | elsif ($self->{doc_id} < $self->{last_doc_id}) { |
| 79 | print_log('kq_neg', 'Current doc_id ' . $self->{doc_id} . ' is fine') if DEBUG; |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | # Reached the end of the index |
| 84 | else { |
| 85 | print_log('kq_neg', 'Current doc_id ' . $self->{doc_id} . ' is beyond index') if DEBUG; |
| 86 | $self->{doc_id} = undef; |
| 87 | return 0; |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | sub current { |
| 92 | my $self = shift; |
| 93 | return if !defined $self->{doc_id} || $self->{doc_id} == -1; |
| 94 | Krawfish::Posting::Doc->new( |
| 95 | $self->{doc_id} |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | sub freq { |
| Akron | d3355ba | 2017-05-17 21:16:35 +0200 | [diff] [blame] | 100 | my $self = shift; |
| 101 | $self->{last_doc_id} - $self->{query}->freq; |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | sub to_string { |
| 105 | return "not(" . $_[0]->{query}->to_string . ")"; |
| 106 | }; |
| 107 | |
| 108 | 1; |