| Akron | 5f9091c | 2017-03-24 20:37:35 +0100 | [diff] [blame] | 1 | package Krawfish::Corpus::Or; |
| 2 | use parent 'Krawfish::Corpus'; |
| 3 | use Krawfish::Log; |
| 4 | use strict; |
| 5 | use warnings; |
| 6 | |
| 7 | use constant DEBUG => 0; |
| 8 | |
| 9 | sub new { |
| 10 | my $class = shift; |
| 11 | bless { |
| 12 | first => shift, |
| 13 | second => shift, |
| 14 | doc_id => -1 |
| 15 | }, $class; |
| 16 | }; |
| 17 | |
| 18 | sub init { |
| 19 | return if $_[0]->{init}++; |
| 20 | $_[0]->{first}->next; |
| 21 | $_[0]->{second}->next; |
| 22 | }; |
| 23 | |
| 24 | sub next { |
| 25 | my $self = shift; |
| 26 | $self->init; |
| 27 | |
| 28 | my $first = $self->{first}->current; |
| 29 | my $second = $self->{second}->current; |
| 30 | |
| 31 | my $curr = 'first'; |
| 32 | |
| 33 | while ($first || $second) { |
| 34 | |
| 35 | # First span is no longer available |
| 36 | if (!$first) { |
| 37 | print_log('vc_or', 'Current is second operand (a)') if DEBUG; |
| 38 | $curr = 'second'; |
| 39 | } |
| 40 | |
| 41 | # Second span is no longer available |
| 42 | elsif (!$second) { |
| 43 | print_log('vc_or', 'Current is first operand (b)') if DEBUG; |
| 44 | $curr = 'first'; |
| 45 | } |
| 46 | |
| 47 | elsif ($first->doc_id < $second->doc_id) { |
| 48 | print_log('vc_or', 'Current is first operand (1)') if DEBUG; |
| 49 | $curr = 'first'; |
| 50 | } |
| 51 | elsif ($first->doc_id > $second->doc_id) { |
| 52 | print_log('vc_or', 'Current is second operand (1)') if DEBUG; |
| 53 | $curr = 'second'; |
| 54 | } |
| 55 | else { |
| 56 | print_log('vc_or', 'Current is first operand (4)') if DEBUG; |
| 57 | $curr = 'first'; |
| 58 | }; |
| 59 | |
| 60 | # Get the current posting of the respective operand |
| 61 | my $curr_post = $self->{$curr}->current; |
| 62 | |
| 63 | # Only return unique identifier |
| 64 | if ($self->{doc_id} == $curr_post->doc_id) { |
| 65 | |
| 66 | if (DEBUG) { |
| 67 | print_log('vc_or', 'Document ID already returned: '. $self->{doc_id}); |
| 68 | }; |
| 69 | |
| 70 | # Forward |
| 71 | $self->{$curr}->next; |
| 72 | |
| 73 | # Set current docs |
| 74 | $first = $self->{first}->current; |
| 75 | $second = $self->{second}->current; |
| 76 | |
| 77 | next; |
| 78 | }; |
| 79 | |
| 80 | $self->{doc_id} = $curr_post->doc_id; |
| 81 | |
| 82 | if (DEBUG) { |
| 83 | print_log('vc_or', 'Current doc is ' . $self->current->to_string); |
| 84 | print_log('vc_or', "Next on $curr operand"); |
| 85 | }; |
| 86 | |
| 87 | $self->{$curr}->next; |
| 88 | return 1; |
| 89 | }; |
| 90 | |
| 91 | $self->{doc_id} = undef; |
| 92 | return; |
| 93 | }; |
| 94 | |
| 95 | |
| 96 | sub to_string { |
| 97 | my $self = shift; |
| 98 | return 'or(' . $self->{first}->to_string . ',' . $self->{second}->to_string . ')'; |
| 99 | }; |
| 100 | |
| 101 | 1; |