| Akron | 95a37a4 | 2017-11-20 17:16:40 +0100 | [diff] [blame] | 1 | package Krawfish::Query::Constraint; |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 2 | use strict; |
| 3 | use warnings; |
| Akron | 7aed51c | 2017-10-31 16:23:49 +0100 | [diff] [blame] | 4 | use Role::Tiny::With; |
| Akron | 903894a | 2017-02-20 22:19:59 +0100 | [diff] [blame] | 5 | use Krawfish::Util::Buffer; |
| Akron | 993e014 | 2017-07-07 23:24:19 +0200 | [diff] [blame] | 6 | use List::Util qw/min/; |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 7 | use Krawfish::Log; |
| Akron | 71fc0ec | 2017-11-02 17:34:21 +0100 | [diff] [blame] | 8 | |
| 9 | with 'Krawfish::Query::Base::Dual'; |
| 10 | with 'Krawfish::Query'; |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 11 | |
| Akron | a588d07 | 2017-10-13 14:45:34 +0200 | [diff] [blame] | 12 | # TODO: |
| 13 | # Improve by skipping to the same document |
| 14 | # |
| 15 | # TODO: |
| 16 | # The check probably needs more than just the span |
| 17 | # information, e.g. to get the max_length() of |
| 18 | # a span for skip_pos() stuff. |
| 19 | |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 20 | use constant { |
| 21 | NEXTA => 1, |
| 22 | NEXTB => 2, |
| 23 | MATCH => 4, |
| Akron | 993e014 | 2017-07-07 23:24:19 +0200 | [diff] [blame] | 24 | DONE => 8, # Short circuit match |
| Akron | b723547 | 2017-07-15 16:20:44 +0200 | [diff] [blame] | 25 | DEBUG => 0 |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 26 | }; |
| 27 | |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 28 | |
| Akron | a588d07 | 2017-10-13 14:45:34 +0200 | [diff] [blame] | 29 | # Constructor |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 30 | sub new { |
| 31 | my $class = shift; |
| 32 | bless { |
| 33 | constraints => shift, |
| 34 | first => shift, |
| 35 | second => shift, |
| Akron | ea6a223 | 2017-02-03 18:12:00 +0100 | [diff] [blame] | 36 | |
| 37 | # TODO: |
| Akron | a588d07 | 2017-10-13 14:45:34 +0200 | [diff] [blame] | 38 | # Second operand should be nested |
| 39 | # in buffer by Dual |
| Akron | 903894a | 2017-02-20 22:19:59 +0100 | [diff] [blame] | 40 | buffer => Krawfish::Util::Buffer->new |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 41 | }, $class; |
| 42 | }; |
| 43 | |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 44 | |
| Akron | a588d07 | 2017-10-13 14:45:34 +0200 | [diff] [blame] | 45 | # Clone query |
| Akron | b765367 | 2017-08-07 14:34:14 +0200 | [diff] [blame] | 46 | sub clone { |
| 47 | my $self = shift; |
| 48 | __PACKAGE__->new( |
| 49 | [map { $_->clone } @{$self->{constraints}}], |
| 50 | $self->{first}->clone, |
| 51 | $self->{second}->clone |
| 52 | ); |
| 53 | }; |
| 54 | |
| 55 | |
| Akron | ea6a223 | 2017-02-03 18:12:00 +0100 | [diff] [blame] | 56 | # Check all constraints sequentially |
| Akron | f00b57a | 2017-02-02 20:09:32 +0100 | [diff] [blame] | 57 | sub check { |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 58 | my $self = shift; |
| Akron | ea6a223 | 2017-02-03 18:12:00 +0100 | [diff] [blame] | 59 | my ($first, $second) = @_; |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 60 | |
| 61 | # Initialize the return value |
| 62 | my $ret_val = 0b0111; |
| 63 | |
| 64 | # Iterate |
| 65 | foreach (@{$self->{constraints}}) { |
| 66 | |
| Akron | ea6a223 | 2017-02-03 18:12:00 +0100 | [diff] [blame] | 67 | # TODO: |
| 68 | # Under certain circumstances it may be |
| Akron | 1afe7a3 | 2017-07-15 15:30:04 +0200 | [diff] [blame] | 69 | # faster to |
| Akron | ea6a223 | 2017-02-03 18:12:00 +0100 | [diff] [blame] | 70 | |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 71 | # Check constrained |
| Akron | ea6a223 | 2017-02-03 18:12:00 +0100 | [diff] [blame] | 72 | my $check = $_->check($first, $second); |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 73 | |
| 74 | # Combine NEXTA and NEXTB rules |
| 75 | $ret_val &= $check; |
| 76 | |
| 77 | # Check matches |
| 78 | unless ($check & MATCH) { |
| 79 | |
| Akron | 1afe7a3 | 2017-07-15 15:30:04 +0200 | [diff] [blame] | 80 | if (DEBUG) { |
| 81 | print_log('constr', 'Constraint ' . $_->to_string . ' does not match'); |
| 82 | }; |
| 83 | |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 84 | # No match - send NEXTA and NEXTB rules |
| 85 | return $ret_val; |
| 86 | }; |
| Akron | 993e014 | 2017-07-07 23:24:19 +0200 | [diff] [blame] | 87 | |
| Akron | 1afe7a3 | 2017-07-15 15:30:04 +0200 | [diff] [blame] | 88 | if (DEBUG) { |
| 89 | print_log('constr', 'Constraint ' . $_->to_string . ' matches for ' . |
| 90 | $first->to_string . ' and ' . $second->to_string); |
| 91 | }; |
| 92 | |
| Akron | 993e014 | 2017-07-07 23:24:19 +0200 | [diff] [blame] | 93 | # If done flag is set, do short circuit |
| 94 | last if $check & DONE; |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 95 | }; |
| 96 | |
| Akron | f00b57a | 2017-02-02 20:09:32 +0100 | [diff] [blame] | 97 | # Match! |
| Akron | ea6a223 | 2017-02-03 18:12:00 +0100 | [diff] [blame] | 98 | $self->{doc_id} = $first->doc_id; |
| Akron | 6fc5b71 | 2017-10-24 14:48:39 +0200 | [diff] [blame] | 99 | |
| 100 | # Flags need to be considered from both operands, |
| 101 | # as not both operands are filtered |
| 102 | $self->{flags} = $first->flags | $second->flags; |
| Akron | ea6a223 | 2017-02-03 18:12:00 +0100 | [diff] [blame] | 103 | $self->{start} = $first->start < $second->start ? $first->start : $second->start; |
| 104 | $self->{end} = $first->end > $second->end ? $first->end : $second->end; |
| Akron | f00b57a | 2017-02-02 20:09:32 +0100 | [diff] [blame] | 105 | $self->{payload} = $first->payload->clone->copy_from($second->payload); |
| 106 | |
| Akron | ea6a223 | 2017-02-03 18:12:00 +0100 | [diff] [blame] | 107 | print_log('constr', 'Constraint matches: ' . $self->current->to_string) if DEBUG; |
| 108 | |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 109 | return $ret_val | MATCH; |
| 110 | }; |
| 111 | |
| 112 | |
| Akron | a588d07 | 2017-10-13 14:45:34 +0200 | [diff] [blame] | 113 | # Get maximum frequency of query |
| Akron | faf7685 | 2017-07-19 17:37:07 +0200 | [diff] [blame] | 114 | sub max_freq { |
| Akron | 993e014 | 2017-07-07 23:24:19 +0200 | [diff] [blame] | 115 | my $self = shift; |
| Akron | faf7685 | 2017-07-19 17:37:07 +0200 | [diff] [blame] | 116 | min($self->{first}->max_freq, $self->{second}->max_freq); |
| Akron | 993e014 | 2017-07-07 23:24:19 +0200 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | |
| Akron | 15fc197 | 2017-07-20 22:53:00 +0200 | [diff] [blame] | 120 | # Filter constraint by a corpus by only applying to |
| Akron | 2bc94da | 2017-10-27 15:20:36 +0200 | [diff] [blame] | 121 | # the least frequent operand, in case, there are no |
| 122 | # further requirements |
| Akron | 15fc197 | 2017-07-20 22:53:00 +0200 | [diff] [blame] | 123 | sub filter_by { |
| 124 | my ($self, $corpus) = @_; |
| 125 | |
| Akron | 2bc94da | 2017-10-27 15:20:36 +0200 | [diff] [blame] | 126 | my $first = $self->{first}; |
| 127 | my $second = $self->{second}; |
| 128 | |
| 129 | # There is a need for filtering |
| 130 | if ($first->requires_filter || $second->requires_filter) { |
| 131 | |
| 132 | # First operand requires a filter |
| 133 | if ($first->requires_filter) { |
| 134 | $self->{first} = $first->filter_by($corpus); |
| 135 | }; |
| 136 | |
| 137 | # Second operand requires a filter |
| 138 | if ($second->requires_filter) { |
| 139 | $self->{second} = $second->filter_by($corpus); |
| 140 | }; |
| 141 | |
| 142 | return $self; |
| 143 | }; |
| 144 | |
| Akron | 15fc197 | 2017-07-20 22:53:00 +0200 | [diff] [blame] | 145 | # The first operand is least frequent |
| Akron | 2bc94da | 2017-10-27 15:20:36 +0200 | [diff] [blame] | 146 | if ($first->max_freq < $second->max_freq) { |
| 147 | $self->{first} = $first->filter_by($corpus); |
| Akron | 15fc197 | 2017-07-20 22:53:00 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | # The second operand is least frequent (default) |
| 151 | else { |
| Akron | 2bc94da | 2017-10-27 15:20:36 +0200 | [diff] [blame] | 152 | $self->{second} = $second->filter_by($corpus); |
| Akron | 15fc197 | 2017-07-20 22:53:00 +0200 | [diff] [blame] | 153 | }; |
| 154 | return $self; |
| 155 | }; |
| 156 | |
| 157 | |
| Akron | 2bc94da | 2017-10-27 15:20:36 +0200 | [diff] [blame] | 158 | # Requires filtering |
| 159 | sub requires_filter { |
| 160 | my $self = shift; |
| 161 | if ($self->{first}->requires_filter) { |
| 162 | return 1; |
| 163 | } |
| 164 | elsif ($self->{second}->requires_filter) { |
| 165 | return 1; |
| 166 | }; |
| 167 | return 0; |
| 168 | }; |
| 169 | |
| 170 | |
| Akron | a588d07 | 2017-10-13 14:45:34 +0200 | [diff] [blame] | 171 | # Stringification |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 172 | sub to_string { |
| 173 | my $self = shift; |
| 174 | my $str = 'constr('; |
| 175 | $str .= join(',', map { $_->to_string } @{$self->{constraints}}); |
| 176 | $str .= ':'; |
| 177 | $str .= $self->{first}->to_string . ',' . $self->{second}->to_string; |
| 178 | return $str . ')'; |
| 179 | }; |
| 180 | |
| Akron | f00b57a | 2017-02-02 20:09:32 +0100 | [diff] [blame] | 181 | |
| Akron | 934afe0 | 2016-11-18 03:35:20 +0100 | [diff] [blame] | 182 | 1; |