| Akron | a604ddd | 2016-11-24 12:29:59 +0100 | [diff] [blame] | 1 | package Krawfish::Corpus; |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 2 | use strict; |
| 3 | use warnings; |
| Akron | 21143fe | 2017-11-06 19:54:30 +0100 | [diff] [blame] | 4 | use Krawfish::Util::Constants qw/NOMOREDOCS/; |
| Akron | ec35165 | 2017-11-01 16:04:38 +0100 | [diff] [blame] | 5 | use Role::Tiny; |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 6 | use Krawfish::Log; |
| 7 | |
| Akron | ec35165 | 2017-11-01 16:04:38 +0100 | [diff] [blame] | 8 | requires qw/current |
| 9 | next |
| 10 | next_doc |
| 11 | skip_doc |
| 12 | same_doc |
| 13 | clone |
| 14 | max_freq |
| 15 | to_string |
| 16 | /; |
| Akron | a604ddd | 2016-11-24 12:29:59 +0100 | [diff] [blame] | 17 | |
| Akron | a588d07 | 2017-10-13 14:45:34 +0200 | [diff] [blame] | 18 | # Krawfish::Corpus is the base class for all corpus queries. |
| 19 | |
| Akron | ec35165 | 2017-11-01 16:04:38 +0100 | [diff] [blame] | 20 | use constant DEBUG => 0; |
| 21 | |
| Akron | a604ddd | 2016-11-24 12:29:59 +0100 | [diff] [blame] | 22 | # Current span object |
| 23 | sub current { |
| 24 | my $self = shift; |
| 25 | return unless defined $self->{doc_id}; |
| Akron | e1a8a1b | 2017-10-20 16:51:09 +0200 | [diff] [blame] | 26 | return Krawfish::Posting->new( |
| Akron | 9248c33 | 2017-10-23 20:06:31 +0200 | [diff] [blame] | 27 | doc_id => $self->{doc_id}, |
| 28 | flags => $self->{flags} |
| Akron | a604ddd | 2016-11-24 12:29:59 +0100 | [diff] [blame] | 29 | ); |
| 30 | }; |
| 31 | |
| 32 | |
| Akron | a588d07 | 2017-10-13 14:45:34 +0200 | [diff] [blame] | 33 | # Overwrite query object |
| 34 | sub next_doc { |
| 35 | return $_[0]->next; |
| 36 | }; |
| 37 | |
| 38 | |
| Akron | ec35165 | 2017-11-01 16:04:38 +0100 | [diff] [blame] | 39 | # Overwrite |
| 40 | # Skip to (or beyond) a certain doc id. |
| 41 | # This should be overwritten to more effective methods. |
| 42 | sub skip_doc { |
| 43 | my ($self, $target_doc_id) = @_; |
| 44 | |
| 45 | print_log('corpus', refaddr($self) . ': skip to doc id ' . $target_doc_id) if DEBUG; |
| 46 | |
| 47 | while (!$self->current || $self->current->doc_id < $target_doc_id) { |
| Akron | 21143fe | 2017-11-06 19:54:30 +0100 | [diff] [blame] | 48 | $self->next_doc or return NOMOREDOCS; |
| Akron | ec35165 | 2017-11-01 16:04:38 +0100 | [diff] [blame] | 49 | }; |
| 50 | |
| Akron | ec35165 | 2017-11-01 16:04:38 +0100 | [diff] [blame] | 51 | return $self->current->doc_id; |
| 52 | }; |
| 53 | |
| 54 | |
| 55 | |
| 56 | # Move both operands to the same document |
| 57 | sub same_doc { |
| 58 | my ($self, $second) = @_; |
| 59 | |
| 60 | my $first_c = $self->current or return; |
| 61 | my $second_c = $second->current or return; |
| 62 | |
| 63 | # Iterate to the first matching document |
| 64 | while ($first_c->doc_id != $second_c->doc_id) { |
| 65 | print_log('corpus', 'Current span is not in docs') if DEBUG; |
| 66 | |
| 67 | # Forward the first span to advance to the document of the second span |
| 68 | if ($first_c->doc_id < $second_c->doc_id) { |
| 69 | print_log('corpus', 'Forward first') if DEBUG; |
| Akron | 21143fe | 2017-11-06 19:54:30 +0100 | [diff] [blame] | 70 | if ($self->skip_doc($second_c->doc_id) == NOMOREDOCS) { |
| 71 | return; |
| 72 | }; |
| Akron | ec35165 | 2017-11-01 16:04:38 +0100 | [diff] [blame] | 73 | $first_c = $self->current; |
| 74 | } |
| 75 | |
| 76 | # Forward the second span to advance to the document of the first span |
| 77 | else { |
| 78 | print_log('corpus', 'Forward second') if DEBUG; |
| Akron | 21143fe | 2017-11-06 19:54:30 +0100 | [diff] [blame] | 79 | if ($second->skip_doc($first_c->doc_id) == NOMOREDOCS) { |
| 80 | return; |
| 81 | }; |
| Akron | ec35165 | 2017-11-01 16:04:38 +0100 | [diff] [blame] | 82 | $second_c = $second->current; |
| 83 | }; |
| 84 | }; |
| 85 | |
| 86 | return 1; |
| 87 | }; |
| 88 | |
| 89 | |
| 90 | # Per default every operation is complex |
| 91 | sub complex { |
| 92 | return 1; |
| 93 | }; |
| 94 | |
| 95 | |
| Akron | 255a850 | 2017-11-12 20:07:34 +0100 | [diff] [blame] | 96 | # Stop compilation of results in non-compile queries |
| 97 | sub compile { |
| 98 | 1; |
| 99 | }; |
| 100 | |
| 101 | |
| 102 | |
| Akron | a604ddd | 2016-11-24 12:29:59 +0100 | [diff] [blame] | 103 | 1; |