| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 1 | package Krawfish::Corpus::Or; |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 2 | use strict; |
| 3 | use warnings; |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 4 | use Role::Tiny::With; |
| 5 | use Krawfish::Util::Bits; |
| 6 | use Krawfish::Log; |
| 7 | |
| 8 | with 'Krawfish::Corpus'; |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 9 | |
| Akron | 2bc94da | 2017-10-27 15:20:36 +0200 | [diff] [blame] | 10 | use constant DEBUG => 0; |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 11 | |
| 12 | sub new { |
| 13 | my $class = shift; |
| 14 | bless { |
| 15 | first => shift, |
| 16 | second => shift, |
| Akron | a588d07 | 2017-10-13 14:45:34 +0200 | [diff] [blame] | 17 | doc_id => -1, |
| 18 | flags => 0b0000_0000_0000_0000 |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 19 | }, $class; |
| 20 | }; |
| 21 | |
| Akron | a588d07 | 2017-10-13 14:45:34 +0200 | [diff] [blame] | 22 | |
| 23 | # Clone query object |
| Akron | c40598b | 2017-08-07 12:13:34 +0200 | [diff] [blame] | 24 | sub clone { |
| 25 | my $self = shift; |
| 26 | __PACKAGE__->new( |
| 27 | $self->{first}->clone, |
| 28 | $self->{second}->clone |
| 29 | ); |
| 30 | }; |
| 31 | |
| Akron | a588d07 | 2017-10-13 14:45:34 +0200 | [diff] [blame] | 32 | |
| 33 | # Initialize query |
| 34 | sub _init { |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 35 | return if $_[0]->{init}++; |
| 36 | $_[0]->{first}->next; |
| 37 | $_[0]->{second}->next; |
| 38 | }; |
| 39 | |
| Akron | a588d07 | 2017-10-13 14:45:34 +0200 | [diff] [blame] | 40 | |
| 41 | # Move to next posting |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 42 | sub next { |
| 43 | my $self = shift; |
| Akron | a588d07 | 2017-10-13 14:45:34 +0200 | [diff] [blame] | 44 | $self->_init; |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 45 | |
| 46 | my $first = $self->{first}->current; |
| 47 | my $second = $self->{second}->current; |
| 48 | |
| 49 | my $curr = 'first'; |
| Akron | 9248c33 | 2017-10-23 20:06:31 +0200 | [diff] [blame] | 50 | my $both; |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 51 | |
| 52 | while ($first || $second) { |
| 53 | |
| 54 | # First span is no longer available |
| 55 | if (!$first) { |
| 56 | print_log('vc_or', 'Current is second operand (a)') if DEBUG; |
| 57 | $curr = 'second'; |
| 58 | } |
| 59 | |
| 60 | # Second span is no longer available |
| 61 | elsif (!$second) { |
| 62 | print_log('vc_or', 'Current is first operand (b)') if DEBUG; |
| 63 | $curr = 'first'; |
| 64 | } |
| 65 | |
| 66 | elsif ($first->doc_id < $second->doc_id) { |
| 67 | print_log('vc_or', 'Current is first operand (1)') if DEBUG; |
| 68 | $curr = 'first'; |
| 69 | } |
| 70 | elsif ($first->doc_id > $second->doc_id) { |
| 71 | print_log('vc_or', 'Current is second operand (1)') if DEBUG; |
| 72 | $curr = 'second'; |
| 73 | } |
| 74 | else { |
| 75 | print_log('vc_or', 'Current is first operand (4)') if DEBUG; |
| 76 | $curr = 'first'; |
| Akron | 9248c33 | 2017-10-23 20:06:31 +0200 | [diff] [blame] | 77 | $both = 1; |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | # Get the current posting of the respective operand |
| 81 | my $curr_post = $self->{$curr}->current; |
| 82 | |
| 83 | # Only return unique identifier |
| 84 | if ($self->{doc_id} == $curr_post->doc_id) { |
| 85 | |
| 86 | if (DEBUG) { |
| 87 | print_log('vc_or', 'Document ID already returned: '. $self->{doc_id}); |
| 88 | }; |
| 89 | |
| 90 | # Forward |
| 91 | $self->{$curr}->next; |
| 92 | |
| 93 | # Set current docs |
| 94 | $first = $self->{first}->current; |
| 95 | $second = $self->{second}->current; |
| 96 | |
| Akron | 4204f17 | 2017-10-02 22:32:02 +0200 | [diff] [blame] | 97 | CORE::next; |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | $self->{doc_id} = $curr_post->doc_id; |
| Akron | 6fc5b71 | 2017-10-24 14:48:39 +0200 | [diff] [blame] | 101 | $self->{flags} = $curr_post->flags; |
| Akron | 9248c33 | 2017-10-23 20:06:31 +0200 | [diff] [blame] | 102 | |
| 103 | # Set flags |
| 104 | if ($both) { |
| 105 | if (DEBUG) { |
| 106 | print_log('vc_or', 'Current doc is ' . $self->current->to_string); |
| 107 | }; |
| Akron | 6fc5b71 | 2017-10-24 14:48:39 +0200 | [diff] [blame] | 108 | $self->{flags} |= $second->flags; |
| Akron | 9248c33 | 2017-10-23 20:06:31 +0200 | [diff] [blame] | 109 | }; |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 110 | |
| 111 | if (DEBUG) { |
| 112 | print_log('vc_or', 'Current doc is ' . $self->current->to_string); |
| 113 | print_log('vc_or', "Next on $curr operand"); |
| 114 | }; |
| 115 | |
| 116 | $self->{$curr}->next; |
| 117 | return 1; |
| 118 | }; |
| 119 | |
| Akron | 9248c33 | 2017-10-23 20:06:31 +0200 | [diff] [blame] | 120 | $self->{flags} = 0b0000_0000_0000_0000; |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 121 | $self->{doc_id} = undef; |
| 122 | return; |
| 123 | }; |
| 124 | |
| 125 | |
| Akron | a588d07 | 2017-10-13 14:45:34 +0200 | [diff] [blame] | 126 | # Stringification |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 127 | sub to_string { |
| 128 | my $self = shift; |
| 129 | return 'or(' . $self->{first}->to_string . ',' . $self->{second}->to_string . ')'; |
| 130 | }; |
| 131 | |
| Akron | faf7685 | 2017-07-19 17:37:07 +0200 | [diff] [blame] | 132 | |
| Akron | a588d07 | 2017-10-13 14:45:34 +0200 | [diff] [blame] | 133 | # Get maximum frequency |
| Akron | faf7685 | 2017-07-19 17:37:07 +0200 | [diff] [blame] | 134 | sub max_freq { |
| 135 | my $self = shift; |
| 136 | $self->{first}->max_freq + $self->{second}->max_freq; |
| 137 | }; |
| 138 | |
| 139 | |
| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 140 | 1; |